Stairway pattern implementation

怎甘沉沦 提交于 2019-11-28 04:59:14
Gary McLean Hall

Application entry points should be the composition root, as per Mark Seemann's excellent book on Dependency Injection. Here, the issue is more about Dependency Inversion, whereby both client and implementation should both depend on abstractions.

What that diagram doesn't show, but hopefully other parts of the book make clear, is that the entry point will naturally and necessarily reference everything that is needed to construct whatever are your resolution roots (controllers, services, etc.) But this is the only place that has such knowledge.

Bear in mind that it is sometimes ok for clients to 'own' the interfaces on which they depend: the interface ISecurityService might live in the Controllers assembly, the IUserRepository might live in the ServiceImplementations assembly, and so on. This is impractical when >1 client needs access to the interface, of course.

If you follow SOLID, you will naturally find that Dependency Injection is a necessity, but Inversion of Control containers are less of a priority. I find myself using Pure Dependency Injection (manual construction of resolution roots) more and more often.

In that case Client project should contain references to both Service and ServiceImplementation. Those references will be used only to create IoC container which will be used be DI. At application start you need to register all interface implementations in IoC container.

If you will implement ServiceImplementation against Service interface and you will code Client based on Service intereface then there will be no dependency on ServiceImplementation.

You can also see how Stairway pattern is implemented in samples for "Adaptive Code via C#":

https://github.com/garymcleanhall/AdaptiveCode/tree/master/Sprints/sample-sprint2-markdown

I would put it in the ServiceFactory. You need some parameter e.g. passed in the factory constructor or retrieved from configuration etc. that determines which IService implementation gets created by the factory.

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