1: /// <summary>
2: /// Maps a type to another type
3: /// </summary>
4: public class TypeMapping : Factory<Type, object>, ITypeMapping
5: {
6: #region Constructor
7:
8: /// <summary>
9: /// Constructor
10: /// </summary>
11: public TypeMapping()
12: {
13: Setup();
14: }
15:
16: #endregion
17:
18: #region Private Functions
19:
20: /// <summary>
21: /// Sets up the mappings
22: /// </summary>
23: private void Setup()
24: {
25: if (PropertyMappings != null)
26: return;
27: PropertyMappings = new Dictionary<Type, MappingInfo>();
28: }
29:
30: #endregion
31:
32: #region Protected Functions
33:
34: /// <summary>
35: /// Maps a type to another type
36: /// </summary>
37: /// <typeparam name="Source">Source type</typeparam>
38: /// <typeparam name="Destination">Destination type</typeparam>
39: /// <param name="DestinationType">Function for creating the destination type</param>
40: /// <param name="DestinationProperty">Property to map to</param>
41: protected void Map<Source, Destination>(Func<object> DestinationType,
42: Expression<Func<Destination, object>> DestinationProperty)
43: {
44: Setup();
45: Register(typeof(Source), DestinationType);
46: PropertyMappings.Add(typeof(Source), new MappingInfo("", Utilities.Reflection.GetPropertyName<Destination>(DestinationProperty), "",""));
47: }
48:
49: /// <summary>
50: /// Maps a type to another type
51: /// </summary>
52: /// <typeparam name="Source">Source type</typeparam>
53: /// <typeparam name="Destination">Destination type</typeparam>
54: /// <param name="DestinationType">Function for creating the destination type</param>
55: /// <param name="DestinationProperty">Property to map to</param>
56: /// <param name="Format">Format string</param>
57: protected void Map<Source, Destination>(Func<object> DestinationType,
58: Expression<Func<Destination, object>> DestinationProperty,
59: string Format)
60: {
61: Setup();
62: Register(typeof(Source), DestinationType);
63: PropertyMappings.Add(typeof(Source), new MappingInfo("", Utilities.Reflection.GetPropertyName<Destination>(DestinationProperty), Format, ""));
64: }
65:
66: /// <summary>
67: /// Maps a description to another type
68: /// </summary>
69: /// <typeparam name="Destination">Destination type</typeparam>
70: /// <param name="DestinationType">Function for creating the destination type</param>
71: /// <param name="DestinationProperty">Property to map to</param>
72: protected void MapDescription<Destination>(Func<object> DestinationType,
73: Expression<Func<Destination, object>> DestinationProperty)
74: {
75: MapDescription<Destination>(DestinationType, DestinationProperty, "");
76: }
77:
78: /// <summary>
79: /// Maps a description to another type
80: /// </summary>
81: /// <typeparam name="Destination">Destination type</typeparam>
82: /// <param name="DestinationType">Function for creating the destination type</param>
83: /// <param name="DestinationProperty">Property to map to</param>
84: /// <param name="Format">Formatting string</param>
85: protected void MapDescription<Destination>(Func<object> DestinationType,
86: Expression<Func<Destination, object>> DestinationProperty,
87: string Format)
88: {
89: Register(typeof(MappingInfo), DestinationType);
90: DescriptionMapping = new MappingInfo("", Utilities.Reflection.GetPropertyName<Destination>(DestinationProperty), Format, "");
91: }
92:
93: #endregion
94:
95: #region Public Functions
96:
97: /// <summary>
98: /// Creates the destination type
99: /// </summary>
100: /// <param name="SourceValue">Source value</param>
101: /// <param name="SourceType">Source type</param>
102: /// <returns>The destination type</returns>
103: public object CreateDestination(object SourceValue, Type SourceType)
104: {
105: object DestinationObject = Create(SourceType);
106: Utilities.Reflection.SetValue(SourceValue, DestinationObject, PropertyMappings[SourceType].Destination, PropertyMappings[SourceType].Format);
107: return DestinationObject;
108: }
109:
110: /// <summary>
111: /// Creates a description object based off of the description input
112: /// </summary>
113: /// <param name="DescriptionValue">The description value</param>
114: /// <returns>The description object</returns>
115: public object CreateDescription(object DescriptionValue)
116: {
117: object DestinationObject = Create(typeof(MappingInfo));
118: Utilities.Reflection.SetValue(DescriptionValue, DestinationObject, DescriptionMapping.Destination, DescriptionMapping.Format);
119: return DestinationObject;
120: }
121:
122: /// <summary>
123: /// Syncs the source to the destination
124: /// </summary>
125: /// <param name="SourceObject">Source object</param>
126: /// <param name="SourcePropertyInfo">Property to copy</param>
127: /// <param name="DestinationObject">Destination object</param>
128: public void Sync(object SourceObject, PropertyInfo SourcePropertyInfo, object DestinationObject)
129: {
130: object Value = Utilities.Reflection.GetPropertyValue(DestinationObject, PropertyMappings[SourcePropertyInfo.PropertyType].Destination);
131: Utilities.Reflection.SetValue(Value, SourceObject, SourcePropertyInfo, PropertyMappings[SourcePropertyInfo.PropertyType].Format);
132: }
133:
134: #endregion
135:
136: #region Internal Properties
137:
138: internal Dictionary<Type, MappingInfo> PropertyMappings { get; set; }
139: internal MappingInfo DescriptionMapping { get; set; }
140:
141: #endregion
142: }