I am learning ASP.NET clean architecture structure. I have an application layer with a query GetAllRestaurants
which has to return paginated restaurant objects.
I am using repository pattern, with the structure as shown in this screenshot:
Now, I want to create an endpoint which takes GetRestaurantQuery
[FromBody]
, which is a class in the application layer, then passes this object to the RestaurantRepository
and returns PagedResult
object.
My problem is that the domain layer has no access to any other layers.
So in IRestaurantRepository
, I cannot define any method that uses DTOs or objects from the application layer.
This is the Handle
method from my application layer:
public async Task<PagedResult<List<RestaurantDto>>> Handle(GetRestaurantQuery request, CancellationToken cancellationToken)
{
var restaurants = await _restaurantRepository.GetAllRestaurants(request);
// etc...
}
And my IRestaurantRepository
is trying to use those DTOs:
using Domain.Entities;
namespace Domain.Repository
{
public interface IRestaurantRepository
{
Task<PagedResult<List<Restaurant>>> GetAllRestaurants(GetRestaurantQuery query); <----
Task<Restaurant> GetRestaurantById(int Id);
Task<int> CreateRestaurant(Restaurant restaurant);
}
}
which obviously is not working, because the domain layer has no access.
What is the best way to handle such situation?
I thought about:
- removing repository layer (but I still want to know how I should handle this)
- passing primitive types to
IRestaurantRepository
methods, then return primitive types as well and construct objects in application layer. But that seems wrong.
So how should I handle that? Thanks in advance.
2
Answers
The Repository interfaces need to be in the application layer and not Domain layer.
The implementation of the interfaces would be in the Infrastructure layer.
The Domain layer is only for application level objects such as ‘Entities’, ‘Constants’, ‘Application Exception’ etc..
This way, you would be able to pass Entity objects to the repo layer and have it returned Entities/ Dtos as you like.
The Dependency Rule tells us:
So you have correctly identified that we cannot use elements from the application layer (PagedResult, GetRestaurantQuery) in the domain layer.
Here’s what it might look like conceptually (pseudocode):