Using emballo to get singletons

3 views
Skip to first unread message

Ejik

unread,
Feb 28, 2010, 3:55:32 AM2/28/10
to Emballo
Hi.

I just wrote such test:

var
im, im2 : IImbaseService;
begin
im := TImbaseService.create();
GetDIRegistry.RegisterFactory(IImbaseService, im);
im2 := emballo.get(IImbaseService) as IImbaseService;
CheckSame(im2, im);
end;

Test fail. How I can register instance of object to get singleton
from Emballo?

Magno Machado

unread,
Feb 28, 2010, 7:05:44 AM2/28/10
to emb...@googlegroups.com
This is very strange... I've just made a similar test here and it's OK (But I use D2010)

Maybe you have registered another factory for IImbaseService.
Try this:
var
   im, im2 : IImbaseService;
begin
   im := TImbaseService.create();
   GetDIRegistry.Clear();

   GetDIRegistry.RegisterFactory(IImbaseService, im);
   im2 := emballo.get(IImbaseService) as IImbaseService;
   CheckSame(im2, im);
end;

Ejik

unread,
Mar 3, 2010, 1:56:42 PM3/3/10
to Emballo
Hi.
Thanks for help. That was my bug. Im register another factory in
initialization section.
GetDIRegistry.RegisterFactory(IImbaseService, TImbaseService);

Another side, when I write this code, it mean that i register
IImbaseService not as a singleton? Am I right?

Magno Machado

unread,
Mar 3, 2010, 2:36:44 PM3/3/10
to emb...@googlegroups.com
>Another side, when I write this code, it mean that i register
>IImbaseService not as a singleton? Am I right?
Yes... This way, a new TImbaseService will be instantiated everytime you request an IImbaseService

I have a class that add support for singleton (without requiring you to pre-instantiate the object) on Emballo's old version... I just need to add it to the project

Ejik

unread,
Mar 3, 2010, 4:00:41 PM3/3/10
to Emballo
It will be nice to have that class!!!

Magno Machado

unread,
Mar 9, 2010, 5:47:33 PM3/9/10
to emb...@googlegroups.com
Hi... 
First, I apologise for taking too long to implement your request... But now it's done :)

I created two new methods on DIRegistry called RegisterFactorySingleton, one receive a GUID and a TInjectableClass, and other receive a GUID, any TClass and a constructor address. You use like:
GetDIRegistry.RegisterFactorySingleton(IMyService, TMyService); // You can use it if TMyService inherits from TInjectable

GetDIRegistry.RegisterFactorySingleton(IMyService, TMyService, @TMyService.Create); { TMyService can inherit from any class, but TMyService.Create must be a no-argument constructor }

Ejik

unread,
Mar 9, 2010, 10:45:07 PM3/9/10
to Emballo
This is wonderful news!!!
Reply all
Reply to author
Forward
0 new messages