asp.net-coredependency-injection.net-5.net-4.8dependency-inversion

Dependency Inversion Principle. Why can I access classes of a non-directly referenced project in ASP.NET 5?


Imagine I have four projects in my solution:

The UI projects has dependencies to the Domain and Boot projects.

The Boot project has dependencies to the Domain and Repository projects for DI container configurations.

If I write this in ASP.NET 5, the UI code can access and instantiate Repository classes, even if I didn't have a dependency in the UI project.

In .NET Framework 4.8, this did not happen. This behavior turn an isolation impossible, so Dependency Inversion Principle in this configuration is easily breakable.

There is a way to turn that behavior off in ASP.NET 5?


Solution

  • This is a default behaviour of .NET Core, if you want to change this and don't allow UI to have access to Repository layer indirectly, you can set it like below in .csproj file:

     <ProjectReference Include="..\Boot\Boot.csproj" >
      <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference>
    

    To know more about this, take a look at the below link link