• Plain Old CLR Object vs Data Transfer Object


    Plain Old CLR Object vs Data Transfer Object

    POCO = Plain Old CLR (or better: Class) Object

    DTO = Data Transfer Object

    In this post there is a difference, but frankly most of the blogs I read describe POCO in the way DTO is defined: DTOs are simple data containers used for moving data between the layers of an application.

    Are POCO and DTO the same thing?

    回答1

    A POCO follows the rules of OOP. It should (but doesn't have to) have state and behavior. POCO comes from POJO, coined by Martin Fowler [anecdote here]. He used the term POJO as a way to make it more sexy to reject the framework heavy EJB implementations. POCO should be used in the same context in .Net. Don't let frameworks dictate your object's design.

    A DTO's only purpose is to transfer state, and should have no behavior. See Martin Fowler's explanation of a DTO for an example of the use of this pattern.

    Here's the difference: POCO describes an approach to programming (good old fashioned object oriented programming), where DTO is a pattern that is used to "transfer data" using objects.

    While you can treat POCOs like DTOs, you run the risk of creating an anemic domain model if you do so. Additionally, there's a mismatch in structure, since DTOs should be designed to transfer data, not to represent the true structure of the business domain. The result of this is that DTOs tend to be more flat than your actual domain.

    In a domain of any reasonable complexity, you're almost always better off creating separate domain POCOs and translating them to DTOs. DDD (domain driven design) defines the anti-corruption layer (another link here, but best thing to do is buy the book), which is a good structure that makes the segregation clear.

    回答2

    It's probably redundant for me to contribute since I already stated my position in my blog article, but the final paragraph of that article kind of sums things up:

    So, in conclusion, learn to love the POCO, and make sure you don’t spread any misinformation about it being the same thing as a DTO. DTOs are simple data containers used for moving data between the layers of an application. POCOs are full fledged business objects with the one requirement that they are Persistence Ignorant (no get or save methods). Lastly, if you haven’t checked out Jimmy Nilsson’s book yet, pick it up from your local university stacks. It has examples in C# and it’s a great read.

    BTW, Patrick I read the POCO as a Lifestyle article, and I completely agree, that is a fantastic article. It's actually a section from the Jimmy Nilsson book that I recommended. I had no idea that it was available online. His book really is the best source of information I've found on POCO / DTO / Repository / and other DDD development practices.

    回答3

    DTO is a good practice to minimise the amount of data you transfer over the wire to only include relevant fields. This is amongst other benefits. Checkout Automapper and the ProjectTo method which can automatically convert DAL to DTO and vice versa. ProjectTo under the bonnet will only select columns included in the configured mapping.

    https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions

  • 相关阅读:
    CRM 2016 刷新 Iframe
    CRM 2016 升级CRM365之注意事项
    CRM 插件导出
    [LeetCode]Valid Sudoku
    [iOS]转:iOS最佳实践
    [LeetCode]Permutation Sequence
    [C/C++]函数可变参数表
    [LeetCode]Next Permutation
    [LeetCode]Remove Element
    [LeetCode]4Sum
  • 原文地址:https://www.cnblogs.com/chucklu/p/16594929.html
Copyright © 2020-2023  润新知