Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-01

Inversion of control (IoC) and Dependency injection (DI)

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

luiz americo pereira camara

Starting a new thread.

2015-01-05 15:16 GMT-03:00 silvioprog :

Show quoted text 19 lines
On Mon, Jan 5, 2015 at 12:54 PM, Marco van de Voort wrote: > > As a first step in the 3.0.0 release process the stable branch was > branched > off to branches/fixes_3_0 and the version number was updated to 3.0.1 > > The version in trunk was raised to 3.1.1 > > Scripts might need modification accordingly. Good news! Chances of custom attributes[1] (or something like this) in release 3.0? I have plans to use IoC[2] and DI using this feature.

I created a IoC Container that can be found at
https://code.google.com/p/luipack/source/browse/trunk/luicomponents/luiioccontainer.pas . It has no dependencies.

With it is possible to implement Service Locator and Property Injection patterns. Constructor Injection is not possible due to lacking of Extended RTTI support in fpc.

While the C# crowd praises the Constructor Injection pattern ( http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container ), i had hard times figuring how it would work in real world / Lazarus projects. Also, polluting the constructor signature is not something that i like. And finally with Constructor Injection is not possible to use with TComponent / TForm

So in the end, i use the service locator and have plans to use Property Injection pattern

BTW: Custom Attributes support would help to have a nicer Property Injection implementation

Luiz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150105/3cc8954b/attachment-0001.html>

Reply by email

Marcos Douglas
to luiz americo pereira camara
luiz americo pereira camara wrote 44 lines
Starting a new thread. 2015-01-05 15:16 GMT-03:00 silvioprog : > > On Mon, Jan 5, 2015 at 12:54 PM, Marco van de Voort > wrote: >> >> >> As a first step in the 3.0.0 release process the stable branch was >> branched >> off to branches/fixes_3_0 and the version number was updated to 3.0.1 >> >> The version in trunk was raised to 3.1.1 >> >> Scripts might need modification accordingly. > > > Good news! > > Chances of custom attributes[1] (or something like this) in release 3.0? I > have plans to use IoC[2] and DI using this feature. > I created a IoC Container that can be found at https://code.google.com/p/luipack/source/browse/trunk/luicomponents/luiioccontainer.pas . It has no dependencies. With it is possible to implement Service Locator and Property Injection patterns. Constructor Injection is not possible due to lacking of Extended RTTI support in fpc. While the C# crowd praises the Constructor Injection pattern ( http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container ), i had hard times figuring how it would work in real world / Lazarus projects. Also, polluting the constructor signature is not something that i like. And finally with Constructor Injection is not possible to use with TComponent / TForm So in the end, i use the service locator and have plans to use Property Injection pattern BTW: Custom Attributes support would help to have a nicer Property Injection implementation

Interesting.
Could you give us some example how this class works?

Regards,
Marcos Douglas

Reply by email

luiz americo pereira camara
to Marcos Douglas

2015-01-05 21:44 GMT-03:00 Marcos Douglas :

Show quoted text 19 lines
On Mon, Jan 5, 2015 at 6:07 PM, luiz americo pereira camara > > I created a IoC Container that can be found at > https://code.google.com/p/luipack/source/browse/trunk/luicomponents/luiioccontainer.pas > . It has no dependencies. > > So in the end, i use the service locator and have plans to use Property > Injection pattern > > BTW: Custom Attributes support would help to have a nicer Property Injection > implementation Interesting. Could you give us some example how this class works?

There's a demo in demos/ioccontainer

In a working project i use a IPresentationManager
https://code.google.com/p/luipack/source/browse/trunk/luicontrols/presentationmanager.pas#36

When a class needs to use it, resolves from a TIoCContainer (Service Locator pattern). The global variable and concrete class are not exposed. In the other hand depends of the IoCContainer. I have plans to switch to a Property Injection pattern that helps to decouple a bit more.

Luiz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150105/e1947d89/attachment.html>

Reply by email

silvioprog
to luiz americo pereira camara: Starting a new thread. 2015-01-05 15:16 GMT-03:00 silvioprog : I created a IoC Container…

On Mon, Jan 5, 2015 at 5:07 PM, luiz americo pereira camara <

Show quoted text 29 lines
wrote: Starting a new thread. 2015-01-05 15:16 GMT-03:00 silvioprog : > On Mon, Jan 5, 2015 at 12:54 PM, Marco van de Voort > wrote: > >> >> As a first step in the 3.0.0 release process the stable branch was >> branched >> off to branches/fixes_3_0 and the version number was updated to 3.0.1 >> >> The version in trunk was raised to 3.1.1 >> >> Scripts might need modification accordingly. > > > Good news! > > Chances of custom attributes[1] (or something like this) in release 3.0? > I have plans to use IoC[2] and DI using this feature. > > I created a IoC Container that can be found at https://code.google.com/p/luipack/source/browse/trunk/luicomponents/luiioccontainer.pas . It has no dependencies.

Awesome!

With it is possible to implement Service Locator and Property Injection

patterns. Constructor Injection is not possible due to lacking of Extended RTTI support in fpc.

Can you send a small (and isolated) sample showing how to use it?

Show quoted text 13 lines
While the C# crowd praises the Constructor Injection pattern ( http://www.devtrends.co.uk/blog/how-not-to-do-dependency-injection-the-static-or-singleton-container ), i had hard times figuring how it would work in real world / Lazarus projects. Also, polluting the constructor signature is not something that i like. And finally with Constructor Injection is not possible to use with TComponent / TForm So in the end, i use the service locator and have plans to use Property Injection pattern BTW: Custom Attributes support would help to have a nicer Property Injection implementation

I've plan to use something like this (CDI inspirations: http://cdi-spec.org ):

The model:

uses

  My.Company.MySDK.EntityAttributes, My.Company.MySDK.TableAttributes ...;

type
[Entity]
[Table('products')]
TProduct = class(TObject)

  public
    [Id][GeneratedValue]
    property Id: LongInt ...;
    [ManyToOne][JoinColumn('OrderId')]
    property OrderId: LontInt ...;
    [NotNull][NotEmpty][Size(1, 10)][Unique]
    property Name: String ...;
    [Nulls][Lenght(100)][Unique]
    property Description: String ...;
    [MyCustomCurrConversion(TypeInfo(Currency), TypeInfo(String), ',0.00')]
    [MyCustomMinMax('Price must be >= %d and <= %d.', [1, 1000])]
    property Price: Currency ...;
  end;

The DAO:

type
  [RequestScoped]
  TProductDao = class(TObject)
  private
    [Inject] property Session: TSession ... ;
    [Transational] procedure Add(AProduct: TProduct);
    ...
  end;
...procedure TProductDao.Add(AProduct: TProduct);begin
  Session.Save(AProduct);end;

The *produces* (to use external libs like *JCore*):

type
  [ApplicationScoped]
  TSessionCreator = class(TObject)
  private
    [Inject] property SessionFactory: TJCoreSession ... ;
    [Produces] function GetSession: TSession;
...
  end;
...
TSessionCreator.GetSession: TSession;begin
  Result := ASessionFactory.OpenSession;end;
TSessionCreator.Close([Disposes]ASession: TSession) {begin  if
ASession.IsOpen    ASession.Close;end;

The controller:

uses
  My.Company.ControllerAttributes ... ;
type
  [Controller]
  TProcuctController = class(TObject)
  public
    [Post][Transational] procedure Add([Valid] AProduct: TProduct); //
[Valid]: to validade properties
    [Get][Path('./product')][Transational] procedure
List([Pagination('page={[0-9]}&rows={[0-9]}')] AProducts:
TArrayList<TProduct>); // to paginate products
    [Inject] property ProductDao: TProductDao ...; // inject DAO
    [Inject] property Result: TResponse ...; // inject Response
  end;
...
procedure TProcuctController.Add(AProduct: TProduct);begin
  ProductDao.Add(AProduct);
  Result.SetAttribute(AProduct,
'product').RedirectTo(TProductController).List;end;
procedure TProcuctController.List(AProducts: TArrayList<TProduct>);begin
  Result.SetAttribute(AProducts, 'products'); // to create a variable
"products" in my BSP viewend;

The view:

<html>
<ul>
<c:forEach items="${products}" var="product">
<li> ${product.name} - <fmt:currency value="${product.price}" mask="R$ ,0.00"/></li>
</c:forEach>
</ul>
</html>

But I think that I'll still wait some years to make it possible in Free Pascal hehe ...

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150111/e7269c03/attachment-0001.html>

Reply by email

luiz americo pereira camara
to silvioprog

2015-01-11 17:12 GMT-03:00 silvioprog :

Show quoted text 10 lines
On Mon, Jan 5, 2015 at 5:07 PM, luiz americo pereira camara < > wrote: > I created a IoC Container that can be found at > https://code.google.com/p/luipack/source/browse/trunk/luicomponents/luiioccontainer.pas > . It has no dependencies. > Awesome!

Thanks.

Show quoted text 8 lines
With it is possible to implement Service Locator and Property Injection > patterns. Constructor Injection is not possible due to lacking of Extended > RTTI support in fpc. > Can you send a small (and isolated) sample showing how to use it?

There's a demo in demos/ioccontainer folder

In a real project, in the app start, i do:

  FPresentations := TPresentationManager.Create(Self);
  Services.Register(IPresentationManager, FPresentations);

When i need the service i do:

  FPresentations :=
IPresentationManager(Services.Resolve(IPresentationManager));

The global, nor the actual implementation is exposed, making easier to test. There's still a dependency to the container (Services), this is the whole point of criticism to service locator pattern.
In the other side there always be a place where the dependency is set, even in other DI patterns.

BTW: Custom Attributes support would help to have a nicer Property

Show quoted text 7 lines
> Injection implementation > I've plan to use something like this (CDI inspirations: http://cdi-spec.org):

Seems a full ORM plus a MVC like framework.

I'm more modest. I use these classes to make modular code thus a bit more maintanable.

All in all i use the IoC container little, in my project i register only two interfaces. In other places, i configure the dependency manually

Luiz
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20150111/20cc45d5/attachment.html>

Reply by email