0

Currently setting up a new Web Api using C# and dotnet Core. And i notice some strange behaviour when referencing other projects.

My solutions is pretty straight forward: DataAccess, BusinessLogic, WebApi

The WebApi project has a reference to the BusinessLogic project and the BusinessLogic project has a reference to the DataAccess project.

Now with previous versions of .NET my WebApi project would not have access to anything from the DataAccess project but in my current setup i can access anything form the DataAccess project in my WebApi project without having a reference to it.

Could anyone please explain this behaviour and possibly how to prevent it?

Shikyo
  • 1,430
  • 1
  • 14
  • 25

2 Answers2

2

That is how you are able to get all AspNetCore packages through only referencing Microsoft.AspNetCore.All.

Microsoft docs mention Metapackaging in details.

Also, note that since the composition root of your application is the WebAPI, you would want it to see the types from the DataAccess so you can register them with the container.

One way to go about that is to have on your BusinessLogic also the abstraction for persistence. And invert the reference (high level module should not depend on low level module) of the BusinessLogic and DataAccess.

Something like:

BusinessLogic:

  • User.cs
  • IUserRepository.cs

DataAccess:

  • UserRepository : IUserRepository

Now WebAPI project can reference DataAccess and BusinessLogic and register with the container:

Register<IUserRepository, UserRepository>();

You could go further and have a DataAccess.Abstractions just for the interfaces but that will depend on your project and how far you want to go.

Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38
  • I think having repositories and entities or DAL related interfaces in the business logic is suboptimal. Though IoC is nice I'd rather have proper layer separation. It's always possible to create separate IoC containers in the BLL and DAL. Perhaps I am thinking in an old-fashioned way. – Shikyo Sep 14 '17 at 19:36
0

Found a way to prevent this behaviour in this thread: Disable transitive project reference in .NET Standard 2

When editing the project file the PrivateAssets attribute can be set to All to prevent this behaviour.

  <ItemGroup>
    <ProjectReference Include="..\DataAccess\1-DataAccess.csproj" PrivateAssets="All" />
  </ItemGroup>
Shikyo
  • 1,430
  • 1
  • 14
  • 25