Help needed in Data transfer object practice RRS feed

  • Question

  • User1489758560 posted


    I am designing API which will be consumed by mobile app and website. when i create the class with properties, should i use mixed data types else all as stings. below are the two example

    public class PersonDto
        public string Id { get; set; }
        public string Name { get; set; }
    public class PersonDto
        public int Id { get; set; }
        public string Name { get; set; }

    which one is best. please guide me .

    Sunday, September 2, 2018 1:13 PM

All replies

  • User475983607 posted
    Your question is not clear. Both classes are exactly the same.
    Sunday, September 2, 2018 3:36 PM
  • User1489758560 posted

    thank you for the reply and it's not about the class. it's about the properties inside the class.  on the first one  both are string and on the second one mixed data type. Id is int and name is string. so when we create  DTO is it good having all the property a string? what is the universal standard. please suggest me

    Sunday, September 2, 2018 3:54 PM
  • User1120430333 posted

    The DTO should have primitive data type properties  that match the primitive data types they were derived from, such a Int to Int, Double to Double, String to String, etc. and etc.  It makes no sense to make something Sting when it derived from Int only to have to convert it from String back to Int in order to use it. 

        public class DtoProject
            public int ProjectId { get; set; }
            public string ClientName { get; set; }
            public string ProjectName { get; set; }
            public string Technology { get; set; }
            public string ProjectType { get; set; }
            public string UserId { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
            public decimal Cost { get; set; }

    Sunday, September 2, 2018 3:55 PM
  • User-474980206 posted

    you should use the C# data types, so the class is easier to use from from C#. when converted to json, there are only 

    1) arrays: []
    2) objects: {}
    3) string: ""
    4) Number: 124.1 
    5) null: null

    note: you need to take care with dates. there is no JSON standard for dates. typically they are serialized to an ISO date string. how they are parsed depends on the library used.

    Sunday, September 2, 2018 6:12 PM