Dependency Injection Container for each Class Library

我的未来我决定 提交于 2019-12-02 03:17:45

My intention was to have each project having its own DI container (says Unity DI). I'm not sure that each project can have its own DI container.

As explained here, you should compose all object graphs:

As close as possible to the application's entry point.

This place is called the Composition Root:

A Composition Root is a (preferably) unique location in an application where modules are composed together.

In other words, the only place that you should use your DI container and configure your application's dependencies is in the start-up project. All other projects should be oblivious to the container and should purely apply Constructor Injection.

I have read some of the article, showing that it cannot be done

It can be done, but you shouldn't.

If each project can have its own DI, when registering the IMapper as instance will it override IMapper of other layers?

This problem goes away if you apply the Composition Root pattern. In the Composition Root you have full control over which component gets which dependency.

PRO TIP: Read this book.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!