comparison Source/Mapping/ValueMapping.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 using BLToolkit.Common;
4
5 using KeyValue = System.Collections.Generic.KeyValuePair<System.Type, System.Type>;
6 using Table = System.Collections.Generic.Dictionary<System.Collections.Generic.KeyValuePair<System.Type, System.Type>, BLToolkit.Mapping.IValueMapper>;
7
8 namespace BLToolkit.Mapping
9 {
10 public static class ValueMapping
11 {
12 #region Init
13
14 private static readonly Table _mappers = new Table();
15
16 #endregion
17
18 #region Default Mapper
19
20 class DefaultValueMapper : IValueMapper
21 {
22 public void Map(
23 IMapDataSource source, object sourceObject, int sourceIndex,
24 IMapDataDestination dest, object destObject, int destIndex)
25 {
26 dest.SetValue(destObject, destIndex, source.GetValue(sourceObject, sourceIndex));
27
28 //object o = source.GetValue(sourceObject, sourceIndex);
29
30 //if (o == null) dest.SetNull (destObject, destIndex);
31 //else dest.SetValue(destObject, destIndex, o);
32 }
33 }
34
35 private static IValueMapper _defaultMapper = new DefaultValueMapper();
36 [CLSCompliant(false)]
37 public static IValueMapper DefaultMapper
38 {
39 get { return _defaultMapper; }
40 set { _defaultMapper = value; }
41 }
42
43 #endregion
44
45 #region GetMapper
46
47 private static readonly object _sync = new object();
48
49 [CLSCompliant(false)]
50 public static IValueMapper GetMapper(Type t1, Type t2)
51 {
52 if (t1 == null) t1 = typeof(object);
53 if (t2 == null) t2 = typeof(object);
54
55 if (t1.IsEnum) t1 = Enum.GetUnderlyingType(t1);
56 if (t2.IsEnum) t2 = Enum.GetUnderlyingType(t2);
57
58 var key = new KeyValue(t1, t2);
59
60 lock (_sync)
61 {
62 IValueMapper t;
63
64 if (_mappers.TryGetValue(key, out t))
65 return t;
66
67 //t = BLToolkit.Mapping.ValueMappingInternal.MapperSelector.GetMapper(t1, t2);
68
69 if (null == t)
70 {
71 var type = typeof(GetSetDataChecker<,>).MakeGenericType(t1, t2);
72
73 if (((IGetSetDataChecker)Activator.CreateInstance(type)).Check() == false)
74 {
75 t = _defaultMapper;
76 }
77 else
78 {
79 type = t1 == t2 ?
80 typeof(ValueMapper<>).MakeGenericType(t1) :
81 typeof(ValueMapper<,>).MakeGenericType(t1, t2);
82
83 t = (IValueMapper)Activator.CreateInstance(type);
84 }
85 }
86
87 _mappers.Add(key, t);
88
89 return t;
90 }
91 }
92
93 #endregion
94
95 #region Generic Mappers
96
97 interface IGetSetDataChecker
98 {
99 bool Check();
100 }
101
102 class GetSetDataChecker<S,D> : IGetSetDataChecker
103 {
104 public bool Check()
105 {
106 return
107 !(MapGetData<S>.I is MapGetData<S>.Default<S>) &&
108 !(MapSetData<S>.I is MapSetData<S>.Default<S>) &&
109 !(MapGetData<D>.I is MapGetData<D>.Default<D>) &&
110 !(MapSetData<D>.I is MapSetData<D>.Default<D>);
111 }
112 }
113
114 class ValueMapper<T> : IValueMapper
115 {
116 public void Map(
117 IMapDataSource source, object sourceObject, int sourceIndex,
118 IMapDataDestination dest, object destObject, int destIndex)
119 {
120 if (source.IsNull(sourceObject, sourceIndex))
121 dest.SetNull(destObject, destIndex);
122 else
123 {
124 var setter = MapSetData<T>.I;
125 var getter = MapGetData<T>.I;
126
127 setter.To(dest, destObject, destIndex,
128 getter.From(source, sourceObject, sourceIndex));
129 }
130 }
131 }
132
133 class ValueMapper<S,D> : IValueMapper
134 {
135 public void Map(
136 IMapDataSource source, object sourceObject, int sourceIndex,
137 IMapDataDestination dest, object destObject, int destIndex)
138 {
139 if (source.IsNull(sourceObject, sourceIndex))
140 dest.SetNull(destObject, destIndex);
141 else
142 {
143 var setter = MapSetData<D>.I;
144 var getter = MapGetData<S>.I;
145 var converter = Convert<D,S>.From;
146
147 setter.To(dest, destObject, destIndex,
148 converter(getter.From(source, sourceObject, sourceIndex)));
149 }
150 }
151 }
152
153 #endregion
154 }
155 }