I am learning how to implement onion architecture using .Net core API. I understand that the focus of Onion architecture is more on flow of dependencies rather on how the layers interact with each other. But I found that domain entities are still accessible in the presentation layer (API layer). This is because the presentation layer has a project dependency on the application layer and the application layer depends on the domain layer where domain entities are defined as public. We cannot define it as internal because the entities will not be accessible by the application layer. This way anyone in the team mistakenly access the domain entities instead of the corresponding DTO.
One solution proposed on Udemy Q&A that we can define domain entities as internal and we can specify the assemblies that can access these internal entities by using the below build configuration
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>NameOfProjectToGainAccess</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
But this is causing error in the MyApplicationDbContext file in the Infrastructure project. The compiler flags error in the below code
public DbSet<MyEntity> MyEntity {get; set;}
It says that the DbSet is less accessible than the public property MyEntity.
I get the similar error in the repository contract definition as base interface is less accessible.
public interface IMyEntityRepository : IGenericRepository<MyEntity>
I want domain entities should not be accessible in the presentation/API layer. This layer should only access DTOs.
Please advise if there is way to do this.