/// <summary> /// Save revenue data from old database as deposit to new database. /// </summary> /// <param name="context">EF context for old database.</param> /// <param name="revenuesCount">Total revenue count in old database.</param> /// <param name="revenueMapper">Revenue to transaction mapper.</param> /// <param name="revenueIndex">Current processed revenue index.</param> /// <param name="revenue">New loaded revenue data.</param> /// <returns>Updated current processed revenue index.</returns> private int SaveDeposit( ProfitAndExpenseEntities context, int revenuesCount, ObjectsMapper <RevenueList, TransactionData> revenueMapper, int revenueIndex, ref TransactionData revenue) { RevenueProgressTextBlock.Text = string.Format("{0} / {1}", revenueIndex + 1, revenuesCount); RevenueProgressBar.Value = revenueIndex + 1; RevenueProgressPercentTextBlock.Text = string.Format("{0:0.00}%", 100 * RevenueProgressBar.Value / RevenueProgressBar.Maximum); UpdateStatus("Importing revenues..."); moneyClient.Deposit(userId, accountId, revenue.Date, revenue.Price, revenue.Quantity, revenue.CategoryId, revenue.Comment); if (revenueIndex < revenuesCount) { // read next revenue revenueIndex++; revenue = revenueIndex < revenuesCount ? GetRevenue(context, revenueMapper, revenueIndex) : null; } return(revenueIndex); }
public void ConvertGenericDemo1() { ObjectsMapper <Source, Destination> mapper = ObjectMapperManager .DefaultInstance .GetMapper <Source, Destination>( //new DefaultMapConfig() new CustomMapConfig() .ConvertGeneric( typeof(HashSet <>), typeof(List <>), //new DefaultCustomConverterProvider(typeof(HashSetToListConverter<>)) new CustomConverterProvider() ) ); Source source = new Source(); Destination destination = null; destination = mapper.Map(source); //Assert.IsNotNull(destination); //Assert.IsNotNull(destination.HashSetList); //Assert.AreEqual(3, destination.HashSetList.Count); //Assert.AreEqual(source.HashSetList.ElementAt(0), destination.HashSetList.ElementAt(0)); //Assert.AreEqual(source.HashSetList.ElementAt(1), destination.HashSetList.ElementAt(1)); //Assert.AreEqual(source.HashSetList.ElementAt(2), destination.HashSetList.ElementAt(2)); }
/// <summary> /// Save expense data from old database as withdrawal to new database. /// </summary> /// <param name="context">EF context for old database.</param> /// <param name="expensesCount">Total expense count in old database.</param> /// <param name="expenseMapper">Expense to transaction mapper.</param> /// <param name="expenseIndex">Current processed expense index.</param> /// <param name="expense">New loaded expense data.</param> /// <returns>Updated current processed expense index.</returns> private int SaveWithdrawal( ProfitAndExpenseEntities context, int expensesCount, ObjectsMapper <ExpenseList, TransactionData> expenseMapper, int expenseIndex, ref TransactionData expense) { ExpenseProgressTextBlock.Text = string.Format("{0} / {1}", expenseIndex + 1, expensesCount); ExpenseProgressBar.Value = expenseIndex + 1; ExpenseProgressPercentTextBlock.Text = string.Format("{0:0.00}%", 100 * ExpenseProgressBar.Value / ExpenseProgressBar.Maximum); UpdateStatus("Importing expenses..."); moneyClient.Withdrawal(userId, accountId, expense.Date, expense.Price, expense.Quantity, expense.CategoryId, expense.Comment); if (expenseIndex < expensesCount) { // read next expense expenseIndex++; expense = expenseIndex < expensesCount ? GetExpense(context, expenseMapper, expenseIndex) : null; } return(expenseIndex); }
/// <summary> /// 强制换 /// </summary> /// <typeparam name="From"></typeparam> /// <typeparam name="To"></typeparam> /// <param name="entity"></param> /// <returns></returns> public static To Convert <From, To>(From entity) { ObjectsMapper <From, To> mapper = ObjectMapperManager.DefaultInstance.GetMapper <From, To>(IgnoreCaseRule.NewInstance); return(mapper.Map(entity)); }
public static void Initialize() { emitMapper = ObjectMapperManager.DefaultInstance.GetMapper <B2, A2>( new DefaultMapConfig() .ConstructBy <A2.Int>(() => new A2.Int(0)) .NullSubstitution <decimal?, decimal>(state => 42) .NullSubstitution <bool?, bool>(state => true) .NullSubstitution <int?, int>(state => 42) .NullSubstitution <long?, long>(state => 42) .ConvertUsing <long, int>(value => (int)value + 1) .ConvertUsing <short, int>(value => (int)value + 1) .ConvertUsing <byte, int>(value => (int)value + 1) .ConvertUsing <decimal, int>(value => (int)value + 1) .ConvertUsing <float, int>(value => (int)value + 1) .ConvertUsing <char, int>(value => (int)value + 1) ); AutoMapper.Mapper.CreateMap <B2.Int, A2.Int>().ConstructUsing(s => new A2.Int(0)); AutoMapper.Mapper.CreateMap <long, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <short, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <byte, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <decimal, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <float, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <char, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap <B2, A2>() .ForMember(s => s.nullable1, opt => opt.NullSubstitute((decimal)42)) .ForMember(s => s.nullable2, opt => opt.NullSubstitute(true)) .ForMember(s => s.nullable3, opt => opt.NullSubstitute(42)) .ForMember(s => s.nullable4, opt => opt.NullSubstitute((long)42)); }
public static ProductViewModel ToViewModel(this Product product) { //AutoMapper,EmitMapper性能比拼,EmitMapper比AutoMapper快十倍 /* * using (MiniProfiler.Current.Step("AutoMapper()")) * { * for (int i = 0; i < 10000; i++) * { * Mapper.DynamicMap<Product, ProductViewModel>(product); * } * } * using (MiniProfiler.Current.Step("EmitMapper()")) * { * for (int i = 0; i < 10000; i++) * { * ObjectsMapper<Product, ProductViewModel> mapper = ObjectMapperManager.DefaultInstance.GetMapper<Product, ProductViewModel>(); * ProductViewModel dst = mapper.Map(product); * } * } */ ObjectsMapper <Product, ProductViewModel> mapper = ObjectMapperManager.DefaultInstance.GetMapper <Product, ProductViewModel>(); return(mapper.Map(product)); }
public void MapFrom_Config_Into_Obj_With_List_of_ParticularType_Prop() { var config = new Dictionary <string, object> { { "Property1", "Value1" }, { "Property Two", "Value2" }, { "Property3", new [] { new { Id = 11, Name = "Property31" }, new { Id = 12, Name = "Property32" }, } } }; var result = ObjectsMapper.MapInto <OtherClassEnumTyped>(config); // TODO: like below //var result = _mapper.MapInto<OtherClassEnumTyped>(config); Assert.Equal("Value1", result.Property1); Assert.Equal("Value2", result.Property2); Assert.Equal(11, result.Property3.First().Id); Assert.Equal(12, result.Property3.Last().Id); Assert.Equal("Property31", result.Property3.First().Name); Assert.Equal("Property32", result.Property3.Last().Name); }
public void Assign_Complex_With_Two_Items_In_List() { var config = new Dictionary <string, object> { { "Property1", "Value1" }, { "Property Two", "2" }, { "Property3", new List <object> { new { Id = 31, Name = "Property3_1" }, new { Id = 32, Name = "Property3_2" }, } } }; var result = ObjectsMapper.MapInto <OtherClassEnumTyped>(config); // TODO: like below //var result = _mapper.MapInto<OtherClassEnumTyped>(config); Assert.Equal("Value1", result.Property1); Assert.Equal("2", result.Property2); Assert.Equal("Property3_1", result.Property3.First().Name); Assert.Equal(31, result.Property3.First().Id); Assert.Equal("Property3_2", result.Property3.Last().Name); Assert.Equal(32, result.Property3.Last().Id); }
public TTo MapTo <TFrom, TTo>(TFrom from) { ObjectsMapper <TFrom, TTo> mapper = ObjectMapperManager.DefaultInstance .GetMapper <TFrom, TTo>(new CommonMapingConfig()); return(mapper.Map(from)); }
public static TDestination Convert(TSource source) { ObjectsMapper <TSource, TDestination> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TDestination>(); TDestination destination = mapper.Map(source); return(destination); }
/// <summary> /// 将TFrom模型属性映射到TTo模型,未映射字段属性不变 /// </summary> public static TTo Mapper <TFrom, TTo>(TFrom from, TTo tto) where TFrom : class { var mapConfig = new DefaultMapConfig(); var ignores = new List <string>(); //初始化忽略映射的属性 typeof(TFrom).GetProperties().ForEach(p => { if (p.GetCustomAttributes(typeof(IgnoreMapperAttribute), false).Length > 0) { ignores.Add(p.Name); } }); typeof(TTo).GetProperties().ForEach(p => { if (p.GetCustomAttributes(typeof(IgnoreMapperAttribute), false).Length > 0) { ignores.Add(p.Name); } }); mapConfig.IgnoreMembers <TFrom, TTo>(ignores.ToArray()); ObjectsMapper <TFrom, TTo> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TFrom, TTo>(mapConfig); return(mapper.Map(from, tto)); }
/// <summary> /// 用自定义映射规则进行模型映射 /// </summary> /// <param name="from"></param> /// <param name="config">映射规则</param> public static TTo Mapper <TFrom, TTo>(TFrom from, IMappingConfigurator mapConfig) where TFrom : class { var ignores = new List <string>(); //初始化忽略映射的属性 typeof(TFrom).GetProperties().ForEach(p => { if (p.GetCustomAttributes(typeof(IgnoreMapperAttribute), false).Length > 0) { ignores.Add(p.Name); } }); typeof(TTo).GetProperties().ForEach(p => { if (p.GetCustomAttributes(typeof(IgnoreMapperAttribute), false).Length > 0) { ignores.Add(p.Name); } }); if (mapConfig != null && mapConfig is MapConfigBase <TFrom> ) { (mapConfig as MapConfigBase <TFrom>).IgnoreMembers <TFrom, TTo>(ignores.ToArray()); } ObjectsMapper <TFrom, TTo> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TFrom, TTo>(mapConfig); return(mapper.Map(from)); }
/// <summary> /// 实体(模型)转换 /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="source"></param> /// <returns></returns> public static TResult EntityMap <TSource, TResult>(TSource source) { ObjectsMapper <TSource, TResult> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TResult>(); return(mapper.Map(source)); }
/// <summary> /// Maps the specified from. /// </summary> /// <typeparam name="TFrom">The type of from.</typeparam> /// <typeparam name="TTo">The type of to.</typeparam> /// <param name="from">The object from.</param> /// <returns>The mapped object.</returns> public virtual TTo Map <TFrom, TTo>(TFrom @from) { AssertCore.ArgumentNotNull(@from, "@from"); ObjectsMapper <TFrom, TTo> mapper = this.GetMapper <TFrom, TTo>(); return(mapper.Map(@from)); }
public static void Initialize() { emitMapper = ObjectMapperManager.DefaultInstance.GetMapper <BenchSource, BenchDestination>(); AutoMapper.Mapper.CreateMap <BenchSource.Int1, BenchDestination.Int1>(); AutoMapper.Mapper.CreateMap <BenchSource.Int2, BenchDestination.Int2>(); AutoMapper.Mapper.CreateMap <BenchSource, BenchDestination>(); }
public static TDestEntity MapFrom <TSourceEntity, TDestEntity>(this TDestEntity obj, TSourceEntity source, params string[] members) { ObjectsMapper <TSourceEntity, TDestEntity> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TSourceEntity, TDestEntity>( new DefaultMapConfig().IgnoreMembers <TSourceEntity, TDestEntity>(members)); TDestEntity dst = mapper.Map(source, obj); return(dst); }
/// <summary> /// Maps the collection. /// </summary> /// <typeparam name="TFrom">The type of from.</typeparam> /// <typeparam name="TTo">The type of to.</typeparam> /// <param name="from">The from objects collection.</param> /// <returns>The output mapped collection.</returns> public virtual IEnumerable <TTo> MapCollection <TFrom, TTo>(IEnumerable <TFrom> @from) { AssertCore.ArgumentNotNullOrEmpty(@from, "@from"); ObjectsMapper <TFrom, TTo> mapper = this.GetMapper <TFrom, TTo>(); return(mapper.MapEnum(@from)); }
public TTarget Convert <TSource, TTarget>(TSource source, dynamic config) where TTarget : class, new() where TSource : class { ObjectsMapper <TSource, TTarget> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TTarget>(config); return(mapper.Map(source)); }
public TTarget Convert <TSource, TTarget>(TSource source, TTarget target) where TTarget : class where TSource : class { ObjectsMapper <TSource, TTarget> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TSource, TTarget>(new DefaultMapConfig()); return(mapper.Map(source, target)); }
static void Main(string[] args) { Sourse src = new Sourse { A = 1, B = 10M, C = DateTime.Parse("2011/9/21 0:00:00"), //D = new Inner //{ // D2 = Guid.NewGuid() //}, E = "test" }; Dest dst = null; //dst = mapper.Map(src); //Console.WriteLine(dst.A); //Console.WriteLine(dst.B); //Console.WriteLine(dst.C); ////Console.WriteLine(dst.D.D1); ////Console.WriteLine(dst.D.D2); //Console.WriteLine(dst.F); Stopwatch sw = new Stopwatch(); int seed = 500000; ObjectsMapper <Sourse, Dest> mapper = ObjectMapperManager.DefaultInstance.GetMapper <Sourse, Dest>(); var mapperImpl = mapper.MapperImpl; sw.Restart(); for (var i = 0; i < seed; i++) { mapper = ObjectMapperManager.DefaultInstance.GetMapper <Sourse, Dest>(); //mapper = new ObjectsMapper<Sourse, Dest>(mapperImpl); dst = mapper.Map(src); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); dst = ModelConverter.ConvertObject <Sourse, Dest>(src); sw.Restart(); for (var i = 0; i < seed; i++) { dst = ModelConverter.ConvertObject <Sourse, Dest>(src); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); sw.Restart(); for (var i = 0; i < seed; i++) { dst = ConvertModel <Sourse, Dest>(src); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); }
public static void RegisterBLLTypes(this IUnityContainer container) { container .RegisterType <UserService>() .RegisterType <CategoryService>() .RegisterType <ItemService>() .RegisterType <ReviewService>() .RegisterInstance <IMapper>(ObjectsMapper.CreateMapper()); //, InstanceLifetime.Singleton }
/// <summary> /// Load one revenue data from old SQL database. /// </summary> /// <param name="context">EF context for database.</param> /// <param name="revenueMapper">EmitMapper that maps old revenue data object to new <see cref="TransactionData"/>.</param> /// <param name="skip">How many records to skip from the beginning of the whole "TblRevenueList" table.</param> /// <returns>New transaction data instance filled with data from record in table "TblRevenueList" of the old SQL database.</returns> private static TransactionData GetRevenue( ProfitAndExpenseEntities context, ObjectsMapper <RevenueList, TransactionData> revenueMapper, int skip) { return(revenueMapper.Map(context.TblRevenueLists .OrderBy(list => list.CreationDate) .Skip(skip) .First())); }
public bool TryConvert <S, T>(S s, ref T t) { try { ObjectsMapper <S, T> mapper = _provider.GetMapper <S, T>(); t = mapper.Map(s, t); } catch { return(false); } return(true); }
public static void RegisterBLLTypes(this IUnityContainer container) { container .RegisterType <IAuthenticationService, AuthenticationService>() .RegisterType <IUserService, UserService>() .RegisterType <IRoleService, RoleService>() .RegisterType <IProductService, ProductService>() .RegisterType <IOrderService, OrderService>() .RegisterType <ISupplierService, SupplierService>() .RegisterType <ISuppToProdService, SuppToProdService>() .RegisterInstance <IMapper>(ObjectsMapper.CreateMapper()); //, InstanceLifetime.Singleton }
public void MapInto_Particular_Obj_From_Dynamic() { var resource = new { Id = 11, Name = "Property3" }; var result = ObjectsMapper.MapInto <OtherInner>(resource); Assert.Equal(11, result.Id); Assert.Equal("Property3", result.Name); }
public void MapInto_List_dynamic() { var source = new [] { new { Id = 11, Name = "Property31" }, new { Id = 12, Name = "Property32" } }; var result = ObjectsMapper.MapIntoListOf <OtherInner>(source); Assert.True(result.Count() == 2); Assert.True(result.First().Id == 11); Assert.True(result.Last().Id == 12); }
public void ConvertUsingDemo() { Source1 source = new Source1 { Price = 2 }; ObjectsMapper <Source1, Destination1> mapper = ObjectMapperManager.DefaultInstance.GetMapper <Source1, Destination1>( new DefaultMapConfig() .ConvertUsing <object, decimal>(v => 50.1M) ); Destination1 destination = mapper.Map(source); }
public void Test_Derived() { ObjectsMapper <IDerived, Target> mapper = ObjectMapperManager.DefaultInstance.GetMapper <IDerived, Target>(); Derived source = new Derived { BaseProperty = "base", DerivedProperty = "derived" }; Target destination = mapper.Map(source); Assert.Equal("base", destination.BaseProperty); Assert.Equal("derived", destination.DerivedProperty); }
public void MapInto_List_Dictionary_Anonymous() { var source = new Dictionary <string, object> { { "First", new { Id = 11, Name = "Property31" } }, { "Second", new { Id = 12, Name = "Property32" } } }; var result = ObjectsMapper.MapIntoListOf <OtherInner>(source); Assert.True(result.Count() == 2); Assert.True(result.First().Id == 11); Assert.Equal("Property31", result.First().Name); Assert.True(result.Last().Id == 12); Assert.Equal("Property32", result.Last().Name); }
/// <summary> /// 复制对象中值到目标对象 /// </summary> /// <typeparam name="T">目标类类型</typeparam> /// <typeparam name="TModel">原类类型</typeparam> /// <param name="model">原类类型对象</param> /// <param name="entity">目标对象</param> /// <returns>目标类</returns> public static T CopyToModel <T, TModel>(TModel model, T entity, bool formatDictionary) where T : new() { try { ObjectsMapper <TModel, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper <TModel, T>( new DefaultMapConfig().ConvertUsing <String, DateTime>(o => String.IsNullOrEmpty(o) ? new DateTime() : Convert.ToDateTime(o))); entity = mapper.Map(model); return(entity); } catch (Exception ex) { throw ex; } }
public static void Initialize() { emitMapper = ObjectMapperManager.DefaultInstance.GetMapper<B2, A2>( new DefaultMapConfig() .ConstructBy<A2.Int>( () => new A2.Int(0) ) .NullSubstitution<decimal?, decimal>(state => 42) .NullSubstitution<bool?, bool>(state => true) .NullSubstitution<int?, int>(state => 42) .NullSubstitution<long?, long>(state => 42) .ConvertUsing<long, int>( value => (int)value + 1) .ConvertUsing<short, int>(value => (int)value + 1) .ConvertUsing<byte, int>(value => (int)value + 1) .ConvertUsing<decimal, int>(value => (int)value + 1) .ConvertUsing<float, int>(value => (int)value + 1) .ConvertUsing<char, int>(value => (int)value + 1) ); AutoMapper.Mapper.CreateMap<B2.Int, A2.Int>().ConstructUsing(s => new A2.Int(0)); AutoMapper.Mapper.CreateMap<long, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<short, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<byte, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<decimal, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<float, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<char, int>().ConstructUsing(s => (int)s + 1); AutoMapper.Mapper.CreateMap<B2, A2>() .ForMember(s => s.nullable1, opt => opt.NullSubstitute((decimal)42)) .ForMember(s => s.nullable2, opt => opt.NullSubstitute(true)) .ForMember(s => s.nullable3, opt => opt.NullSubstitute(42)) .ForMember(s => s.nullable4, opt => opt.NullSubstitute((long)42)); }
public static void Initialize() { emitMapper = ObjectMapperManager.DefaultInstance.GetMapper<B2, A2>(); AutoMapper.Mapper.CreateMap<B2, A2>(); AutoMapper.Mapper.CreateMap<char, int>(); }
static ImageService() { _imageMapper_ptov = ObjectMapperManager.DefaultInstance.GetMapper<Image, ImageRequest>(); }
public static void Initialize() { emitMapper = ObjectMapperManager.DefaultInstance.GetMapper<BenchSource, BenchDestination>(); AutoMapper.Mapper.CreateMap<BenchSource.Int1, BenchDestination.Int1>(); AutoMapper.Mapper.CreateMap<BenchSource.Int2, BenchDestination.Int2>(); AutoMapper.Mapper.CreateMap<BenchSource, BenchDestination>(); }
static AdminService() { _productMapper_vtop = ObjectMapperManager.DefaultInstance.GetMapper<ProductRequest, Product>(); _productMapper_ptov = ObjectMapperManager.DefaultInstance.GetMapper<Product, ProductRequest>(); }