3

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.

Dmitry
  • 17,078
  • 2
  • 44
  • 70
Acaz Souza
  • 8,311
  • 11
  • 54
  • 97

2 Answers2

4

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:

ICanNotifyUserOfSuccessfullRegistration

The Infrastructure layer will reference Application and will implement this interface using SMTP or SMS classes:

class SmsNotificator : ICanNotifyUserOfSuccessfullRegistration { ... }

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.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • Application Layer use Infrastructure Services, right? And Infrastructure Services are in Infrastructure Layer? – Acaz Souza Sep 30 '11 at 17:29
  • Yes, but 'uses' does not mean 'depends/refernces'. See my answer. Application uses Infrastructure without depending on it - DependecyInversionPrinciple. – Dmitry Sep 30 '11 at 17:34
  • Yes, I use DI, but my contracts (Interfaces) of Infrastructure Services are in Infrastructure Layer. – Acaz Souza Sep 30 '11 at 17:39
  • See last update. Infrastructure interfaces should be defined in App or Domain (in their terms). – Dmitry Sep 30 '11 at 17:41
  • hooo, now it's all ok, I did not know infrastructure interfaces could keep in the application layer. Thanks man... – Acaz Souza Sep 30 '11 at 18:05
1

Sounds like your layers are either too tightly coupled or have the wrong boundaries. You can decouple the layers by introducing interfaces that live in their own project and can be referenced by the others.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Infrastructure layers have Ninject configuration of all layers? If Yes, my layers are right... – Acaz Souza Sep 30 '11 at 17:25
  • The need for a circular dependency suggests something else, but what do I know. I probably can't help you there. – Dennis Traub Sep 30 '11 at 17:28
  • @Dennis . Precisely.. Thats how we have gone ahead with our project structure as well. One question here though. Sorry its not much of an answer to the OPs question. But what is the difference between application and domain. My guess is that domain is where you have your domain entities and application is where you apply business logic,rules on the domain entities? – Hari Subramaniam Oct 02 '11 at 08:13