数据传输对象模式 [英] Data transfer object pattern

查看:320
本文介绍了数据传输对象模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,我是新手,企业应用以及设计模式。可能是这个问题occcur缺乏了解的设计模式。我发现,它能够更好地使用DTO来传输数据。

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.

如下我的业务实体类:

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

所以在我的应用程序用户只给出ID和HospitalID。所以它要求另一个Web服务并获取个人信息

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; }
 }

所以根据这些信息,我去耐心的对象。 (使用DTO模式)

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;
        }
    }

但最近我读他们用几文章串行Helper类还有的XmlSerializer 我不明白为什么他们使用财产以后这样的。

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.

对于DTO模式是需要使用XmlSerializer的,以及为什么使用它?

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

推荐答案

您真的应该看看AutoMapper。

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.

它会自动映射特性具有相同的名称,并且还pretty的聪明,当涉及到子对象。然而,它也提供了你需要的时候完全映射控制。

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.

修改

几个例子表明AutoMapper是如何工作的。请注意,我从来没有code这样的在现实生活中。简洁!

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

例类。

// 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; }
   }
}

通常情况下,你有你的项目的一部分来配置所有的自动映射。随着我刚给出的例子,你可以配置Entities.Manager和Models.Manager之间的映射,如下所示: -

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>();

然后,在你的code,你会用这样的事情,从实体版本,得到一个新的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() );

顺便说一句,AM是足够聪明的,如果你一直事物命名自动解决很多的属性。

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

上面的例子,USERFIRSTNAME和USERLASTNAME应自动填充,因为: -

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

  • 管理器有一个属性称为用户
  • 用户具有属性叫名字和姓氏

然而,在Models.Manager的UserMiddlename属性将永远是Entities.Manager和Models.Manager之间的映射运算后的空白,因为用户没有叫Middlename一个公共属性。

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.

这篇关于数据传输对象模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆