3

These are my project layers:

.NET Core 2.0 Web Project: UILayer
.NET Standard Class library : BusinessLayer
.NET Standard Class library : DataAccessLayer

I add the reference of BusinessLayer to UILayer and the reference of DataAccessLayer to BusinessLayer

So I expected the layers just had access to the those dlls, that are referenced. But now I see the UILayer has access to DataAccessLayer! Why? And how could I prevent that? (I mean physically not just logically)

It seems it is new in .NET Core I can't see the same functionality with .NET Framework 4.6.1 class libraries. So another question is that why it is added in ASP.NET Core projects? Is there any advantages? If I want to access the project simply I could add the reference.

Saeid
  • 13,224
  • 32
  • 107
  • 173
  • 3
    This is how it's always been. Since UI depends on Business which depends on Data Access, it has the ability to call logic in Data Access. This really shouldn't be an issue. Just code to interfaces instead of implementations and configure your dependency injection accordingly. And if you're not using DI, it's time to start. – mason Jan 18 '18 at 20:58
  • You can also drop most of your access modifiers in DataAccessLayer to internal, then use the `InternalsVisibleTo` assembly attribute to limit which outside assemblies can access it's members. Not the best solution, but it's an option. – Jonathon Chase Jan 18 '18 at 21:04
  • @mason but it was not like this way in .NEt Framework projects before – Saeid Jan 18 '18 at 21:08
  • 1
    @Saeid - When using DI this is the way it has always been. See [Ioc/DI - Why do I have to reference all layers/assemblies in entry application?](https://stackoverflow.com/a/9503612/). – NightOwl888 Jan 19 '18 at 02:25

1 Answers1

4

You can specify that inside the .csproj of your BusinessLayer project, use the <PrivateAssets> element inside the <ProjectReference> to declare the assets that you do not want to be visible for parent projects. Here you have additional info for this element

<ProjectReference Include="{path-of-your-referenced-DataAccessLayer}">
  <PrivateAssets>All</PrivateAssets>
</ProjectReference>
animalito maquina
  • 2,264
  • 3
  • 20
  • 35