0

I've got two applications (in the same solution) that depend on the same shared library (separate project). This shared library does most of the work and takes in some services as interface dependencies.

My IoC container for each application looks identical, except for one service that needs to be different between the two applications. How can I minimize this duplication in the IoC containers?

redcurry
  • 2,381
  • 2
  • 24
  • 38

1 Answers1

1

If i understood the problem correctly (and take in account that it is hard to argue about correct solution without seeing the actual code) the approach would be like this.

Let's assume you have next solution structure:

SharedLib(can be just dll/nuget)
   ...
AppOne(refs and DI's shared)
   ...
AppTwo(refs and DI's shared)
   ...

Then what you'll need to do:

SharedLib
   ...
Shared.DI (refs SharedLib and package with your IoC container, and other stuff if needed)
   ...
AppOne (refs Shared.DI)
   ...
AppTwo (refs Shared.DI)
   ...

In Shared.DI you create a method called like RegisterShared which will handle all registrations and will be called from AppOne and AppTwo. I would suggest somth like this:

public static RegistrationExtensions
{
    public static void RegisterShared(this YourIoCBuilder builder,
       Action<YourIoCBuilder> registerUniqueServiceAction)
    {
         // do the common stuff 
         registerUniqueServiceAction(builder);             
    }
}

You can omit the registerUniqueServiceAction and register the needed service just before or after but personally I prefer more explicit API so it would be obvious that this method does not register everything needed for the SharedLib to work. Also depended on what exactly IoC you use you can provide more obvious signature for registerUniqueServiceAction so it would be clear what actually is needed.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132