阿不

潜水

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

在NBear.Mapping中提供了一种灵活的配置方式,允许你在不改变原有代码的情况下,随时对映射需求进行修改,同时即使你在开发初始期不进行配置,它仍然可以正常的工作。极大的方便你的开发和维护工作。

在你使用ObjectConvertor的接口进行对象映射时,系统会根据你指定的输入类型(如果没有指定则以inputObject的类型为准)、输出类型和viewName,去查找系统已经存在的对应的配置(表现为一个缓存的ObjectMapper对象),如果找到这个配置,则会使用这个ObjectMapper对象,否则会新创建一个ObjectMapper对象来执行映射动作。

在NBear.Mapping.Test中,你可以找到NBear.Mapping.config最完整的配置语法案例,它主要有以一些配置节点:

   1: <NBear.Mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   2:   <typeAliases>
   3:   </typeAliases>
   4:   <defaultInitTypes>
   5:   </defaultInitTypes>
   6:   <mappings>
   7:   </mappings>
   8: </NBear.Mapping>
根配置节点名称 说明
typeAliases
在这里配置一些类型的别名,比如System.Collections.ArrayList你每次都要写完成类名很麻烦,使用ArrayList就方便又简单
defaultInitTypes
指定输入类型的默认实例化类型,如果你的配置节中没有显式的指定实例化类型,而这里有对应的输入类型的实例化类型的话,那就会使用这里的配置。
mappings
对象的映射配置都在这个配置节下面。
typeAliases 配置节
typeAliases 配置节点名称 说明
remove
如果在其它的配置文件已经存在了相同的配置,则清除已经有的冲突配置。如下:<remove type="ArrayList"></remove>
add
增加一个类型别名配置,如下:
<add type="ArrayList" fullTypeName="System.Collections.ArrayList"></add>

defaultInitTypes 配置节

defaultInitTypes 的子节点名称 说明
remove
如果在其它的配置文件已经存在了相同的配置,则清除已经有的冲突配置。如下:
<remove type="IList"></remove>
add
增加一个默认初始化类型的配置,如下:
<add type="System.Collections.IList" initType="ArrayList"></add>

mappings 配置节

mappings下的节点,由object组成。每一个object节点有两个属性

object的属性 说明 要求
inputType
映射的输入类型 必填
outputType
映射的输出类型 必填

由于每一个输入和输出类型可能会有多种的映射方案,我们将不同的映射方案都称之为view,对应的都有一个viewName,放在object的views节点下面,同时在views节点中,你还可以指定它默认使用哪个view,如果.NET 2.0中的provider模型。

views节点的属性

views的属性 说明 要求
defaultView
指定默认使用哪种配置方案,如果没有指定,则默认使用最后一种配置方案。 可选

views节点下,就是配置各种不同映射方案的地方了。它由一些view配置组成,view配置的属性:

view的属性 说明 要求
name
设定这种映射方案的名称 可选
initType
指定在这种映射方案下它所使用的初始化类型 可选
mappingSpecifiedOnly
指定在这种映射方案下,它是否为显式映射(只映射指定的字段) 可选

view配置节下,可以配置一些指定的映射字段和映射顺序,希望被忽略的字段,以及自定义的映射行为。

view的子配置节点 说明
properties
不同源字段名和目标字段的映射关系配置,如果view的mappingSpecifiedOnly设为true,也需显式指定配置字段,配置示例:
<add srcName="UserID" destName="ID" order="1"/>
ignoreProperties
希望被排除的字段名,这里指定的是输出字段名。
<add destName="Status"></add>
customMapping
如果默认的NBear.Mapping行为不能满足您的要求,你可以定义一个ICustomObjectMemberMapping的实现,来完成您的自定义映射工作。
<add className="NBear.Mapping.Test.CustomUserToUser2"></add>

 

properties子节点可配置的属性
说明 要求
srcName
映射的源字段名 必填
destName
映射的目标(输出)字段名 必填
order
映射的顺序,您可以在这里指定这个字段在第几位被映射,但是不可以重复。如果第1的位字段不能有两个。 可选

 

ignoreProperties子节点可配置的属性
说明 要求
destName
希望被除排除的目标字段 必填

 

customMapping子节点可配置的属性
说明 要求
className
实现ICustomObjectMemberMapping的类名 必填

以上就是NBear.Mapping配置的所有内容了,可以说是相当简单。只要你看一个完整的例子应该就可以明白了,下面是NBear.Mapping.Test中的完整配置例子。

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <NBear.Mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   3:   <typeAliases>
   4:     <remove type="ArrayList"></remove>
   5:     <add type="ArrayList" fullTypeName="System.Collections.ArrayList"></add>
   6:     <add type="NameValueCollection" fullTypeName="System.Collections.Specialized.NameValueCollection"></add>
   7:     <add type="User" fullTypeName="NBear.Mapping.Test.User"></add>
   8:     <add type="User2" fullTypeName="NBear.Mapping.Test.User2"></add>
   9:     <add type="DataRow" fullTypeName="System.Data.DataRow"/>
  10:     <add type="IDataReader" fullTypeName="System.Data.IDataReader"/>
  11:     <add type="IList" fullTypeName="System.Collections.IList"/>
  12:     <add type="IUser" fullTypeName="NBear.Mapping.Test.IUser"></add>
  13:   </typeAliases>
  14:   <defaultInitTypes>
  15:     <remove type="IList"></remove>
  16:     <add type="System.Collections.IList" initType="ArrayList"></add>
  17:   </defaultInitTypes>
  18:   <mappings>
  19:     <object inputType="DataRow" outputType="User">
  20:         <views>
  21:             <view name="Default" mappingSpecifiedOnly="false">
  22:                 <properties>
  23:                     <add srcName="UserID" destName="ID" order="1"/>
  24:                 </properties>
  25:             </view>
  26:         </views>
  27:     </object>
  28:     
  29:     <object inputType="DataRow" outputType="User2">
  30:       <views>
  31:         <view name="Default" mappingSpecifiedOnly="false">
  32:           <properties>
  33:             <add srcName="UserID" destName="ID" order="1"/>
  34:           </properties>
  35:         </view>
  36:       </views>
  37:     </object>
  38:  
  39:     <object inputType="IDataReader" outputType="User2">
  40:       <views>
  41:         <view name="Default" mappingSpecifiedOnly="false">
  42:           <properties>
  43:             <add srcName="UserID" destName="ID" order="1"/>
  44:           </properties>
  45:         </view>
  46:       </views>
  47:     </object>
  48:     
  49:     
  50:     <object inputType="IUser" outputType="User">
  51:         <views>
  52:             <view mappingSpecifiedOnly="true" name="Default">
  53:                 <properties>
  54:                     <add srcName="UserID" destName="ID" />
  55:                 </properties>
  56:             </view>
  57:         </views>
  58:     </object>
  59:     
  60:     <object inputType="User" outputType="User2">
  61:         <views defaultView="default">
  62:             <view mappingSpecifiedOnly="true"  name="default">
  63:                 <properties>
  64:                     <add srcName="UserID" destName="ID" order="1"/>
  65:                     <add srcName="Status" destName="Status" order="2"/>
  66:                 </properties>
  67:             </view>
  68:             <view mappingSpecifiedOnly="false" name="ignoreStatus">
  69:                 <ignoreProperties>
  70:                     <add destName="Status"></add>
  71:                 </ignoreProperties>
  72:             </view>
  73:             <view  mappingSpecifiedOnly="true" name="customMapping">
  74:                 <customMapping>
  75:                     <add className="NBear.Mapping.Test.CustomUserToUser2"></add>
  76:                 </customMapping>
  77:             </view>
  78:         </views>
  79:     </object>
  80:     
  81:     <object inputType="IUser" outputType="IUser">
  82:         <views>
  83:             <view mappingSpecifiedOnly="false" initType="User2" name="initTypeASUser2">
  84:             </view>
  85:         </views>
  86:     </object>
  87:     
  88:     <object inputType="NBear.Mapping.Test.OrderedClass" outputType="NBear.Mapping.Test.OrderedClass">
  89:         <views>
  90:             <view name="Default">
  91:                 <properties>
  92:                     <add srcName="Property3" destName="Property3" order="2"/>
  93:                 </properties>
  94:             </view>
  95:         </views>
  96:     </object>
  97:   </mappings>
  98: </NBear.Mapping>

最后,如何将配置添加到系统中? 在系统配置文件中(web.config或app.config),增加一个配置节名称:

<configSections>
<section name="nbearMapping" type="NBear.Mapping.NBearMappingSection,NBear.Mapping"/>
</configSections>
然后增加配置:
<nbearMapping>
<
includes>
<
add key="test" value="NBear.Mapping.config"></add>
</
includes>
</
nbearMapping>

当然,includes下是可以支持多个文件的。也就是说,NBear.Mapping是支持多个配置文件。

阿不

posted on 2007-12-16 22:13  阿不  阅读(3044)  评论(18编辑  收藏  举报