阿不

潜水

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

不断有朋友希望能提供一些示例,同时我也发现,单纯发一些文字性的教程。大家好像都没有看明白NBear.Mapping是干嘛用的,所以从这篇开始我会陆续以实例的方式介绍NBear.Mapping的使用,以便给大家直观的印象。这篇我们首先关注的是对象与对象间映射,在这篇文章里你将会看到。即使你的项目不需要与数据库的映射,NBear.Mapping在日常开发中也会给你带来非常大的帮助。

就如之前教程介绍的那样,有一个IUser接口,这个接口定义了用户的一些基本属性:

   1: public enum UserStatus
   2: {
   3:     Normal,
   4:     Admin
   5: }
   6: public interface IUser
   7: {
   8:     int? ID { get;set;}
   9:     string Name { get;set;}
  10:     string Address { get;set;}
  11:     int Age { get;set;}
  12:     UserStatus Status { get;set;}
  13: }

我们用一个类UserObject来实现它,这个类型除了实现IUser接口外,还有一个自定义的属性Password:

   1: public class UserObject : IUser
   2:     {
   3:         #region IUser Members
   4:         private string name;
   5:         public string Name
   6:         {
   7:             get
   8:             {
   9:                 return name;
  10:             }
  11:             set
  12:             {
  13:                 name = value;
  14:             }
  15:         }
  16:  
  17:         private int? id;
  18:         public int? ID
  19:         {
  20:             get
  21:             {
  22:                 return id;
  23:             }
  24:             set
  25:             {
  26:                 id = value;
  27:             }
  28:         }
  29:         string address;
  30:         public string Address
  31:         {
  32:             get
  33:             {
  34:                 return address;
  35:             }
  36:             set
  37:             {
  38:                 address = value;
  39:             }
  40:         }
  41:         int age;
  42:         public int Age
  43:         {
  44:             get
  45:             {
  46:                 return age;
  47:             }
  48:             set
  49:             {
  50:                 age = value;
  51:             }
  52:         }
  53:  
  54:         UserStatus status;
  55:         public UserStatus Status
  56:         {
  57:             get
  58:             {
  59:                 return status;
  60:             }
  61:             set
  62:             {
  63:                 status = value;
  64:             }
  65:         }
  66:  
  67:         #endregion
  68:  
  69:         private string password;
  70:  
  71:         public string Password
  72:         {
  73:             get { return password; }
  74:             set { password = value; }
  75:         }
  76:  
  77:         public UserObject() { }
  78:  
  79:         public UserObject(int? id, string name, string address, int age, UserStatus status, string password)
  80:         {
  81:             this.id = id;
  82:             this.name = name;
  83:             this.address = address;
  84:             this.age = age;
  85:             this.status = status;
  86:             this.password = password;
  87:         }
  88:     }

这时,我们希望将一个UserObject实例的属性拷贝到另一个UserObject实例中,那么我们可能会使用如下的代码:

   1: UserObject user = new UserObject(1, "abu", "fuzhou", 24, UserStatus.Admin,"*&3345-+¥");
   2: UserObject userObject = new UserObject();
   3: userObject.Address = user.Address;
   4: userObject.Age = user.Age;
   5: userObject.ID = user.ID;
   6: userObject.Name = user.Name;
   7: userObject.Password = user.Password;
   8: userObject.Status = user.Status;

当然,如果单纯的都是这样的属性拷贝的话,那完全可以通过实现ICloneable接口,来实现对象的浅拷贝。但是如果我们使用NBear.Mapping的话,那我们的代码将会变成这样:

   1: UserObject user = new UserObject(1, "abu", "fuzhou", 24, UserStatus.Admin,"*&3345-+¥");
   2: UserObject outputObject = ObjectConvertor.ToObject<UserObject>(user);

你会发现你的对象转换代码将会变的非常干净简单。此时,在保证系统安全的情况下,我并不希望Password字段参与拷贝工作,那么我们有几下几种做法:

1、修改代码,使用ObjectConvertor.ToObject<InputType,OutputType>(user)的形式,如下:

   1: UserObject outputObject = ObjectConvertor.ToObject<IUser,UserObject>(user);

这时,参与对象拷贝的就只有IUser接口里所定义的属性字段了。

2、不改变原有代码,在配置文件中增加这样的一段配置,就可以让过滤掉Password的拷贝:

   1: <object inputType="UserObject" outputType="UserObject">
   2:   <views>
   3:     <view name="Default">
   4:       <ignoreProperties>
   5:         <add destName="Password"></add>
   6:       </ignoreProperties>
   7:     </view>
   8:   </views>
   9: </object>
  10: <mappings>

以上是两种最简单的做法,通过NBear.Mapping,还有其它的调用方法来达到这个目的。

这篇博客比较短,目的是希望能让大家对NBear.Mapping有直观的印象。示例代码下载

阿不

posted on 2007-12-17 20:08  阿不  阅读(5571)  评论(14编辑  收藏  举报