Tag: automapper 4

如何在ASP.Net webapp中引用的项目DLL中初始化AutoMapper配置文件

在我的项目类库(dll)中如何使用automapper苦苦挣扎。 请参阅下面我的整体解决方案的结构。 WebApp启动,在Global.asax App Start中,调用AutoMapper.Configure()方法以添加映射配置文件。 现在我只是添加Services.AutoMapperViewModelProfile。 但我需要以某种方式考虑每个WebStoreAdapters中的配置文件(下例中的BigCommerce和Shopify)。 我希望不要在WebApp中添加对每个WebStoreAdapter的引用,只是为了能够在AutoMapperConfig中添加配置文件。 如果我在WebStoreFactory中添加对AutoMapper.Initialize的另一个调用,它将覆盖WebApp中的一个。 还有其他方式,我错过或完全偏离这里以其他方式? WebApp – AutoMapperConfig – AddProfile Services.AutoMapperViewModelProfile Services.dll – AutoMapperViewModelProfile Scheduler.dll (uses HangFire to execute cron jobs to get data from shop carts. Its UI is accessed via the WebApp) WebStoreAdapter.dll -WebStoreFactory BigCommerceAdapter.dll – AutoMapperBigCommerceDTOProfile ShopifyAdapter.dll – AutoMapperShopifyDTOProfile 从Global.asax调用初始化: public static class AutoMapperConfiguration { public static […]

使用AutoMapper合并对象

我正在尝试使用AutoMapper来合并来自多个对象的数据,而且我遇到了一些我似乎无法解决的问题。 我有一个像这样的对象: public class Parent { public string Id { get; set; } public List Children { get; set; } } public class Child { public string Key { get; set; } public int? Value1 { get; set; } public int? Value2 { get; set; } public int? Value3 { get; set; } public int? […]

使用AutoMapper 4.2和Autofac无法解析AutoMapper.IMapper

我尝试过各种各样的排列,但我目前的配置(因为它与AutoMapper有关)是这样的: builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As(); builder.Register(c => new MapperConfiguration(cfg => { foreach (var profile in c.Resolve<IEnumerable>()) { cfg.AddProfile(profile); } })).AsSelf().SingleInstance(); builder.Register(c => c.Resolve().CreateMapper(c.Resolve)).As().InstancePerLifetimeScope(); builder.RegisterType().As(); 我有一个使用IMapper mapper的构造函数,但是我继续得到YSOD: None of the constructors found with’Autofac.Core.Activators.Reflection.DefaultConstructorFinder’ on type ” can be invoked with the available services and parameters: Cannot resolve parameter ‘AutoMapper.IMapper mapper’ of constructor ‘Void .ctor(…,…,…, AutoMapper.IMapper)’. 这个类在没有automapper引用的情况下工作得很好,所以我确定麻烦在于我的automapper配置。 我不确定我在这里缺少什么,因为我对AutoFac和AutoMapper都很陌生。 编辑: 我也尝试过: […]

Automapper – inheritance映射器不使用类型转换器

不能一起使用Mapping Inheritance和TypeConverter。 我有这个代码: /* BaseClassTypeConverter.cs */ public class BaseClassTypeConverter : ITypeConverter { public BaseClass Convert(ResolutionContext context) { if (context == null || context.IsSourceValueNull) return null; var src = (SourceClass)context.SourceValue; return new BaseClass() { CommonAttr = src.SourceAttr }; } } /* AutoMapperConfig.cs */ public static class AutoMapperConfig { public static void RegisterMappings() { AutoMapper.Mapper.Initialize(config => { […]

将平面JSON / Dictionary映射到模型(包含子类)

我想将一个扁平的json字符串转换为一个模型,目标类具有子类,而扁平的json具有带前缀的所有子类对象; 比如“{classname}。{property}”。 { “FirstName”: “Joey”, “LastName”: “Billy”, “EmploymentDetails.JobTitle”: “JobTitle”, “EmploymentDetails.StartDate”: “2015-01-01T00:00:00”, “ContactDetails.HouseNumberName”: “10”, “ContactDetails.Road”: “Road” } 这是我的目的地类: public class Person { public string FirstName { get; set; } public string LastName { get; set; } public virtual EmploymentDetails EmploymentDetails { get;set;} public virtual ContactDetails ContactDetails { get;set;} } public class EmploymentDetails { public string JobTitle […]