开发者

Dependency inversion - owner of interfaces?

开发者 https://www.devze.com 2023-02-03 21:26 出处:网络
I am starting with ASP.NET MVC and trying to learn DI and dependency inversion at the same time. I am setting up at MV开发者_C百科C project where the controllers and views reside in one assembly and I

I am starting with ASP.NET MVC and trying to learn DI and dependency inversion at the same time. I am setting up at MV开发者_C百科C project where the controllers and views reside in one assembly and I also have a couple of more assemblies for domain models and services that to most of the actual business logic.

The plan is to have all my services implement interfaces. The controllers that call the services access them through these interfaces. Instantiation is done using the Ninject DI framework.

Now the actual question; who "owns" the interfaces? From my understanding of dependency inversion, the service interfaces would be owned by the controllers and therefore reside in that assembly.


None of your components have to own the interfaces. Good interfaces are their own animal - they just have to be visible to both the controllers and services.

Physically segregating them creates headaches that may be unnecessary - I recommend that you do not use multiple assemblies without a good reason.


The interfaces must be visible to the implementors. The implementation may be visible to consumers. If you want to separate the implementation from interface such that they can be deployed separately, then the interfaces should reside in their own assembly.

If you are disciplined enough to organize your code based on namespaces and have no special deployment requirements, the interfaces can reside in the same assembly as the implementation.


Interfaces to services (normally provide data and implemented as WCF) need to reside on a separate DLL and MVC will have a reference to.

So here is a typical (and basic) splitting of the classes and interfaces into projects (and assemblies):

  • Common
  • Entity (reference Common)
  • Service Interface (reference Common and Entity)
  • Service Implementation (reference all above)
  • Presentation (References all above but Implementation) and has WCF procies, view specific logic
  • MVC project (references all above but Implementation)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号