0
|
1 using System;
|
|
2
|
|
3 using NUnit.Framework;
|
|
4
|
|
5 using BLToolkit.Data;
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.Reflection.Extension;
|
|
8 using BLToolkit.Reflection.MetadataProvider;
|
|
9
|
|
10 namespace Mapping.MetadataProvider
|
|
11 {
|
|
12 [TestFixture]
|
|
13 public class MetadataProviderTest
|
|
14 {
|
|
15 class CustomMetadataProvider : MetadataProviderBase
|
|
16 {
|
|
17 public override string GetFieldName(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
|
|
18 {
|
|
19 string name = string.Empty;
|
|
20
|
|
21 foreach (char c in member.Name)
|
|
22 {
|
|
23 if (char.IsUpper(c))
|
|
24 {
|
|
25 if (name.Length > 0)
|
|
26 name += '_';
|
|
27 name += c;
|
|
28 }
|
|
29 else
|
|
30 {
|
|
31 name += char.ToUpper(c);
|
|
32 }
|
|
33 }
|
|
34
|
|
35 isSet = true;
|
|
36
|
|
37 return name;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 public class Person
|
|
42 {
|
|
43 public string FirstName;
|
|
44 public string LastName;
|
|
45 }
|
|
46
|
|
47 static void MapMetadataProvider_OnCreateProvider(MetadataProviderBase parentProvider)
|
|
48 {
|
|
49 parentProvider.AddProvider(new CustomMetadataProvider());
|
|
50 }
|
|
51
|
|
52 [Test]
|
|
53 public void Test()
|
|
54 {
|
|
55 MetadataProviderBase.OnCreateProvider += MapMetadataProvider_OnCreateProvider;
|
|
56
|
|
57 string cmd = "SELECT '1' as FIRST_NAME, '2' as LAST_NAME";
|
|
58 #if ORACLE || FIREBIRD
|
|
59 cmd += " FROM dual";
|
|
60 #endif
|
|
61 using (DbManager db = new DbManager())
|
|
62 {
|
|
63 Person p = (Person)db
|
|
64 .SetCommand(cmd)
|
|
65 .ExecuteObject(typeof(Person));
|
|
66
|
|
67 Assert.AreEqual("1", p.FirstName);
|
|
68 Assert.AreEqual("2", p.LastName);
|
|
69 }
|
|
70 }
|
|
71
|
|
72 [TearDown]
|
|
73 public void TearDown()
|
|
74 {
|
|
75 MetadataProviderBase.OnCreateProvider -= MapMetadataProvider_OnCreateProvider;
|
|
76 }
|
|
77 }
|
|
78 }
|