What is the difference between the use of ViewModel and DTO classes?



  • Let's say we have the next class.

    public class Request
    {
        public int Id {get;set;}
        public int RequestTypeId {get;set;}
        public DateTime OpeningDate {get;set;}
        public DateTime? ClosingDate {get;set;}
        public IEnumerable<RequestHistory> History {get;set;}
    }
    

    public class RequestHistory
    {
    public int Id {get;set;}
    public int RequestId {get;set;}
    public int RequestStateId {get;set;}
    public string Note {get;set;}
    public int AuthorId {get;set;}
    public DateTime Timestamp { get;set;}
    }

    I've got a lot to do with the business object. Other Classes:

    public class CreateRequest
    {
    public int RequestTypeId {get;set;}
    public DateTime OpeningDate {get;set;}
    public string Note {get;set;}
    public int AuthorId {get;set;}
    }

    public class EditRequest
    {
    public int Id {get;set;}
    public int RequestStateId {get;set;}
    public string Note {get;set;}
    public int AuthorId {get;set;}
    }

    Somewhere. service layer I'm changing classes. CreateRequestEditRequest Total Request and RequestHistory

    In the annex, default classes are available prefix ViewModelas well, these classes are equipped with validation attributes (Obligation, Minimum/maximum length, etc.).

    There is a so-called design template. https://ru.wikipedia.org/wiki/DTO :

    is used to transmit data between the annex subsystems. Data Transfer Object, unlike business object or data access object, should not contain any conduct.

    Please explain for what and in what cases a method should be used.



  • Difference in names. I'm bigger. WPF I've seen it, so it's an example.

    I'm transferring anything from the server to the client. That's it. DTOit is intended for transmission. On the client, I'm formatting, combining, subscribing. Got it. ViewModel

    For example, a list of letters came from the server. The letters are obvious. DTO♪ And then on the client, I'm already taking notes from the letters, showing signs of importance, headings, investment, that's just routine. ViewModel♪ Of course, you can download this server and make it form a display model - it's easier for the client. But that's how you limit the client's ability and support to other clients right away.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2