locked
Should DAL return Entity or Data Transfer Object? RRS feed

  • Question

  • User-863064074 posted

    What should return Data Access Layer - Entity or Data Transfer Object?

    Tuesday, May 2, 2017 5:07 PM

Answers

  • User-821857111 posted

    in that case should I create DTO?
    Yes - you can create a specialised class to encapsulate the various properties that you need. You can call it whatever you like: DTO, ViewModel, Projection. They all pretty much amount to the same thing. If you are working on your own, you can please yourself in terms of what label you give the class. If you are working as part of a team, you should agree on the terms that you use to describe the specialised class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 3, 2017 10:33 AM
  • User765422875 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 3, 2017 1:13 PM

All replies

  • User765422875 posted

    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.

    Tuesday, May 2, 2017 9:38 PM
  • User-863064074 posted

    What if I have that query in DAL:

    _dbContext.Order.Select(x =>
    new 
    {
    Order= x, 
    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?

    Wednesday, May 3, 2017 8:36 AM
  • User-821857111 posted

    in that case should I create DTO?
    Yes - you can create a specialised class to encapsulate the various properties that you need. You can call it whatever you like: DTO, ViewModel, Projection. They all pretty much amount to the same thing. If you are working on your own, you can please yourself in terms of what label you give the class. If you are working as part of a team, you should agree on the terms that you use to describe the specialised class.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 3, 2017 10:33 AM
  • User765422875 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 3, 2017 1:13 PM