Search code examples
asp.netdependency-injectiondomain-driven-designinversion-of-controlproject

Circular reference between app and infra layers .Net Core


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!


Solution

  • 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.