Search code examples
wpfwcfdependency-injectionunity-container

DI Unity in multiple projects - best practises


I have a question. I have WPF project with Unity DI container, library project and WCF project in common solution. In WCF library is service which read from the database. UserManager set in the constructor, but this has an error.

My question is must unity container be in each project in solution or I can configure one unity container so that it finds dependencies in the whole solution?

My container is configured as follows:

var container = new UnityContainer();
container.RegisterTypes(
    AllClasses.FromLoadedAssemblies(), 
    WithMappings.FromMatchingInterface, 
    WithName.Default);

But it doesn't resolve dependencies in the WCF application.


Solution

  • must unity container be in each project in solution or I can configure one unity container so that it finds dependencies in the whole solution?

    Neither. You will typically have each start-up project create its own container, and configure its dependencies. Since you have multiple start-up projects (both a web service and a desktop application), you will have multiple places where you configure the container. This is related to the concept of the Composition Root. Also look at Composition Root reuse for more details.