0

I am developing webapi project and extensively using Automapper to map objects. Currently i am using Mapper.Map

Can anyone direct me how to implement this? How to call this configure method while registering IMappingEngine in Unity?

My current automapper mapping:

public static class AutomapperConfiguration { public static void Configure() { Mapper.Initialize(cfg => { cfg.ConstructServicesUsing(type => UnityConfig.GetConfiguredContainer().Resolve(type)); cfg.CreateMap<Logger, Log>() .ForMember(d => d.TxId, opt => opt.ResolveUsing<AsRunTxIdValueResolver>()); } } } 

Thanks

1 Answer 1

1

You can use automapper profiles to create various mappers and register all profiles like this:

public static class AutomapperConfiguration { public static MapperConfiguration MyMapperConfiguration; public static void Configure() { MyMapperConfiguration = new MapperConfiguration(cfg => { var types = typeof(Program).Assembly.GetTypes(); var profiles = types.Where(x => x.IsSubclassOf(typeof(Profile))) .Select(Activator.CreateInstance) .OfType<Profile>() .ToList(); profiles.Each(p => cfg.AddProfile(p)); }); MapperConfiguration.AssertConfigurationIsValid(); } } 

Your automapper profile class will look like this:

public class LoggerProfile : Profile { protected override void Configure() { CreateMap<Logger,Log>() .ForMember(d => d.TxId, opt => opt.ResolveUsing<AsRunTxIdValueResolver>()); } } 

In your startup.cs, you would have to register the mappers like this:

AutoMapperConfiguration.RegisterMappers(); 
Sign up to request clarification or add additional context in comments.

1 Comment

How do use it as with dependency injection? There used to be an IMappingEngine once

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.