CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataMapper.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataMapper.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Map data from a source into a target object</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.ComponentModel;
11using Csla.Properties;
12using Csla.Reflection;
13using System.Linq;
14
15namespace Csla.Data
16{
22 public static class DataMapper
23 {
24
25 #region Map from IDictionary
26
37 public static void Map(System.Collections.IDictionary source, object target)
38 {
39 Map(source, target, false);
40 }
41
54 public static void Map(System.Collections.IDictionary source, object target, params string[] ignoreList)
55 {
56 Map(source, target, false, ignoreList);
57 }
58
72 public static void Map(
73 System.Collections.IDictionary source,
74 object target, bool suppressExceptions,
75 params string[] ignoreList)
76 {
77 List<string> ignore = new List<string>(ignoreList);
78 foreach (string propertyName in source.Keys)
79 {
80 if (!ignore.Contains(propertyName))
81 {
82 try
83 {
84 SetPropertyValue(target, propertyName, source[propertyName]);
85 }
86 catch (Exception ex)
87 {
88 if (!suppressExceptions)
89 throw new ArgumentException(
90 String.Format("{0} ({1})",
91 Resources.PropertyCopyFailed, propertyName), ex);
92 }
93 }
94 }
95 }
96
97 #endregion
98
99 #region Map to Dictionary
100
106 public static void Map(object source, Dictionary<string, object> target)
107 {
108 Map(source, target, false);
109 }
110
118 public static void Map(object source, Dictionary<string, object> target, params string[] ignoreList)
119 {
120 Map(source, target, false, ignoreList);
121 }
122
131 public static void Map(
132 object source, Dictionary<string, object> target,
133 bool suppressExceptions,
134 params string[] ignoreList)
135 {
136 List<string> ignore = new List<string>(ignoreList);
137 foreach (var propertyName in GetPropertyNames(source.GetType()))
138 {
139 if (!ignore.Contains(propertyName))
140 {
141 try
142 {
143 target.Add(propertyName, MethodCaller.CallPropertyGetter(source, propertyName));
144 }
145 catch (Exception ex)
146 {
147 if (!suppressExceptions)
148 throw new ArgumentException(
149 String.Format("{0} ({1})",
150 Resources.PropertyCopyFailed, propertyName), ex);
151 }
152 }
153 }
154 }
155
156 #endregion
157
158 #region Map from Object
159
171 public static void Map(object source, object target)
172 {
173 Map(source, target, false);
174 }
175
189 public static void Map(object source, object target, params string[] ignoreList)
190 {
191 Map(source, target, false, ignoreList);
192 }
193
214 public static void Map(
215 object source, object target,
216 bool suppressExceptions,
217 params string[] ignoreList)
218 {
219 List<string> ignore = new List<string>(ignoreList);
220 foreach (var propertyName in GetPropertyNames(source.GetType()))
221 {
222 if (!ignore.Contains(propertyName))
223 {
224 try
225 {
226 object value = MethodCaller.CallPropertyGetter(source, propertyName);
227 SetPropertyValue(target, propertyName, value);
228 }
229 catch (Exception ex)
230 {
231 if (!suppressExceptions)
232 throw new ArgumentException(
233 String.Format("{0} ({1})",
234 Resources.PropertyCopyFailed, propertyName), ex);
235 }
236 }
237 }
238 }
239
252 public static void Map(object source, object target, DataMap map)
253 {
254 Map(source, target, map, false);
255 }
256
270 public static void Map(object source, object target, DataMap map, bool suppressExceptions)
271 {
272 foreach (DataMap.MemberMapping mapping in map.GetMap())
273 {
274 try
275 {
276 object value = mapping.FromMemberHandle.DynamicMemberGet(source);
277 SetValueWithCoercion(target, mapping.ToMemberHandle, value);
278 }
279 catch (Exception ex)
280 {
281 if (!suppressExceptions)
282 throw new ArgumentException(
283 String.Format("{0} ({1})",
284 Resources.PropertyCopyFailed, mapping.FromMemberHandle.MemberName), ex);
285 }
286 }
287 }
288
289 private static IList<string> GetPropertyNames(Type sourceType)
290 {
291 List<string> result = new List<string>();
292 PropertyDescriptorCollection props = TypeDescriptor.GetProperties(sourceType);
293 foreach (PropertyDescriptor item in props)
294 if (item.IsBrowsable)
295 result.Add(item.Name);
296 return result;
297 }
298 #endregion
299
300 #region Load from IDictionary
301
318 public static void Load(System.Collections.IDictionary source, object target, Func<string, object> nameMapper)
319 {
320 var validTarget = target as Core.IManageProperties;
321 if (validTarget == null)
322 throw new NotSupportedException();
323 var propertyList = validTarget.GetManagedProperties();
324 foreach (var p in propertyList)
325 validTarget.LoadProperty(p, source[nameMapper(p.Name)]);
326 }
327
328#endregion
329
330#region Load to IDictionary
331
348 public static void Load(object source, System.Collections.IDictionary target, Func<string, object> nameMapper)
349 {
350 var validSource = source as Core.IManageProperties;
351 if (validSource == null)
352 throw new NotSupportedException();
353 var propertyList = validSource.GetManagedProperties();
354 foreach (var p in propertyList)
355 target[nameMapper(p.Name)] = validSource.ReadProperty(p);
356 }
357
358#endregion
359
360#region SetValue
361
369 public static void SetPropertyValue(
370 object target, string propertyName, object value)
371 {
372 DynamicMemberHandle handle = MethodCaller.GetCachedProperty(target.GetType(), propertyName);
373 SetValueWithCoercion(target, handle, value);
374 }
375
376 private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
377 {
378 var oldValue = handle.DynamicMemberGet(target);
379
380 Type pType = handle.MemberType;
381
382 var isGeneric = pType.IsGenericType;
383 var isPrimitive = pType.IsPrimitive;
384 var isValueType = pType.IsValueType;
385 if (!isGeneric
386 || (isGeneric && pType.GetGenericTypeDefinition() != typeof(Nullable<>)))
387 {
388 if (isValueType && (isPrimitive || pType == typeof(decimal)) && value == null)
389 {
390 value = 0;
391 }
392 }
393
394 if (value != null)
395 {
396 Type vType = Utilities.GetPropertyType(value.GetType());
397 value = Utilities.CoerceValue(pType, vType, oldValue, value);
398 }
399 handle.DynamicMemberSet(target, value);
400 }
401
409 public static void SetFieldValue(
410 object target, string fieldName, object value)
411 {
412 DynamicMemberHandle handle = MethodCaller.GetCachedField(target.GetType(), fieldName);
413 SetValueWithCoercion(target, handle, value);
414 }
415
422 public static object GetFieldValue(
423 object target, string fieldName)
424 {
425 DynamicMemberHandle handle = MethodCaller.GetCachedField(target.GetType(), fieldName);
426 return handle.DynamicMemberGet.Invoke(target);
427 }
428
429#endregion
430 }
431}
A strongly-typed resource class, for looking up localized strings, etc.
static string PropertyCopyFailed
Looks up a localized string similar to Property copy failed.