Whose responsibility should be the registration of types with an IoC container?

Suppose I have an ASP.NET MVC web application that consists of many assemblies. Here is a very simple example:

  • Myapp.web
    • web application
  • MyApp.Services
    • contains interfaces and implementations of domain services, for example. IOrderProcessor, DefaultOrderProcessor
  • MyApp.DAL
    • contains interfaces and repository implementations, for example. IOrderRepository, SqlOrderRepository

The web application will initialize the IoC container at startup (for example, register controllers, etc.). I’m not sure whose responsibility for registering domain services and repositories should be.

Now I have created a static class in each assembly, each of which contains a method responsible for registering the types contained in the assembly. For instance. To build DAL:

namespace MyApp.DAL
{
  public static class AssemblyInitialization
  {
    public static void RegisterTypes(IUnityContainer container)
    {
      var lm = new TransientLifestyleManager();
      container.RegisterType<IOrderRepository, SqlOrderRepository>(lm);
      ...
    }
  }
}

- ( unit-test , , , ).

, . , IoC (Unity , ). , - ( -).

, , :

  • (-)?
  • () ?
  • IoC ?
  • , ?
+3
3

, MyApp.Web. " /" glabal.asax.

+1

"" . , , IRegistration .

, . IRegistration .

MVC, Turbine, . , :)

+1

IMHO, your code should delay the implementation of this choice as long as possible - until the last crucial moment. I use Unity, so I can wait for web.config to load to declare which types I want to introduce.

I believe that if you do not use the configuration file for this, you are doing something wrong. The purpose of using the DI framework is to reduce and / or remove these compile-time dependencies.

0
source

Source: https://habr.com/ru/post/1763595/


All Articles