NBearMapping - 开源通用对象映射组件v1.0.0.2 beta - 支持枚举类型字段

NBearMapping是NBearV4框架的组件之一,可以独立使用。可用于任意类型对象、DataRow和DataReader对象间的透明映射。推荐结合NBearLite使用。

主要功能:

1、任意类型对象、DataRow和DataReader对象间的透明映射;
2、支持.NET的Nullable类型;
3、较高的性能,性能比基于Reflection的等价转换快约50%,手动代码 vs NBearMapping vs Reflection对象转换的执行时间比大约为1:2.5:3.6,可参见源码中MappingTest.TestPerformance() 测试;


源码、示例及文档下载:

NBearMapping_v1.0.0.2_beta.zip


使用演示:

 1        [TestMethod]
 2        public void TestDataRowToObject()
 3        {
 4            ObjectMapper mapper = new ObjectMapper(typeof(DataRow), typeof(User));
 5            mapper.AddCustomMappingName("UserID""ID");
 6            User user = (User)mapper.ConvertObject(table.Rows[0]);
 7            Assert.AreEqual(table.Rows[0]["UserID"], user.ID);
 8            Assert.AreEqual(table.Rows[0]["Name"], user.Name);
 9            User user2 = new User();
10            mapper.ConvertObject(table.Rows[0], user2);
11            Assert.AreEqual(table.Rows[0]["UserID"], user2.ID);
12            Assert.AreEqual(table.Rows[0]["Name"], user2.Name);   
13        }

14
15        [TestMethod]
16        public void TestDataReaderToObject()
17        {
18            ObjectMapper mapper = new ObjectMapper(typeof(IDataReader), typeof(User));
19            mapper.AddCustomMappingName("UserID""ID");
20            IDataReader reader = table.CreateDataReader();
21            reader.Read();
22            User user = (User)mapper.ConvertObject(reader);
23            Assert.AreEqual(table.Rows[0]["UserID"], user.ID);
24            Assert.AreEqual(table.Rows[0]["Name"], user.Name);
25            User user2 = new User();
26            IDataReader reader2 = table.CreateDataReader();
27            reader2.Read();
28            mapper.ConvertObject(reader2, user2);
29            Assert.AreEqual(table.Rows[0]["UserID"], user2.ID);
30            Assert.AreEqual(table.Rows[0]["Name"], user2.Name);        
31        }

32
33        [TestMethod]
34        public void ObjectToObject()
35        {
36            ObjectMapper mapper = new ObjectMapper(typeof(User), typeof(User));
37            User user = (User)mapper.ConvertObject(user3);
38            Assert.AreEqual(user3.ID, user.ID);
39            Assert.AreEqual(user3.Name, user.Name);
40            User user2 = new User();
41            mapper.ConvertObject(user3, user2);
42            Assert.AreEqual(user3.ID, user2.ID);
43            Assert.AreEqual(user3.Name, user2.Name);        
44        }

45
46        [TestMethod]
47        public void TestObjectToDataTableDataReaderAndDataRow()
48        {
49            ObjectMapper mapper = new ObjectMapper(typeof(User), typeof(DataTable));
50            mapper.AddCustomMappingName("ID""UserID");
51            DataTable userTable = (DataTable)mapper.ConvertObject(user3);
52            Assert.AreEqual(user3.ID, userTable.Rows[0]["UserID"== DBNull.Value ? null : userTable.Rows[0]["UserID"]);
53            Assert.AreEqual(user3.Name, userTable.Rows[0]["Name"]);
54            mapper.ConvertObject(user3, userTable);
55            Assert.AreEqual(user3.ID, userTable.Rows[1]["UserID"== DBNull.Value ? null : userTable.Rows[1]["UserID"]);
56            Assert.AreEqual(user3.Name, userTable.Rows[1]["Name"]);
57
58            mapper = new ObjectMapper(typeof(User), typeof(IDataReader));
59            mapper.AddCustomMappingName("ID""UserID");
60            IDataReader reader = (IDataReader)mapper.ConvertObject(user3);
61            Assert.IsNotNull(reader);
62
63            mapper = new ObjectMapper(typeof(User), typeof(DataRow));
64            mapper.AddCustomMappingName("ID""UserID");
65            DataRow row = (DataRow)mapper.ConvertObject(user3);
66            Assert.IsNotNull(row);
67        }


修订
7/26 更新至v1.0.0.1 修复1.0.0.0中的set null值的bug。

8/6 更新至v1.0.0.2 支持枚举类型字段
posted @ 2007-07-25 15:23  Teddy's Knowledge Base  Views(6605)  Comments(14Edit  收藏  举报