Dependency Injection - Does it violate Separation of Concerns?

我的梦境 提交于 2019-12-05 18:00:52

In general, you're correct. However, Dependency Injection tends to be thought of as "configuration" rather than part of the Presentation Layer (even though it often typically does live there). If your UI components are designed to not know about the data layer, then this is what really matters.

If you are designing a testable system, then the business and data layers should be independent of the UI, but something has to configure them. You could create yet another layer, called MyApp.Configuration that does all this, but most people find this to be over-engineering.

The important thing is whether your components are well designed, not whether the UI has some configuration knowledge of the other layers.

It's really no different from having app settings in your Web.Config. After all, unless your using a service oriented architecture, everything is running on the same computer in the same process anyways. And if you are using an SoA, then you would configure the individual parts on their respective servers.

No, it does not violate the SoC, in fact it encourages it.

The problem is that in your example you are not using DI, at least in the UI. You are letting the WebForm explicitly build the objects it needs, instead of getting those dependencies injected.

The main idea is to have a central bootstrapper in an app that creates the root object, which gets it dependencies which in turn are constructed from other dependencies and so on. Given that is such a pain to do manually, you rely on DI containers that do that automatically using conventions or via configuration, or both.

So in your example, you would have the WebForm be constructed using a DI container and you would specify dependencies to, say, an IBusinessObject that would in turn depend on the DataContext to do things with those entities and create them from dto's. Then in that Save method you would use that by using the instance that was injected from outside, either manually or via a container, but always in some root point in the app lifecycle

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