2

I'm developing an application that has three layers: Application, infrastructure and domain. The application layer is a WebApi, and the rest are Class Library. The infrastructure layer depends on the domain and application layer. The application layer only references the domain layer.

The App layer defines interfaces that the infrastructure layer implements.

App references Domain. Infra references Domain. Infra references App.

However, now that I'm configuring dependency injection, viaStartup class, the project that represents the application layer needs to reference the infrastructure project, which is causing a circular dependency.

Can anyone help me how to solve this?

thanks to all!

  • Here's one problem: `The App layer defines interfaces that the infrastructure layer implements` - why? Keep your interfaces in `infrastructure` - App should be responsible for wiring things up (DI container) – Alex Jun 17 '19 at 14:37
  • If a have an LoggerService used on app layer, for example, its interface and implementation should be in infra layer? –  Jun 17 '19 at 15:45
  • Yes, that would make sense – Alex Jun 17 '19 at 15:46
  • The correct aproach is likr this article? (see images) https://learn.microsoft.com/pt-br/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/ddd-oriented-microservice –  Jun 17 '19 at 18:40
  • There's no right or wrong way, it's whatever works for you and your project. Try this - https://lostechies.com/jimmybogard/2012/08/30/evolutionary-project-structure/ – Alex Jun 17 '19 at 18:52
  • 1
    Related: https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-applications-entry – Steven Jun 18 '19 at 10:11

2 Answers2

1

The App layer defines interfaces that the infrastructure layer implements.

This is the most likely cause of the problem.

If you follow the Dependency Inversion Principle, then, as APPP puts it, "clients [...] own the abstract interfaces".

What that implies for a three-layer application architecture like the one described in the OP is that interfaces should be defined in the Domain Model, not in the top application layer. Read more about layered application architecture and Dependency Injection on my blog here.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
0

Just don't define interfaces in a WebApi or any other executable project for that matter. Any class library trying to implement such an interface is forced to have a dependancy on the whole WebApi project! You really don't want that. So in this case I would seperate those interfaces into their own class library and in doing so you will break the circle. I think moving them to the Infrastructure would break the whole concept of Inversion Of Control

Robbert Draaisma
  • 463
  • 4
  • 14