I am wondering if I have overdone it with repositories in the following code, which is to save a sales order.
I understand that the purpose of a repository is to decouple the domain layer from the persistence layer, but this code seems to be replicating what EF does anyway.
public SalesOrder FindSalesOrder(int id)
{
using (var uow = new UnitOfWork<LogContext>())
{
using (var repository = new SalesOrderRepository(uow))
{
var SalesOrder = repository.Find(s => s.Id == id, s => s.Site, s => s.User);
if (SalesOrder != null)
{
using (var repository2 = new OrderLineRepository(uow))
{
var OrderLines = repository2.GetList(o => o.SalesOrderId == id);
foreach (var OrderLine in OrderLines)
{
OrderLine.SalesOrder = SalesOrder;
SalesOrder.OrderLines.Add(OrderLine);
using (var repository3 = new LineDetailRepository(uow))
{
var LineDetails = repository3.GetList(v => v.OrderLineId == OrderLine.Id, v => v.PropertyName);
foreach(var LineDetail in LineDetails)
{
LineDetail.OrderLine = OrderLine;
OrderLine.LineDetails.Add(LineDetail);
}
}
}
}
}
return SalesOrder;
}
}
}
[Update] I had a chat to my co-developer and think that perhaps the code is OK.
Our reasoning is that EF itself is rather complicated so we want to have a wrapper around what we allow the UI layer to use.
We did realize that we don't need the lineDetail repository as there are no instances where we need to access the lineDetails separate from the orderLines. That means we could make the orderLines return containing the lineDetails pre-populated.
However that would then mean some of our repository's Find methods return entities with children and some don't. Thus I am thinking maybe we need a FindGraph method in our repositories which will return the full graph.
However now I wonder whether the FindSalesOrder method should be in the SalesOrder repository itself instead of the UI layer?