Three-layer architecture: Should the data layer depend on the domain layer, or vice versa?

Are both options correct? Is one of them better than the other? In what situations? Is it more difficult to achieve the other?

+2


a source to share


2 answers


Ideally, neither of them should depend on the other, but practically the domain will depend on the data layer, but hopefully indirectly.

What does it mean?

The data layer should not be dependent on the domain level.



The domain layer will most likely use data objects, but ideally you will do so through dependency injection. One way to do this is to use interfaces and only refer to the interfaces at compile time. At run time, provide concrete implementation for these interfaces through an IoC container such as Structure Map or Unity.

It will also help you unit test your solution, as well as provide separation of concerns and create a loosely coupled system.

+1


a source


Usually the domain layer (business logic) should depend on the data layer, but not vice versa.

Rationale:



  • You want to maintain the flexibility to change your business logic without any unnecessary impact on the data.
  • The data layer will be much simpler and less error prone if you can design it independently of the business logic decisions.
  • The domain layer is usually the caller of the data layer, so there is no need to set the dependency in the opposite direction.

This is actually quite analogous to how the domain layer should have minimal dependencies on the presentation layer (although in practice this can be difficult to achieve).

0


a source







All Articles