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?
DTO stands for Data Transfer Object, which is a design pattern. It is one of the EPA patterns which we call when we need to use such objects that encapsulate and aggregate data for transfer. A DTO is similar to a data structure, but like a data structure, it doesn't contain any business logic.
Transfer Object is a simple POJO class having getter/setter methods and is serialized so that it can be transferred over the network. Server Side business class normally fetches data from the database and fills the POJO and sends it to the client or passes it by value. For clients, the transfer object is read-only.
DTO — Data Transfer Object. DAO — Data Access Object.
In the field of programming a data transfer object (DTO) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation.
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:-
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With