public void AddOrUpdate(UpdateOwnerInput input) { var config = new ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers); var mapper = new MappingEngine(config); config.CreateMap<OwnerDto, Owner>().ConstructUsing(model => { if (model.Id == 0) { Owner toAdd = new Owner(); _ownerRepository.Insert(toAdd); return toAdd; } else { return _ownerRepository.Get(model.Id); } }); config.CreateMap<OwnerLandPropertyDto, LandProperty>().ConstructUsing(model => { return _landPropertyRepository.Get(model.Id); }); try { mapper.Map<OwnerDto, Owner>(input.OwnerToUpdate); } catch (Exception e) { throw e; } }
/// <summary> /// Used to setup AutoMapper /// </summary> /// <param name="additionalProfiles">These profiles will override any existing mappings in an additive manner. Base and ProjectTo are applied automatically.</param> /// <exception cref="AutoMapperConfigurationException">Thrown the mapping configuration is invalid - check the logs</exception> public static void InitializeMappers(params Profile[] additionalProfiles) { if (!hasInitialized) { hasInitialized = true; } else { throw new AutoMapperConfigurationException("InitializeMappers should only be called once during the lifetime of an application."); } // Setup Mapper Mapper.AddProfile(Api); if (additionalProfiles != null) { foreach (Profile additionalProfile in additionalProfiles) { Mapper.AddProfile(additionalProfile); } } // Verify Mapper configuration Mapper.AssertConfigurationIsValid(); // Setup ProjectToMapper ConfigurationStore getAllConfig = CreateConfiguration(); ApiProjectToMapper = CreateMapper(getAllConfig); getAllConfig.AddProfile(Api); getAllConfig.AddProfile(ApiProjectTo); }
public void AddOrUpdate(GetMortgageInput input) { var config = new ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers); var mapper = new MappingEngine(config); config.CreateMap<MortgageDto, Mortgage>().ConstructUsing(model => { if (model.Id == 0) { Mortgage toAdd = new Mortgage(); _mortgageRepository.Insert(toAdd); return toAdd; } else { return _mortgageRepository.Get(model.Id); } }).ForMember(x => x.LandProperty, o => o.Ignore()); try { mapper.Map<MortgageDto, Mortgage>(input.MortgageToUpdate); } catch (Exception e) { throw e; } }
public MappingEngineProvider(Engine engine) { var config = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers()); GetMappingTypesTaggedWith(engine) .Select(type => (IMapping) Activator.CreateInstance(type)) .ToList().ForEach(config.Add); _engine = new MappingEngine(config); }
public static IMappingEngine CreateEngine(Profile mappingProfile) { ConfigurationStore store = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers); store.AssertConfigurationIsValid(); MappingEngine engine = new MappingEngine(store); store.AddProfile(mappingProfile); return engine; }
public BaseController() : base() { AnonymousManagersProvider = new ManagersProvider(); IPlatformSpecificMapperRegistry platformSpecificRegistry = PlatformAdapter.Resolve<IPlatformSpecificMapperRegistry>(true); platformSpecificRegistry.Initialize(); var store = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers); _mapper = new MappingEngine(store); }
public static MappingEngine CreateMappingEngine() { var mapperConfig = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()); new ProductMapper().ConfigureMap(mapperConfig); new CustomerMapper().ConfigureMap(mapperConfig); var mapper = new MappingEngine(mapperConfig); Mapper.AssertConfigurationIsValid(); return mapper; }
public void AddOrUpdate(UpdateLandPropertyInput input) { var config = new ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers); var mapper = new MappingEngine(config); config.CreateMap<LandPropertyDto, LandProperty>().ConstructUsing(model => { if (model.Id == 0) { LandProperty toAdd = new LandProperty(); _landPropertyRepository.InsertAsync(toAdd); return toAdd; } else { return _landPropertyRepository.Get(model.Id); } }); config.CreateMap<LandPropertyOwnerDto, Owner>().ConstructUsing(model => { return _ownerRepository.Get(model.Id); }); config.CreateMap<MortgageDto, Mortgage>().ConstructUsing(model => { if (model.MortgageIdentifier > 0 && model.Id == 0) { Mortgage toAdd = new Mortgage(); _mortgageRepository.Insert(toAdd); return toAdd; } else if (model.Id > 0) { return _mortgageRepository.Get(model.Id); } else { return null; } }).ForMember(x => x.LandProperty, o => o.Ignore()); try { mapper.Map<LandPropertyDto, LandProperty>(input.LandPropToUpdate); } catch (Exception e) { throw e; } }
public void GetAllCustomers_MissingMappingConfiguration_AutoMapperMappingException() { //Arrange var configurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers); IMappingEngine mappingEngine = new MappingEngine(configurationStore); IStorage storage = new MemoryStorage(); ICustomerRepository customerRepository = new CustomerRepository(mappingEngine, storage); //Act IEnumerable<CustomerDomain> actualCustomers = customerRepository.GetAllCustomers(); //Assert Mapper.AssertConfigurationIsValid(); CollectionAssert.AreEqual(GetExpectedCustomers().ToList(), actualCustomers.ToList()); }
public JsonMapper() { var configurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers); this.mapper = new MappingEngine(configurationStore); configurationStore.CreateMap<Feature, JsonFeature>() .ForMember(t => t.FeatureElements, opt => opt.ResolveUsing(s => s.FeatureElements)) .AfterMap( (sourceFeature, targetFeature) => { foreach (var featureElement in targetFeature.FeatureElements.ToArray()) { featureElement.Feature = targetFeature; } }); configurationStore.CreateMap<Example, JsonExample>(); configurationStore.CreateMap<Keyword, JsonKeyword>(); configurationStore.CreateMap<Scenario, JsonScenario>() .ForMember(t => t.Feature, opt => opt.Ignore()); configurationStore.CreateMap<ScenarioOutline, JsonScenarioOutline>() .ForMember(t => t.Feature, opt => opt.Ignore()); configurationStore.CreateMap<Step, JsonStep>(); configurationStore.CreateMap<Table, JsonTable>(); configurationStore.CreateMap<TestResult, JsonTestResult>().ConstructUsing(ToJsonTestResult); configurationStore.CreateMap<TableRow, JsonTableRow>() .ConstructUsing(row => new JsonTableRow(row.Cells.ToArray())); configurationStore.CreateMap<IFeatureElement, IJsonFeatureElement>().ConvertUsing( sd => { var scenario = sd as Scenario; if (scenario != null) { return this.mapper.Map<JsonScenario>(scenario); } var scenarioOutline = sd as ScenarioOutline; if (scenarioOutline != null) { return this.mapper.Map<JsonScenarioOutline>(scenarioOutline); } throw new ArgumentException("Only arguments of type Scenario and ScenarioOutline are supported."); }); }
private void AssertConfigurationIsValid(IEnumerable <TypeMap> typeMaps) { Seal(); var maps = typeMaps as TypeMap[] ?? typeMaps.ToArray(); var badTypeMaps = (from typeMap in maps where typeMap.ShouldCheckForValid() let unmappedPropertyNames = typeMap.GetUnmappedPropertyNames() where unmappedPropertyNames.Length > 0 select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames) ).ToArray(); if (badTypeMaps.Any()) { throw new AutoMapperConfigurationException(badTypeMaps); } var typeMapsChecked = new List <TypeMap>(); var configExceptions = new List <Exception>(); var engine = new MappingEngine(this, CreateMapper()); foreach (var typeMap in maps) { try { DryRunTypeMap(typeMapsChecked, new ResolutionContext(typeMap, null, typeMap.SourceType, typeMap.DestinationType, new MappingOperationOptions(), engine)); } catch (Exception e) { configExceptions.Add(e); } } if (configExceptions.Count > 1) { throw new AggregateException(configExceptions); } if (configExceptions.Count > 0) { throw configExceptions[0]; } }
static void Main(string[] args) { AutoMapper.Mappers.MapperRegistry.Reset(); var autoMapperCfg = new AutoMapper.ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers); var mappingEngine = new AutoMapper.MappingEngine(autoMapperCfg); autoMapperCfg.Seal(); autoMapperCfg.CreateMap <Entity1, Entity2>().ReverseMap(); var a1 = new Entity1(); var b1 = mappingEngine.Map <Entity2>(a1); var colEntity1 = new List <Entity1> { new Entity1() { DepId = 1, Name = "abc" }, new Entity1() { DepId = 2, Name = "qwe" }, new Entity1() { DepId = 3, Name = "qaz" } }; var colEntity2 = new List <Entity2> { new Entity2() { DepId = 2, Name = "newNameFirst" }, new Entity2() { DepId = 3, Name = "newNameSecond" }, new Entity2() { DepId = 4, Name = "newName" } }; //Mapper.Initialize(cfg => cfg.CreateMap<Entity1, Entity2>()); CompareCollections <Entity1, Entity2> .Compare(colEntity1, colEntity2, x => x.DepId); }
public BundleConverter() { _mapper = CreateMapper(); }
public GameMapperConfig() { Engine = new MappingEngine(CreateConfig()); }
public static void DestructMappers() { Mapper.Reset(); ApiProjectToMapper = null; hasInitialized = false; }
public Mapper(string featureLanguage = LanguageServices.DefaultLanguage) { var configurationStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers); this.mapper = new MappingEngine(configurationStore); configurationStore.CreateMap<string, Keyword>().ConvertUsing(new KeywordResolver(featureLanguage)); configurationStore.CreateMap<G.TableCell, string>() .ConstructUsing(cell => cell.Value); configurationStore.CreateMap<G.TableRow, TableRow>() .ConstructUsing(row => new TableRow(row.Cells.Select(this.mapper.Map<string>))); configurationStore.CreateMap<G.DataTable, Table>() .ForMember(t => t.HeaderRow, opt => opt.MapFrom(dt => dt.Rows.Take(1).Single())) .ForMember(t => t.DataRows, opt => opt.MapFrom(dt => dt.Rows.Skip(1))); configurationStore.CreateMap<G.DocString, string>().ConstructUsing(docString => docString.Content); configurationStore.CreateMap<G.Step, Step>() .ForMember(t => t.NativeKeyword, opt => opt.MapFrom(s => s.Keyword)) .ForMember(t => t.Name, opt => opt.MapFrom(s => s.Text)) .ForMember(t => t.DocStringArgument, opt => opt.MapFrom(s => s.Argument is G.DocString ? s.Argument : null)) .ForMember(t => t.TableArgument, opt => opt.MapFrom(s => s.Argument is G.DataTable ? s.Argument : null)); configurationStore.CreateMap<G.Tag, string>() .ConstructUsing(tag => tag.Name); configurationStore.CreateMap<G.Scenario, Scenario>() .ForMember(t => t.Description, opt => opt.NullSubstitute(string.Empty)); configurationStore.CreateMap<IEnumerable<G.TableRow>, Table>() .ForMember(t => t.HeaderRow, opt => opt.MapFrom(s => s.Take(1).Single())) .ForMember(t => t.DataRows, opt => opt.MapFrom(s => s.Skip(1))); configurationStore.CreateMap<G.Examples, Example>() .ForMember(t => t.TableArgument, opt => opt.MapFrom(s => ((G.IHasRows)s).Rows)); configurationStore.CreateMap<G.ScenarioOutline, ScenarioOutline>() .ForMember(t => t.Description, opt => opt.NullSubstitute(string.Empty)); configurationStore.CreateMap<G.Background, Scenario>() .ForMember(t => t.Description, opt => opt.NullSubstitute(string.Empty)); configurationStore.CreateMap<G.ScenarioDefinition, IFeatureElement>().ConvertUsing( sd => { var scenario = sd as G.Scenario; if (scenario != null) { return this.mapper.Map<Scenario>(scenario); } var scenarioOutline = sd as G.ScenarioOutline; if (scenarioOutline != null) { return this.mapper.Map<ScenarioOutline>(scenarioOutline); } throw new ArgumentException("Only arguments of type Scenario and ScenarioOutline are supported."); }); configurationStore.CreateMap<G.Feature, Feature>() .ForMember(t => t.FeatureElements, opt => opt.ResolveUsing(s => s.ScenarioDefinitions)) .AfterMap( (sourceFeature, targetFeature) => { foreach (var featureElement in targetFeature.FeatureElements.ToArray()) { featureElement.Feature = targetFeature; } if (targetFeature.Background != null) { targetFeature.Background.Feature = targetFeature; } }); }