I have a solution that I have implemented by Onion architecture and DDD like eShopOnContainers.
But I have an issue to do this, so I decided share it with you. I try to explain it by an example. You suppose I have a interface that named IOrderRepository like IOrderRepository
public interface IOrderRepository : IRepository<Order>
{
Order Add(Order order); // first issue
Task<OrderDTO> GetAsync(int orderId); // second issue
}
First issue
I implemented Add method like OrderRepository but I need extra parameters in Add method like following code:
public Order Add(Order order, int par1, int par2)
{
// I need a DTO to persist order
OrderDTO orderDTO = new OrderDTO({Order = order, Par1 = par1, Par2 = par2 });
// Call a method with orderDTO parameter as a service to insert order
}
As you can see, implementing IOrderRepository is wrong because of extra parameters that I need.
Wrong solutions for first issue
I have two wrong solutions to tackle with this issue.
1- Adjusting IOrderRepository
Changing input IOrderRepositoryparameters by adding the parameters like this:
public interface IOrderRepository : IRepository<Order>
{
Order Add(Order order, int par1, int par2);
}
As I know, there isn't any business rule for par1 and par2 and to do implement DDD, first of all I should specify IRepository, but by using this solution, I put Infrastructure layer concerns in Domain layer that is wrong architecture.
2- Take IOrderRepository to Infrastructure layer
I can put IOrderRepository in Infrastructure layer, but it's another wrong architecture because as far as I know this sort of interfaces should be located in Domain layer.
My first question
1- How I can use extra parameters in methods of a Repository in Infrastructure layer to implement IRepository of Domain layer that there isn't any connection between the parameters and Domain layer?
Second issue
As you can see in IOrderRepository, I should implement GetAsync method that returns OrderDTO including Order and extra parameters. As I know, I can't use DTO (Data Transfer Object) in Domain layer. I couldn't come up with an idea to handle it.
My second question
2- How I can return DTO
OrderRepositorymethods that is in infrastructure layer, but I can't apply it inIOrderRepositorythat is in Domain layer.
Thanks in advance for your time.
Since we have no context as to why you need those two parameters you could also go with two other, probably less intrusive, options:-
Firtly, you could have a new class that inherits from
Orderand also contains those extra parameters:In your repository you could then cast, or safe-cast, to the
MyOrderclass.Another option would be to use an extension method for
IOrderRepository:Perhaps these would not work in your scenario so YMMV. In either case you may need to take another look at your design.