I have UI, Application, Domain and Infrastructure Layers.
In my Infrastructure Layer take reference of Domain and Application Layer to register services interfaces of both using Ninject.
But I need in my Application Layer a service in Infrastructure Layer, then i need to reference the Infrastructure Layer in my Application Layer.
The problem is Infrastructure Layer take reference to Application Layer and when I’ll reference Infrastructure Layer in Application Layer the following error is show:
A reference to ‘Infrastructure’ could not be added. Addind this project as a reference would cause a circular dependency.
How I solve this? Put the Ninject Configuration of Application Layer in the Application Layer? I think this is not correct, because I’ll have Infrastructure implementation in my Application Layer.
Infrastructure services contracts should be defined in the layers that consume them (Domain and Application), but implemented in Infrastructure. Take a look at Dependency Inversion Principle and Onion Architecture. Infrastructure layer should depend on App and Domain. Your Domain and App should not depend on Infrastructure. They should depend on abstraction defined in their own terms. You may find this answer interesting. The actual implementation of this abstraction should be injected at the application startup in a so called Composition Root.
For example in your Application you can define and interface like:
The Infrastructure layer will reference Application and will implement this interface using SMTP or SMS classes:
Later on this implementation will be injected into Application by DI container. Application will not have a dependency on Infrastructure but will still use it, hence Dependecny Inversion. I recommend reading Dependency Injection in .NET, even if you use Java or other stacks.