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.
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:-
Then, in your code, you'd use something like this to get a new Models.Manager object from the Entity version.
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:-
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.