0

I have an interface called IService and derived services like IServiceA : IService, IServiceB : IService, IServiceC : IService, etc. The implementation classes of those services needs a context parameter of type MyContext, and some services will need a refernece to other services in that context. for example: ServiceA : IServiceA needs MyContext and IServiceC (in constructor). ServiceB : IServiceB only needs MyContext.

  • I want to create an abstract factory that will create every service
    upon request. something like:

    T GetService<T>(/*MyContext context - ?*/) where T : IService;

    but i don't quite know how to add the context part. I understand that the MyContext parameter is part of the implementation details of every service, so I'll be happy to hear
    about different ways to solve this problem.

  • I also need a way to create those services according to their context value, so that for example multiple requests to create IServiceA with the same MyContext parameter will return the same instance of IServiceA and if a request for IServiceA with different MyContext parameter will return a different instance.
  • Finally, I need a way to integrate it with a DI Container (Castle Windsor preferred) so that the creation and the context handling will occur close to the application root.

Thanks

MaorB
  • 117
  • 2
  • 10
  • 1
    Nice requirments, but we are not here to write code instead of you. Show some code and exact problem ! – mybirthname Nov 01 '16 at 14:07
  • Why do you need an abstract factory for this? All constructor parameters are injectables. Just inject them via the constructor, setup the registrations, and you're done. – Maarten Nov 01 '16 at 14:17
  • I think he want to create context dependent singletons (one instance per context). – eocron Nov 01 '16 at 14:19
  • If you want different instances for certain circumstances is a **lifestyle/scoping** issue. Register your component with the correct scope/lifestyle, and it will solve itself. – Maarten Nov 01 '16 at 14:21
  • Possible duplicate of [Castle.Windsor lifestyle depending on context?](http://stackoverflow.com/questions/11873905/castle-windsor-lifestyle-depending-on-context) – eocron Nov 01 '16 at 14:21
  • @mybirthname I know you are not here to write code instead of me, there isn't code here anyway, only an idea of implementation. I chose not to include the services implementation because i think it is irrelevant to the problem and i prefer not to add redundant code here. @eocron06 yes i need context dependent singleton my problem is how to use it with the `MyContext` parameter And I don't believe it is duplicate,I read the answer about Hybrid lifestyle, I need something similar but not exaclty the same,there the use of it is especially for WebRequests for ThreadStatic Resources, here I don't. – MaorB Nov 01 '16 at 18:07
  • @Maarten how can I register my components in this way? Do you have any link I can use to accomplish that? – MaorB Nov 01 '16 at 18:08
  • Have a look at [Factory method with DI and Ioc](http://stackoverflow.com/a/31971691/181087). – NightOwl888 Jan 18 '17 at 05:02
  • I've ended up using a mixture of solutions (DI container's scope accessor and a custom scope). I will post a proper answer, I just didn't had time to provide my solution as a proper answer. But NightOwl888 the Link you provided is also a great suggestion with a mixture of the custom scope (for the context parameter),I will add it to the answer as well. Thanks. – MaorB Mar 08 '17 at 18:14

0 Answers0