1: /// <summary>
2: /// Maps two objects together
3: /// </summary>
4: /// <typeparam name="Source">Source type</typeparam>
5: /// <typeparam name="Destination">Destination type</typeparam>
6: public class Mapping<Source,Destination>:IMapping
7: {
8:
9: #region Constructor
10:
11: /// <summary>
12: /// Constructor
13: /// </summary>
14: public Mapping()
15: {
16: Setup();
17: }
18:
19: #endregion
20:
21: #region IMapping Members
22:
23: public Type SourceType { get { return typeof(Source); } }
24:
25: public Type DestinationType { get { return typeof(Destination); } }
26:
27: public void Sync(object SourceObject, object DestinationObject)
28: {
29: if (SourceObject.GetType() == SourceType)
30: {
31: for (int y = 0; y < Mappings.Count; ++y)
32: {
33: object SourceValue = Utilities.Reflection.GetPropertyValue(SourceObject, Mappings[y].Source);
34: Utilities.Reflection.SetValue(SourceValue, DestinationObject, Mappings[y].Destination, Mappings[y].Format);
35: }
36: return;
37: }
38: for (int y = 0; y < Mappings.Count; ++y)
39: {
40: object DestinationValue = Utilities.Reflection.GetPropertyValue(SourceObject, Mappings[y].Destination);
41: Utilities.Reflection.SetValue(DestinationValue, DestinationObject, Mappings[y].Source, Mappings[y].Format);
42: }
43: }
44:
45: public object Create(object Source)
46: {
47: if (Source.GetType() == SourceType)
48: {
49: object DestinationObject = typeof(Destination).Assembly.CreateInstance(typeof(Destination).FullName);
50: Sync(Source, DestinationObject);
51: return DestinationObject;
52: }
53: object SourceObject = typeof(Destination).Assembly.CreateInstance(typeof(Source).FullName);
54: Sync(Source, SourceObject);
55: return SourceObject;
56: }
57:
58: #endregion
59:
60: #region Protected Functions
61:
62: /// <summary>
63: /// Maps a source property to a destination property
64: /// </summary>
65: /// <param name="SourceExpression">Source property</param>
66: /// <param name="DestinationExpression">Destination property</param>
67: protected void Map(Expression<Func<Source, object>> SourceExpression,
68: Expression<Func<Destination, object>> DestinationExpression)
69: {
70: Map(SourceExpression, DestinationExpression, "");
71: }
72:
73: /// <summary>
74: /// Maps a source property to a destination property
75: /// </summary>
76: /// <param name="SourceExpression">Source property</param>
77: /// <param name="DestinationExpression">Destination property</param>
78: /// <param name="Format">The string format that the destination should use</param>
79: protected void Map(Expression<Func<Source, object>> SourceExpression,
80: Expression<Func<Destination, object>> DestinationExpression,
81: string Format)
82: {
83: Setup();
84: string SourceName = Utilities.Reflection.GetPropertyName<Source>(SourceExpression);
85: string DestinationName = Utilities.Reflection.GetPropertyName<Destination>(DestinationExpression);
86: for (int x = 0; x < Mappings.Count; ++x)
87: {
88: if (Mappings[x].Destination == DestinationName)
89: {
90: Mappings.RemoveAt(x);
91: break;
92: }
93: }
94: for (int x = 0; x < Mappings.Count; ++x)
95: {
96: if (Mappings[x].Source == SourceName)
97: {
98: Mappings.RemoveAt(x);
99: break;
100: }
101: }
102: Mappings.Add(new MappingInfo(SourceName, DestinationName, Format));
103: }
104:
105: /// <summary>
106: /// Removes a mapping
107: /// </summary>
108: /// <param name="SourceExpression">Source property to ignore</param>
109: protected void Ignore(Expression<Func<Source, object>> SourceExpression)
110: {
111: Setup();
112: string SourceName = Utilities.Reflection.GetPropertyName<Source>(SourceExpression);
113: for (int x = 0; x < Mappings.Count; ++x)
114: {
115: if (Mappings[x].Source == SourceName)
116: {
117: Mappings.RemoveAt(x);
118: break;
119: }
120: }
121: }
122:
123: /// <summary>
124: /// Removes a mapping
125: /// </summary>
126: /// <param name="DestinationExpression">Destination property to ignore</param>
127: protected void Ignore(Expression<Func<Destination, object>> DestinationExpression)
128: {
129: Setup();
130: string DestinationName = Utilities.Reflection.GetPropertyName<Destination>(DestinationExpression);
131: for (int x = 0; x < Mappings.Count; ++x)
132: {
133: if (Mappings[x].Destination == DestinationName)
134: {
135: Mappings.RemoveAt(x);
136: break;
137: }
138: }
139: }
140:
141: #endregion
142:
143: #region Private Functions
144:
145: /// <summary>
146: /// Sets up the mapping
147: /// </summary>
148: public void Setup()
149: {
150: if (Mappings == null)
151: {
152: Mappings = new List<MappingInfo>();
153: PropertyInfo[] Properties = typeof(Source).GetProperties();
154: Type DestinationType = typeof(Destination);
155: for (int x = 0; x < Properties.Length; ++x)
156: {
157: if (DestinationType.GetProperty(Properties[x].Name) != null)
158: {
159: Mappings.Add(new MappingInfo(Properties[x].Name, Properties[x].Name, ""));
160: }
161: }
162: }
163: }
164:
165: #endregion
166:
167: #region Protected Variables
168:
169: internal List<MappingInfo> Mappings { get; set; }
170:
171: #endregion
172: }