2

I did Google and checked couple of tutorials. Everywhere, it is said that DTO help to hide our actual entity structure. But, I do not get it. Here is example:

public class WebSite
{
    public string Url {get;set;}
    public string Author {get;set;}

    public DateTime CreatedAt {get;set;}

}

Now Dto

   public class WebSiteDto
    {
        public string Url {get;set;}
        public string Author {get;set;}

        public DateTime CreatedAt {get;set;}

    }

Lets say, I use DTO to collect info from user. What we have protected or hidden here from hackers or what kind of encapsulation is done here? Note: I am newbie to MVC Web Api

Thanks

user3379745
  • 77
  • 1
  • 8
  • 1
    Maybe a bad example :) But we could think that the Author is actually the User that edited the page, and that user information contains sensitive data irrelevant to your frontend. So it could be handy to map the Author Dataobject to the only information we need... The Author's full name :) – Laurent Lequenne Jun 20 '17 at 11:39
  • The idea of a some displayed information could be formatted ,or calculated and could be reused a plenty times on your output, so it could be better to create an object with all information that you will need, and to map/format/calculate them only once. – Laurent Lequenne Jun 20 '17 at 11:40
  • In your particular example you don't need DTO object at all. But you will need one, if you want return object without information about created date – Fabio Jun 20 '17 at 11:42
  • 1
    DTOs is one of the biggest sheep mentality beliefs of recent years. The example you gave is one that I see everywhere for DTO. These people are yet to discover the _request/reply_ pattern where the shape of the message revolves around a _verb_ (`UpdatePatientRequest`; `SearchMuppetCharactersRequest`) rather than just a meaningless data payload with no incidication as to what should be done. The latter just leads to confusing classes with `Dto` suffixes. http://www.enterpriseintegrationpatterns.com; http://www.soapatterns.org/ Check out _canonical data model_ –  Jun 20 '17 at 11:58

0 Answers0