// Automagically guess the property relationships between various POCOs and create a delegate that will set them up public static Delegate GetAutoMapper(Type[] types) { // Build a key var combiner = new HashCodeCombiner("auto-mapping"); combiner.Each(types, (x, t) => x.AddType(t)); var key = combiner.GetCombinedHashCode(); return(AutoMappers.Get(key, () => { // Create a method var m = new DynamicMethod("poco_automapper", types[0], types, true); var il = m.GetILGenerator(); for (int i = 1; i < types.Length; i++) { bool handled = false; for (int j = i - 1; j >= 0; j--) { // Find the property var candidates = types[j].GetProperties().Where(p => p.PropertyType == types[i]).ToList(); if (candidates.Count == 0) { continue; } if (candidates.Count > 1) { throw new InvalidOperationException(string.Format("Can't auto join {0} as {1} has more than one property of type {0}", types[i], types[j])); } // Generate code var lblIsNull = il.DefineLabel(); il.Emit(OpCodes.Ldarg_S, j); // obj il.Emit(OpCodes.Ldnull); // obj, null il.Emit(OpCodes.Beq, lblIsNull); // If obj == null then don't set nested object il.Emit(OpCodes.Ldarg_S, j); // obj il.Emit(OpCodes.Ldarg_S, i); // obj, obj2 il.Emit(OpCodes.Callvirt, candidates[0].GetSetMethod(true)); // obj = obj2 il.MarkLabel(lblIsNull); handled = true; } if (!handled) { throw new InvalidOperationException(string.Format("Can't auto join {0}", types[i])); } } il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ret); // Cache it var del = m.CreateDelegate(Expression.GetFuncType(types.Concat(types.Take(1)).ToArray())); return del; })); }
protected override ObservableCollection <OneTypeDeProjetVM> LoadItems() { ObservableCollection <OneTypeDeProjetVM> Selection = new ObservableCollection <OneTypeDeProjetVM>(); foreach (C.TypeDeProjet item in Service.GetAll()) { Selection.Add(AutoMappers <C.TypeDeProjet, OneTypeDeProjetVM> .AutoMapper(item)); } return(Selection); }
public void Configuration(IAppBuilder app) { ConfigureAuth(app); // Set up Web API -- was in 4th lecture //app.UseWebApi(WebApiConfig.Register()); // Map models AutoMappers.Configure(); }
static async Task Main(string[] args) { maps = AutoMappers.InitMaps(); //await maps.SimpleMap(); //await maps.LeszarmazottMap(); //maps.ConverterMap(); //await maps.ResolverMap(); //maps.GenericMap(); await maps.ProjectToMap(); Console.ReadKey(); }
// Automagically guess the property relationships between various POCOs and create a delegate that will set them up public static object GetAutoMapper(Type[] types) { // Build a key var key = new ArrayKey <Type>(types); return(AutoMappers.Get(key, () => { // Create a method var m = new DynamicMethod("petapoco_automapper", types[0], types, true); var il = m.GetILGenerator(); for (int i = 1; i < types.Length; i++) { bool handled = false; for (int j = i - 1; j >= 0; j--) { // Find the property var candidates = from p in types[j].GetProperties() where p.PropertyType == types[i] select p; if (candidates.Count() == 0) { continue; } if (candidates.Count() > 1) { throw new InvalidOperationException(string.Format("Can't auto join {0} as {1} has more than one property of type {0}", types[i], types[j])); } // Generate code il.Emit(OpCodes.Ldarg_S, j); il.Emit(OpCodes.Ldarg_S, i); il.Emit(OpCodes.Callvirt, candidates.First().GetSetMethod(true)); handled = true; } if (!handled) { throw new InvalidOperationException(string.Format("Can't auto join {0}", types[i])); } } il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ret); // Cache it return m.CreateDelegate(Expression.GetFuncType(types.Concat(types.Take(1)).ToArray())); } )); }
public OneUtilisateurVM GetFiche() { C.Utilisateur User = Service.GetOne(AutoMappers <OneUtilisateurVM, C.Utilisateur> .AutoMapper(SelectedItem)); // Utiliser un médiator pour pouvoir afficher la ressource nécéssaire (qui sera la même pour tous. Chaque information ^Doit obtenir toute ! les informations sur une utilisateur)// return(AutoMappers <C.Utilisateur, OneUtilisateurVM> .AutoMapper(User)); }
public void Delete() { Service.Delete(AutoMappers <OneUtilisateurVM, C.Utilisateur> .AutoMapper(SelectedItem)); }
public void Delete() { Service.Delete(AutoMappers <OneProjetVM, C.Projet> .AutoMapper(SelectedItem)); }