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

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

问题描述

我很抱歉我是企业应用程序的新手以及设计模式。可能是这个问题,缺乏关于设计模式的知识。我发现使用DTO来更好地传输数据。



我的业务实体类如下:

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

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

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

所以基于这些信息进入Patient对象。 (使用DTO模式)



所以我想要写新的类来转换如下。

  public class PatientDO 
{
public static患者转换功能(PatientRegistrationDTO pregDTO,PersonDTO person)
{
患者p =新患者();
p.NIC = pregDTO.NIC;
p.FullName = person.FullName;
p.FirstName = person.FirstName;
return p;
}
}

但最近我读了几个文章,他们使用 Serializer Helper类以及 XmlSerializer 我不明白为什么他们使用这样的东西。



对于DTO模式是需要使用XmlSerializer和为什么使用它?

解决方案

你应该看看AutoMapper。



http ://automapper.org



这是一个可以包含在您的解决方案中的软件,它将自动将值从一个类映射到另一个类。 / p>

它将自动映射具有相同名称的属性,并且在涉及到子对象时也很聪明。但是,当您需要时,它还提供完整的地图控制。



编辑



几个例子来展示AutoMapper如何工作。请注意,我从来不会在现实生活中代码。简单!



示例类。

  //常见方案。具有到DB的连接的实体类。 
命名空间实体
{
public class Manager
{
public virtual int Id {get;组; }
public virtual User User {get;组; }
public virtual IList< User>厨师{get;组;
}

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



//模型类 - 更扁平化
命名空间模型
{
public class Manager
{
public int Id {get;组; }
public string UserFirstname {get;组; }
public string UserLastname {get;组; }
public string UserMiddlename {get;组; }
}
}

通常,你会有一部分项目配置您的所有AutoMapping。通过我刚刚给出的例子,您可以在Entities.Manager和Models.Manager之间配置一个地图,如下所示: -

  //告诉AutoMapper这个转换是可能的
Mapper.CreateMap< Entities.Manager,Models.Manager>();

然后,在你的代码中,你可以使用这样的东西来获取一个新的Models.Manager对象从实体版本。

  //映射类
var mgr = Map< Entities.Manager,Models.Manager> ;
(repoManager,新Models.Manager());

顺便说一句,如果你一贯地命名事情,AM就会自动解决很多属性​​。 p>

上面的示例应该自动填充UserFirstname和UserLastname,因为: -




  • 一个名为User

  • 的属性用户具有名为Firstname和Lastname
  • 的属性


但是,UserMiddlename属性在Models.Manager和Entity.Manager和Models.Manager之间的映射操作之后,Models.Manager将始终为空白,因为用户没有名为Middlename的公共属性。


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?

解决方案

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.

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

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