private static object MapLazy(ConventionInfo conventionInfo) { var sourceArgument = conventionInfo.SourceProp.Type.GetGenericArguments()[0]; dynamic lazy = conventionInfo.SourceProp.Value; if (lazy.IsLoaded && lazy.Value != null) { if (conventionInfo.TargetProp.Type.IsAssignableFrom(sourceArgument)) { return lazy.Value; } var genericArgument = conventionInfo.TargetProp.Type; if (genericArgument.IsValueType || genericArgument == typeof(string)) { return lazy.Value; } if (genericArgument.IsGenericType) { if (conventionInfo.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Any(d => d == typeof(IEnumerable))) { return MapLists(genericArgument, lazy.Value); } } return Activator.CreateInstance(genericArgument).InjectFrom((object)lazy.Value); } return null; }
protected override Boolean Match(ConventionInfo c) { // Allow only datetime and variations // Don't allow a nullable datetime with a null value to match a target that is non-nullable return base.Match(c) && ((c.SourceProp.Type == typeof(DateTime) || c.SourceProp.Type == typeof(DateTimeOffset) || c.SourceProp.Type == typeof(DateTime?) || c.SourceProp.Type == typeof(DateTimeOffset?)) && (c.TargetProp.Type == typeof(DateTime) || c.TargetProp.Type == typeof(DateTimeOffset) || c.TargetProp.Type == typeof(DateTime?) || c.TargetProp.Type == typeof(DateTimeOffset?))) && !((c.SourceProp.Type == typeof(DateTime?) || c.SourceProp.Type == typeof(DateTimeOffset?)) && (c.TargetProp.Type == typeof(DateTime) || c.TargetProp.Type == typeof(DateTimeOffset)) && c.SourceProp.Value == null); }
//public class NullableAndEnumValueInjecter : ValueInjection //{ // protected override void Inject(object source, object target) // { // var sourceProps = source.GetProps(); // var targetType = target.GetType(); // foreach (var sourceProperty in sourceProps) // { // var targetProperty = targetType.GetProperty(sourceProperty.Name); // if (targetProperty != null && PropertyMatch(sourceProperty, targetProperty)) // { // if (targetProperty.PropertyType.IsEnum) // { // var value = Enum.Parse(targetProperty.PropertyType, sourceProperty.GetValue(source).ToString(), true); // targetProperty.SetValue(target, value); // } // if (targetProperty.PropertyType == typeof(string) && sourceProperty.PropertyType.IsEnum) // { // targetProperty.SetValue(target, sourceProperty.GetValue(source).ToString()); // } // else // { // if (!targetProperty.PropertyType.IsEnum) // { // targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null); // } // } // } // } // } private static bool PropertyMatch(ConventionInfo.PropInfo source, ConventionInfo.PropInfo target) { var result = string.Equals(source.Name, target.Name); if (result) { result = !source.Type.IsArray && !target.Type.IsArray; } if (result) { result = !target.Type.IsArray && !target.Type.IsArray; } if (result) { result = source.Type == target.Type || source.Type == Nullable.GetUnderlyingType(target.Type) || Nullable.GetUnderlyingType(source.Type) == target.Type; if (!result) { result = source.Type.IsEnum || target.Type.IsEnum; } } return result; }
protected override void Inject(object source, object target) { var sourceProps = source.GetProps(); var targetProps = target.GetProps(); var ci = new ConventionInfo { Source = { Type = source.GetType(), Value = source }, Target = { Type = target.GetType(), Value = target } }; for (var i = 0; i < sourceProps.Count; i++) { var s = sourceProps[i]; if (!s.IsReadOnly || (s.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))) { ci.SourceProp.Name = s.Name; ci.SourceProp.Value = s.GetValue(source); if (s.GetValue(source) != null) ci.SourceProp.Type = s.GetValue(source).GetType(); else ci.SourceProp.Type = s.PropertyType; for (var j = 0; j < targetProps.Count; j++) { var t = targetProps[j]; if (!t.IsReadOnly || (t.PropertyType.GetInterfaces().Contains(typeof(IEnumerable)))) { ci.TargetProp.Name = t.Name; ci.TargetProp.Value = t.GetValue(target); if (t.GetValue(target) != null) ci.TargetProp.Type = t.GetValue(target).GetType(); else if (ci.SourceProp.Type != null) ci.TargetProp.Type = ci.SourceProp.Type; else ci.TargetProp.Type = t.PropertyType; if (Match(ci)) { if (ci.SourceProp.Value != null) { var sv = SetValue(ci); if (sv != null) t.SetValue(target, sv); } break; } } } } } }
protected override bool Match(ConventionInfo c) { var propertyMatch = c.SourceProp.Name == c.TargetProp.Name; var sourceNotNull = c.SourceProp.Value != null; bool targetPropertyIdWritable = !(propertyMatch && c.TargetProp.Name == "Id" && !(c.Target.Value is IEntityClass)); return propertyMatch && sourceNotNull && targetPropertyIdWritable; }
protected override object SetValue(ConventionInfo c) { if (c.SourceProp.Value == null) return null; var id = ((int?)c.SourceProp.Value).Value; dynamic repo = IoC.Resolve(typeof(IRepo<>).MakeGenericType(c.TargetProp.Type)); return repo.Get(id); }
protected override bool Match(ConventionInfo c) { return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type.IsGenericType && c.TargetProp.Type.IsGenericType && c.SourceProp.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>) && c.TargetProp.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>) && c.SourceProp.Type.GetGenericArguments()[0].IsSubclassOf(typeof(Entity)) && c.TargetProp.Type.GetGenericArguments()[0] == typeof(int); }
protected override bool Match(ConventionInfo c) { //ignore int = 0 and DateTime = to 1/01/0001 if (c.SourceProp.Type == typeof(DateTime) && (DateTime)c.SourceProp.Value == default(DateTime) || (c.SourceProp.Type == typeof(int) && (int)c.SourceProp.Value == default(int))) return false; return (c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == Nullable.GetUnderlyingType(c.TargetProp.Type)); }
protected override object SetValue(ConventionInfo c) { //for value types and string just return the value as is if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)) return c.SourceProp.Value; //for simple object types create a new instance and apply the clone injection on it return null; }
protected override object SetValue(ConventionInfo c) { if (c.SourceProp.Value == null) return null; dynamic repo = IoC.Resolve(typeof(IRepo<>).MakeGenericType(c.TargetProp.Type.GetGenericArguments()[0])); dynamic list = Activator.CreateInstance(typeof(List<>).MakeGenericType(c.TargetProp.Type.GetGenericArguments()[0])); foreach (var i in ((IEnumerable<int>)c.SourceProp.Value)) list.Add(repo.Get(i)); return list; }
protected override object SetValue(ConventionInfo c) { if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)) return c.SourceProp.Value; if (c.SourceProp.Type.IsGenericType) { var td = c.SourceProp.Type.GetGenericTypeDefinition(); if (td != null && td.GetInterfaces().Contains(typeof(IEnumerable))) { var targetChildType = c.TargetProp.Type.GetGenericArguments()[0]; if (targetChildType.IsValueType || targetChildType == typeof(string)) return c.SourceProp.Value; if (targetChildType.GetInterfaces().Any(x => x == typeof(IValueClass))) { var deleteMethod = c.TargetProp.Value.GetType().GetMethod("Remove"); var rmvItems = (c.TargetProp.Value as IEnumerable).Cast<IValueClass>() .Where(x => x.Id > 0 && !(c.SourceProp.Value as IEnumerable).Cast<IValueClass>().Any(y => y.Id == x.Id)); rmvItems.ToList().ForEach(x => deleteMethod.Invoke(c.TargetProp.Value, new[] { x })); rmvItems = (c.TargetProp.Value as IEnumerable).Cast<IValueClass>() .Where(x => !(c.SourceProp.Value as IEnumerable).Cast<IValueClass>().Contains(x)); rmvItems.ToList().ForEach(x => deleteMethod.Invoke(c.TargetProp.Value, new[] { x })); var sourceCollection = (c.SourceProp.Value as IEnumerable).Cast<IValueClass>(); foreach (var s in sourceCollection) { var sv = s; var target = (c.TargetProp.Value as IEnumerable).Cast<IValueClass>().SingleOrDefault(z => z.Id == sv.Id && z.Id != 0); if (target != null) target.InjectFrom<EntityInjection>(sv); else if (!(c.TargetProp.Value as IEnumerable).Cast<IValueClass>().Contains(sv)) { if (!(sv is IEntityClass)) { sv = Activator.CreateInstance(targetChildType) as IValueClass; Debug.Assert(sv != null); sv.InjectFrom<EntityInjection>(s); sv.Id = 0; } var addMethod = c.TargetProp.Value.GetType().GetMethod("Add"); addMethod.Invoke(c.TargetProp.Value, new[] { sv }); } } } } return c.TargetProp.Value; } if (c.TargetProp.Value == null) c.TargetProp.Value = Activator.CreateInstance(c.TargetProp.Type); return c.TargetProp.Value.InjectFrom<EntityInjection>(c.SourceProp.Value); }
protected override bool Match(ConventionInfo c) { if (c.SourceProp.Name != c.TargetProp.Name) return false; var s = c.SourceProp.Type; var t = c.TargetProp.Type; if (!s.IsGenericType || !t.IsGenericType || s.GetGenericTypeDefinition() != typeof(IEnumerable<>) || t.GetGenericTypeDefinition() != typeof(ICollection<>)) return false; return s.GetGenericArguments()[0] == (typeof(int)) && (t.GetGenericArguments()[0].IsSubclassOf(typeof(Entity))); }
private static object MapLazy(ConventionInfo conventionInfo) { var genericArgument = conventionInfo.SourceProp.Type.GetGenericArguments()[0]; dynamic lazy = conventionInfo.SourceProp.Value; if (lazy.IsLoaded && conventionInfo.TargetProp.Type.IsAssignableFrom(genericArgument)) { return lazy.Value; } return null; }
protected override object SetValue(ConventionInfo c) { //for value types and string just return the value as is if (c.SourceProp.Type.IsValueType || c.SourceProp.Type == typeof(string)) return c.SourceProp.Value; //handle arrays if (c.SourceProp.Type.IsArray) { var arr = c.SourceProp.Value as Array; var clone = arr.Clone() as Array; for (int index = 0; index < arr.Length; index++) { var a = arr.GetValue(index); if (a.GetType().IsValueType || a is string) continue; clone.SetValue(Activator.CreateInstance(a.GetType()).InjectFrom<CloneInjection>(a), index); } return clone; } if (c.SourceProp.Type.IsGenericType) { //handle IEnumerable<> also ICollection<> IList<> List<> if (c.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Contains(typeof(IEnumerable))) { var t = c.SourceProp.Type.GetGenericArguments()[0]; if (t.IsValueType || t == typeof(string)) return c.SourceProp.Value; var tlist = typeof(List<>).MakeGenericType(t); var list = Activator.CreateInstance(tlist); var addMethod = tlist.GetMethod("Add"); foreach (var o in c.SourceProp.Value as IEnumerable) { var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o); addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e); } return list; } //unhandled generic type, you could also return null or throw return c.SourceProp.Value; } //for simple object types create a new instace and apply the clone injection on it return Activator.CreateInstance(c.SourceProp.Type) .InjectFrom<CloneInjection>(c.SourceProp.Value); }
protected override bool Match(ConventionInfo c) { if (c.SourceProp.Name != c.TargetProp.Name) { return false; } if (c.SourceProp.Value == null) { return false; } if (c.SourceProp.Type == typeof(int)) { if(c.SourceProp.Value.Equals(0)){return false;} return true; } if (c.SourceProp.Type == typeof(DateTime)) { if (c.SourceProp.Value.Equals(default(DateTime))) { return false; } return true; } //return c.SourceProp.Value != null; return true; }
protected override object SetValue(ConventionInfo conventionInfo) { if (conventionInfo.SourceProp.Type == conventionInfo.TargetProp.Type) return conventionInfo.SourceProp.Value; if (conventionInfo.SourceProp.Type.IsArray) { var array = (Array)conventionInfo.SourceProp.Value; var clone = (Array)array.Clone(); for (var index = 0; index < array.Length; index++) { var item = array.GetValue(index); if (!item.GetType().IsValueType && !(item is string)) { clone.SetValue(Activator.CreateInstance(item.GetType()).InjectFrom<CloneInjection>(item), index); } } return clone; } if (conventionInfo.SourceProp.Type.IsGenericType) { var genericInterfaces = conventionInfo.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces(); if (genericInterfaces.Any(d => d == typeof(IEnumerable))) { return MapLists(conventionInfo); } if (genericInterfaces.Any(i => i == typeof(ILazyLoaded))) { return MapLazy(conventionInfo); } //unhandled generic type, you could also return null or throw return conventionInfo.SourceProp.Value; } //for simple object types create a new instace and apply the clone injection on it return Activator.CreateInstance(conventionInfo.TargetProp.Type) .InjectFrom<CloneInjection>(conventionInfo.SourceProp.Value); }
protected override object SetValue(ConventionInfo c) { if (c.TargetProp.Type.IsEnum) { var value = Enum.Parse(c.TargetProp.Type, c.SourceProp.Value.ToString(), true); c.TargetProp.Value = value; } if (c.TargetProp.Type == typeof(string) && c.SourceProp.Type.IsEnum) { c.TargetProp.Value = c.SourceProp.Value.ToString(); } else { if (!c.TargetProp.Type.IsEnum) { c.TargetProp.Value = c.SourceProp.Value; } } return c.TargetProp.Value; }
private static object MapLists(ConventionInfo conventionInfo) { var genericArgument = conventionInfo.TargetProp.Type.GetGenericArguments()[0]; if (genericArgument.IsValueType || genericArgument == typeof(string)) { return conventionInfo.SourceProp.Value; } var listType = typeof(List<>).MakeGenericType(genericArgument); var addMethod = listType.GetMethod("Add"); var result = Activator.CreateInstance(listType); foreach (var sourceItem in (IEnumerable)conventionInfo.SourceProp.Value) { var e = Activator.CreateInstance(genericArgument).InjectFrom<CloneInjection>(sourceItem); addMethod.Invoke(result, new[] {e }); } return result; }
protected override Object SetValue(ConventionInfo c) { Func<DateTime, DateTime> dateTimeToDateTime = clientDateTime => ConvertDateTimeFromClientToUtc(clientDateTime, _ClientTimeZoneInfo); Func<DateTime, DateTimeOffset> dateTimeToDateTimeOffset = clientDateTime => new DateTimeOffset(ConvertDateTimeFromClientToUtc(clientDateTime, _ClientTimeZoneInfo)); Func<DateTimeOffset, DateTimeOffset> dateTimeOffsetToDateTimeOffset = clientDateTime => new DateTimeOffset(ConvertDateTimeFromClientToUtc(clientDateTime, _ClientTimeZoneInfo)); Func<DateTimeOffset, DateTime> dateTimeOffsetToDateTime = clientDateTime => ConvertDateTimeFromClientToUtc(clientDateTime, _ClientTimeZoneInfo); if (c.SourceProp.Value == null) { return null; } if (c.SourceProp.Type.Equals(typeof(DateTime)) || c.SourceProp.Type.Equals(typeof(DateTime?))) { return c.TargetProp.Type.Equals(typeof(DateTime)) || c.TargetProp.Type.Equals(typeof(DateTime?)) ? (Object)dateTimeToDateTime((DateTime) c.SourceProp.Value) : (Object)dateTimeToDateTimeOffset((DateTime) c.SourceProp.Value); } return c.TargetProp.Type.Equals(typeof(DateTimeOffset)) || c.TargetProp.Type.Equals(typeof(DateTimeOffset?)) ? (Object)dateTimeOffsetToDateTimeOffset((DateTimeOffset)c.SourceProp.Value) : (Object)dateTimeOffsetToDateTime((DateTimeOffset)c.SourceProp.Value); }
protected override bool Match(ConventionInfo c) { return c.SourceProp.Name == c.TargetProp.Name && Nullable.GetUnderlyingType(c.SourceProp.Type) == c.TargetProp.Type; }
protected override bool Match(ConventionInfo c) { var result = AuditNames.Contains(c.TargetProp.Name); return result; }
protected override bool Match(ConventionInfo conventionInfo) { return conventionInfo.SourceProp.Name == conventionInfo.TargetProp.Name && conventionInfo.SourceProp.Value != null; }
protected override object SetValue(ConventionInfo c) { // don't override these values return c.TargetProp.Value; }
protected virtual object SetValue(ConventionInfo c) { return(c.SourceProp.Value); }
protected override bool Match(ConventionInfo c) { return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type && !ignores.Contains(c.SourceProp.Name); }
protected override bool Match(ConventionInfo c) { return c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == typeof(int) && c.TargetProp.Type.IsSubclassOf(typeof(Enum)); }
protected override Boolean Match(ConventionInfo c) { return c.Source.Type.Name.EndsWith("Dto", StringComparison.OrdinalIgnoreCase) && !c.Target.Type.Name.EndsWith("Dto", StringComparison.OrdinalIgnoreCase); }
protected override bool Match(ConventionInfo c) { return PropertyMatch(c.SourceProp, c.TargetProp); }
protected override bool Match(ConventionInfo c) { return (c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Value != null); }
protected override void Inject(object source, object target) { var sourceProps = source.GetProps(); var targetProps = target.GetProps(); var ci = new ConventionInfo { Source = { Type = source.GetType(), Value = source }, Target = { Type = target.GetType(), Value = target } }; for (var i = 0; i < sourceProps.Count(); i++) { var s = sourceProps.ElementAt(i); ci.SourceProp.Name = s.Name; ci.SourceProp.Value = s.GetValue(source, null); ci.SourceProp.Type = s.PropertyType; for(var j=0; j < targetProps.Count(); j++) { var t = targetProps.ElementAt(j); ci.TargetProp.Name = t.Name; ci.TargetProp.Value = t.GetValue(target, null); ci.TargetProp.Type = t.PropertyType; if (Match(ci)) t.SetValue(target, SetValue(ci), null); } } }
protected abstract bool Match(ConventionInfo c);
protected override object SetValue(ConventionInfo v) { return v.SourceProp.Value == null ? null : (v.SourceProp.Value as IEnumerable<Entity>).Select(o => o.Id); }