Dependency injection vs assembly dependencies

守給你的承諾、 提交于 2019-12-04 07:27:21

If you want to use a DI container, then you should only use it in the Application itself, not in your other class libraries (e.g. BusinessLogic and DataAccessLayer). The place in the Application where you compose your object graph is called the Composition Root.

Quoting for that article:

Only applications should have Composition Roots. Libraries and frameworks shouldn't.

Since you have already prepared your classes to enable poor man DI (now called Pure DI), you should be fine. DI is now enabled in your libraries (Please note that DI and DI containers are different things).

Your application now can wire everything together from all the class libraries, even if that means that your application project will need to reference all the other class libraries.

In my opinion, you would be better off without a DI container (even in the application layer), see this article for a reason why.

  1. In the DL layer create a method to register its components.
  2. In the BL layer do the same but also call the DL method.
  3. In the A layer do the same but also call the BL method.

That way the relatively upper layer is shielded from the lower layers and everything is properly registered. To the A layer it is an implementation detail that the DL even exists. It only knows about the BL layer.

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