Last post May 03, 2017 01:13 PM by deepalgorithm
May 02, 2017 05:07 PM|ptysiek|LINK
What should return Data Access Layer - Entity or Data Transfer Object?
May 02, 2017 09:38 PM|deepalgorithm|LINK
That depends on your use cases / requirements. The only reason I can think of returning a Data Transfer Object (DTO) instead of a full entity is if your entity is very big and you only need to work on a subset of it.
If this is the case, then perhaps you should reconsider your domain model and split your big entity into related smaller entities.
That being said, I prefer to return entities from my DAL (repositories). A repository is responsible for turning persisted data back to entities (models) and vice versa. A model is a business object representing a business entity. A DTO on the
other hand - may look like a model- is concerned with the transfer of data across boundaries and is basically a transient object. Usually mappers are responsible for turning models into DTOs.
May 03, 2017 08:36 AM|ptysiek|LINK
What if I have that query in DAL:
OrderDetailsCount = x.OrderDetails.Count()
That query gives me one query to database, otherwise I will need to have each query per Order for OrderDetails count so I can't return anonymous type from repository - in that case should I create DTO?
May 03, 2017 10:33 AM|Mikesdotnetting|LINK
in that case should I create DTO?
May 03, 2017 01:13 PM|deepalgorithm|LINK
Yeah you will have to create a new class (projection) to wrap the various properties that you need.
However, looking at the query it seems you want all Orders and count of OrderDetails. You could just return List<Order> and
include OrderDetails. Either approach will work.