Search code examples
c#.netdependency-injectionunity-containerenterprise-library

Where should I register-resolve my types in a Console Application with Unity Container?


I am a beginner to Unity DI Container.

I want to use it with a simple Console app.

I will use 'Code as Configuration'

Where should I call my method that Registers and Resolves my abstracts? From the Main method itself or there is a better, more elegant way?


Solution

  • Have a look at the concept of Composition Roots. For a console app the Main() method is exactly the right place.

    For Unity you can bundle your configuration in a class that derives from UnityContainerExtension and place the registration code inside the Initialize() method.

    Then add your extension to the container.