Where are contracts for infrastructure services?

I have layers of interface, application, domain and infrastructure.

At my infrastructure level, take a link to the Domain and Application Layer to register the service interfaces as using Ninject.

But I need the infrastructure level service in my application level, then I need to refer to the infrastructure level in my application level.

The problem is that the infrastructure level refers to the application level, and when I refer to the infrastructure level at the application level, the following error is displayed:

Link to "Infrastructure" cannot be added. Add this project as a reference to cause circular dependency.

How do I solve this? Put the Ninject application level configuration in the application level? I think this is not true because I will have an Infrastructure implementation at my application level.

+2
source share
2 answers

Contracts for infrastructure services should be defined in the layers that consume them (domain and application), but implemented in the infrastructure. Take a look at the Dependency Inversion Principle and Onion architecture . The infrastructure level should depend on the application and domain. Your domain and application should not be infrastructure dependent. They should depend on the abstraction defined in their own terms. You may find this answer interesting. The actual implementation of this abstraction should be introduced when the application is launched in the so-called Root of Composition .

For example, in your application you can define and use an interface, for example:

ICanNotifyUserOfSuccessfullRegistration 

The infrastructure layer will refer to the application and will implement this interface using the SMTP or SMS classes:

 class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration { ... } 

Later this implementation will be introduced into the application by the DI container. The application will not be dependent on the infrastructure, but will still use it, therefore Dependecny Inversion . I recommend reading Injection Dependency in .NET , even if you use Java or other stacks.

+3
source

It looks like your layers are too tightly connected or have irregular borders. You can separate layers by introducing interfaces that live in their own project, and others can reference them.

+1
source

Source: https://habr.com/ru/post/898515/


All Articles