skip to Main Content

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:

my project structure - underlined files i am asking about

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


  1. 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.

    Login or Signup to reply.
  2. The Dependency Rule tells us:

    Source code dependencies can only point inwards

    So you have correctly identified that we cannot use elements from the application layer (PagedResult, GetRestaurantQuery) in the domain layer.

    1. To solve this problem, the missing elements should be identified at the domain level and used at the application level.
    2. From a clean architecture perspective, the application layer should not depend on the "delivery channel", so you still need to create a web layer.

    Here’s what it might look like conceptually (pseudocode):

    // Domain layer
    public interface IRestaurantRepository
    {
       List<IRestaurant> GetRestaurants(IRestaurantFilter filter);
       // etc., IRestaurant and IRestaurantFilter at the domain layer
    }
    
    // Application layer
    public class GetRestaurantsUseCase : IGetRestaurantsUseCase
    {
       private IRestaurantRepository iRestaurantRepository;
       // etc.
       
       List<IUseCaseRestaurant> GetRestaurants(IUseCaseRestaurantFilter useCaseFilter)
       {
           IRestaurantFilter filter = new RestaurantFilterImpl(useCaseFilter);
           List<IRestaurant> restuarants = iRestaurantRepository.GetRestaurants(filter);
           // mapping List<IRestaurant> to List<IUseCaseRestaurant> and return it
           // IUseCaseRestaurant, IUseCaseRestaurantFilter, RestaurantFilterImpl at the application alyer
       }
    }
    
    // Web layer
    public class RestuarantsResource
    {
       private IGetRestaurantsUseCase iGetRestaurantsUseCase;
       // etc.
       
       HttpResponse get(HttpRequest request)
       {
           IUseCaseRestaurantFilter useCaseFilter = new UseCaseRestaurantFilterImpl(request);
           List<IUseCaseRestaurant> restaurants = iGetRestaurantsUseCase.GetRestaurants(useCaseFilter);
           // mapping restaurants to HttpResponse and return it
           // UseCaseRestaurantFilterImpl at the web layer
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search