How to use/configure Unity Container IOC in my situation

六眼飞鱼酱① 提交于 2019-12-07 03:08:27
Mark Seemann

Each application owns a separate container and is responsible for configuring that container instance. Configuration and the call to resolve should happen as close as possible to the application's entry point. This is called the Composition Root.

Your Windows Forms application should own, configure and resolve the container in its bootstrapping code. That there might be other applications that also use ClassLibraryA is totally irrelevant to it.

The Web Service should own another container and configure it to suit its needs. It knows nothing about the Windows Forms application.

Here is more information about Composition Roots:

All the libraries should be completely DI Container-agnostic so that you can use any container (and any container instance) to wire up all the dependencies.

See here for more details:

As you may notice, these are general principles and apply to DI overall - not just to Unity.

you should call the Container.Configure at the very beginning of the app, but the ClassLibraryA should receive the dependency manually,perhaps having the WebMethod that is called insantiate the (or ask the ioc for an instance) right type

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