Service Interface for different Implementation

thexeb's Avatar

thexeb

03 Aug, 2013 10:00 PM

Hi ,
i can't find a way to do this.
I have a service interface which exposes methods like getAllContacts() , getContactByName(name:String) etc.
Now this interface is implemented by two(or more) concrete class, e.g one for LocalDataBase , RemoteDatabase,or a XMLSource .Depending on the App Settings that tells which Service to use ,how that kind of mapping is done?
i have a mediator(or command) which have an instance of IService injected. Now when user calls a service operation , i want to get the required concrete sevice object as defined in AppSettings model. only thing i know is that i can map an interface to a single class like
injector.map(IService).toType(LocalDatabaseService);

,hope i make myself clear.

  1. Support Staff 1 Posted by Ondina D.F. on 04 Aug, 2013 07:47 AM

    Ondina D.F.'s Avatar

    Hello,

    Named mappings:

    injector.map(IService, "local").toType(LocalDatabaseService);
    injector.map(IService, "remote").toType(RemoteDatabaseService);
    

    Injection:

    [Inject (name="local")]
    public var service: IService;//instance of LocalDatabaseService
    
    or
    
    [Inject (name="remote")]
    public var service: IService;//instance of RemoteDatabaseService
    

    That's all there is to it.

    Ondina

  2. 2 Posted by thexeb on 04 Aug, 2013 02:44 PM

    thexeb's Avatar

    oh , thats intresting , and confusing too .in my mediator or model , wherever the service is required , how can i check against my AppSetting.SomeValue ??? My Implementation is consistent regardless of Service ,and also ,as i have to specify 'name' in meta tag , would'nt it require to declare multiple Injected vars?? and then how i will check which Injected Object to use??

  3. Support Staff 3 Posted by Ondina D.F. on 04 Aug, 2013 03:16 PM

    Ondina D.F.'s Avatar

    An option would be something like this:
    http://knowledge.robotlegs.org/discussions/questions/590-using-mult...

    or this:
    http://knowledge.robotlegs.org/discussions/robotlegs-2/4363-mapping...

    or using different commands, RemoteRequestCommand to access the RemoteDatabaseService, LocalRequestCommand to access the LocalDatabaseService and so on

  4. 4 Posted by thexeb on 04 Aug, 2013 03:31 PM

    thexeb's Avatar

    i was more like hoping to use gaurds or hooks(which are nt the case with swift injector , :( ), in the config , Or may be a command which handles the conditions in AppSettings.DATBASE_TYPE , and gives appropriate mapping to the injecter , how can be done something like that?

  5. 5 Posted by thexeb on 04 Aug, 2013 03:34 PM

    thexeb's Avatar

    hmm thanks , i try these aproachs

  6. thexeb closed this discussion on 04 Aug, 2013 03:37 PM.

  7. Ondina D.F. re-opened this discussion on 04 Aug, 2013 05:04 PM

  8. Support Staff 6 Posted by Ondina D.F. on 04 Aug, 2013 05:04 PM

    Ondina D.F.'s Avatar

    If you want to use if/switch statements inside a class, you can let the injector retrieve an instance of the service depending on your conditions (payload of an event, for example):

    var service:ISocketService;
    if (event.service == "local")
        service = injector.getInstance(IService, "local")
    else
        service = injector.getInstance(IService, "remote")
    service.connect();
    

    But, be aware that if/switch statements could turn ugly very quickly, especially if you have lots of them.

    [EDIT] Actually, this would suffice, if you had named mappings for all your services that implement the same interface:

    service = injector.getInstance(IService, event.service)
    service.connect();
    
  9. Ondina D.F. closed this discussion on 04 Aug, 2013 05:04 PM.

  10. thexeb re-opened this discussion on 04 Aug, 2013 07:33 PM

  11. 7 Posted by thexeb on 04 Aug, 2013 07:33 PM

    thexeb's Avatar

    oh thanks alot ondina , this solution clean for me. :)

  12. thexeb closed this discussion on 04 Aug, 2013 07:33 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