Which layer of the application should contain DTO implementation

后端 未结 6 814
再見小時候
再見小時候 2021-01-31 04:11

Lately I\'ve been hearing a lot about DTOs and how useful they are but I can\'t find a good example of using it in ASP.NET context.

Let\'s say I use three tier architect

相关标签:
6条回答
  • 2021-01-31 04:54

    In similar situation I used to put dto's into Core which is known to all three. So you have

         Core
           |
     ------------
     |     |    |
    DAL   BL   PL
    

    Each layer can operate with Core.Dto.Employee. Each layer also exposes Core.Dto.Employee externally in its API. But internally each layer can transform/adapt Core.Dto.Employee, e.g. you read from database EF.Employee and later convert it to Core.Dto.Employee. Transformation is contained by the layer's boundary.

    If you have several different models to represent same thing throughout the layers, for example PL wants PL.Employee and DAL operates on EF.Employee, you will end up with a mess.

    0 讨论(0)
  • 2021-01-31 04:54

    I use a project named Shared for such purposes, spesifically to share object with all layers. Regardless of the name, it should be visible by all layers.

    0 讨论(0)
  • 2021-01-31 05:03

    Have a look at https://stackoverflow.com/a/6310507/1771365 added here as I don’t have enough reputation to add comments.

    Personally I would pass entitys between your Persistence layer and your business layer. As you are using MVC chances are your are going pass view models to your controllers. At which point I would map your view model to your DTOs(s).

    If you plan to use DTO between all of your layers then create a cross cutting project which you can then reference.

    0 讨论(0)
  • 2021-01-31 05:08

    Your service layer exposes DTO's. This means that in the service layer you define data contracts as you would like them to be exposed to the outside world. In most cases they are flattened entities that not necessarily have the same structure as your database entities.

    It is the responsibility of your service layer to use business/data layer and construct the DTO's that you expose to the outside world.

    What you use in your business and data layer depends on the architecture. You could have a domain model that is mapped with code first. In that case, the service layer maps domain entities to data contracts (DTO's). If you don't have a domain model (anemic model), then you could as well just map the database directly to your DTO's.

    The ASP.NET MVC site consumes the service, and maps the DTO's it receives to dedicates view models that are then passed to the specific view.

    In addition, you may decide to also split queries from commands. This is a good approach because the DTO's that you get back as the reqult of a query are totally different than commands that you send to the service. A command only contains what's needed to execute the command and contain the business intend what you want to achieve, while a query returns a flattened model of what you need in the UI.

    Other remarks:

    • Don't expose your database entities.
    • Don't convert in business layer, as it's not business logic.
    0 讨论(0)
  • 2021-01-31 05:12

    An approach that I'm particularly fond of is to the DTO conversion in your Business Layer.

    Scenario: Your Presentation Layer calls your Business Layer passing a DTO. You do some logic & validation, then convert the DTO to an entity and send it to your Data Access Layer.

    i.e. UI --> Bus. Layer (convert to Entity) --> Data Layer

    I like this approach as I believe the Data Layer should not have any conversion logic and should receive and handle entities as needed. Another reason why this is effective is that you can now define specific business rules / validation logic during the conversion process before sending it to the Data Layer. Here's an old MSDN article, but has some great details explaining a similar approach.

    0 讨论(0)
  • 2021-01-31 05:14

    There should not be any conversion. You would just use Poco Objects as DTO.

    EF works with Poco and they can be serialized by WCF.

    They should be defined in an assembly that is refernced by all projects.

    In ASP.NET MVC you would mapp to a ViewModel by using the Poco - DTO.

    0 讨论(0)
提交回复
热议问题