C# – Data transfer object pattern

cdesign-patternsdtonet

i'm sorry i'm newbie to enterprise application as well as the design pattern. might be this question occcur lack of knowledge about design pattern. i found that its better to use DTO to transfer data.

my business entity class as below:

public class Patient
{    
    public string ID { get; set; }
    public string FullName { get; set; }
    public string FirstName { get; set; }
    public string Surname { get; set; }
}

so in my application user only give ID and HospitalID. so it calls for another web service and get person information

 public class PersonDTO
 {
     public string NIC { get; set; }
     public string FullName { get; set; }
     public string FirstName { get; set; }
     public string BirthPlace { get; set; }
     public string BirthCertificateID { get; set; }
 }

so based on these information im going to Patient object. (Using DTO pattern)

so i thought of write new class to convert this as follows.

public class PatientDO
{
    public static Patient ConvertToEntity(
        PatientRegistrationDTO pregDTO,
        PersonDTO person
    )
    {
        Patient p = new Patient();
        p.NIC = pregDTO.NIC;
        p.FullName = person.FullName;
        p.FirstName = person.FirstName;
        return p;
    }
}

but lately i read few articles and they used Serializer Helper class as well as the XmlSerializer i can't understand why they used somthing like that.

for the DTO pattern is that need to use XmlSerializer and why it is used?

Best Answer

You should really take a look at AutoMapper.

http://automapper.org

This is a piece of software that you can include in your solution that will automatically map values from one class to another.

It'll map properties with the same name automatically, and is also pretty smart when it comes to child objects. However, it also offers complete mapping control when you need it.

EDIT

Couple of examples to show how AutoMapper works. Please note I'd never code like this in real life. Brevity!

Example classes.

// Common scenario.  Entity classes that have a connection to the DB.
namespace Entities 
{
   public class Manager
   {
      public virtual int Id { get; set; }
      public virtual User User { get; set; }
      public virtual IList<User> Serfs { get; set; }
   }

   public class User
   {
      public virtual int Id { get; set; }
      public virtual string Firstname { get; set; }
      public virtual string Lastname { get; set; }
   }
}



// Model class - bit more flattened
namespace Models 
{
   public class Manager 
   {
      public int Id { get; set; }
      public string UserFirstname { get; set; }
      public string UserLastname { get; set; }
      public string UserMiddlename { get; set; }
   }
}

Typically, you'd have a part of your project to configure all your AutoMapping. With the examples I've just given, you can configure a map between Entities.Manager and Models.Manager like so:-

// Tell AutoMapper that this conversion is possible
Mapper.CreateMap<Entities.Manager, Models.Manager>();

Then, in your code, you'd use something like this to get a new Models.Manager object from the Entity version.

// Map the class
var mgr = Map<Entities.Manager, Models.Manager>
  ( repoManager, new Models.Manager() );

Incidentally, AM is smart enough to resolve a lot of properties automatically if you name things consistently.

Example above, UserFirstname and UserLastname should be automatically populated because:-

  • Manager has a property called User
  • User has properties called Firstname and Lastname

However, the UserMiddlename property in Models.Manager will always be blank after a mapping op between Entities.Manager and Models.Manager, because User does not have a public property called Middlename.