Dependency Injection for A Proxy Class Object

thexeb's Avatar

thexeb

10 Mar, 2014 10:13 PM

Hi , sorry if the question is ridiculous or redundant , but i could not find anything in discussion area.As i do AS3 programming as a hobby so i try to explain my problem.
I am working on an App , which will have two versions , one "Offline" and the other one "Online" , with respective "Service" to persistence media. I will be configuring App to use different implementation for IService.

...config.as
context.injector.map(IService,"SQLITE_SERVICE").toSingleton(SqlLiteDBService);
context.injector.map(UserVOProxy);

In my previous App , i used Mediator or Command to use IService and fillup the VOs'.
Now I am mediating VO's with a Proxy class, and By Proxy pattern , it will be loading the VO .
So i am trying

...UserVOProxy.as
[Inject]
public var service:IService;
[Inject]
public var injector:IInjector;

but both are not working , the objects are null when i debug.
Also i need multiple instances of UserVOProxy , so i don't want a Singleton implementation . As this proxy may contain another proxies of some VOs' , i think it would be better to let the proxies handle the loading of data when required. Also i used a kind of 'hack' by providing Injector object from Medaitor when creating Proxy. But there may be many Views-Mediator and there will be different Number of Proxy objects based on user selection.A single View will be displaying different UserVO under selection.
Can anyone help ,What am i missing? or this approach is all wrong?

  1. Support Staff 1 Posted by Ondina D.F. on 11 Mar, 2014 08:45 AM

    Ondina D.F.'s Avatar

    Hello,

    If you map your service like this

    context.injector.map(IService, "SQLITE_SERVICE").toSingleton(SqlLiteDBService);
    

    You must use the name in your Inject metatag for the injection to occur:

    [Inject (name="SQLITE_SERVICE")]
    public var service:IService;
    

    This is incomplete, it only maps a request description :

    context.injector.map(UserVOProxy);
    

    Also i need multiple instances of UserVOProxy , so i don't want a Singleton implementation

    See the difference between asSingleton()and toType

    injector.map(SomeModel).asSingleton();
    asSingleton and toSingleton - Makes the mapping return a lazily constructed singleton instance of the mapped type for each consecutive request.

    injector.map(ISomeModel).toType(SomeModel);
    toType - Makes the mapping return a newly created instance of the given type for each consecutive request.

    .asSingleton (), .toType () return the InjectionMapping the method is invoked on

    Let me know if it helped.

    Ondina

  2. 2 Posted by thexeb on 13 Mar, 2014 12:19 PM

    thexeb's Avatar

    thanks alot ondina , i thought may be i could use without implementing some interface .

  3. Support Staff 3 Posted by Ondina D.F. on 18 Mar, 2014 08:36 AM

    Ondina D.F.'s Avatar

    My pleasure:)

    i thought may be i could use without implementing some interface .

    What do you mean?

  4. 4 Posted by thexeb on 19 Mar, 2014 12:04 PM

    thexeb's Avatar

    i meant to say that proxy should be implementing IProxy , so that it is Mapped to concrete class instance, as u said

    context.injector.map(UserVOProxy);
    

    is incomplete.

  5. Support Staff 5 Posted by Ondina D.F. on 19 Mar, 2014 12:35 PM

    Ondina D.F.'s Avatar

    Yes. Depending on what you need, you can use
    .asSingleton(), .toSingleton(), .toValue(), or .toType()

    If SomeService is implementing an interface, IService, then you map it like this:

    injector.map(IService).toSingleton(SomeService);

    or like so

    injector.map(IService).toType(SomeService);

    and if it's not implementing an interface:

    injector.map(SomeService).asSingleton();

    or

    injector.map(SomeService).toType(SomeService);

    Letting services and models implement an interface is a good practice, though.

  6. Support Staff 6 Posted by Ondina D.F. on 19 Mar, 2014 12:49 PM

    Ondina D.F.'s Avatar

    I guess this is resolved. I'm going to close this discussion, but you can re-open it, if need be.

    Cheers,
    Ondina

  7. Ondina D.F. closed this discussion on 19 Mar, 2014 12:49 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac