Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan and auto-configure profiles in AutoMapper?

Tags:

Is there any way to auto-configue Automapper to scan for all profiles in namespace/assembly? What I would like to do is to add mapping profiles to AutoMapper from given assembly filtered by given interface, something like Scan Conventions in StructureMap:

    public static void Configure()     {         ObjectFactory.Initialize(x =>             {                 // Scan Assembly                 x.Scan(                     scanner =>                     {                         scanner.TheCallingAssembly();                         scanner.Convention<MyCustomConvention>();                         scanner.WithDefaultConventions();                     });                  // Add Registries                 x.AddRegistry(new SomeRegistry());             });          Debug.WriteLine(ObjectFactory.WhatDoIHave());     }  public class MyCustomConvention : IRegistrationConvention {     public void Process(Type type, Registry registry)     {         if (!type.CanBeCastTo(typeof(IMyType)))         {             return;         }          string name = type.Name.Replace("SomeRubishName", String.Empty);         registry.AddType(typeof(IMyType), type, name);                 } 

I've tried to use SelfConfigure but can't find any documentation on how to use it to filter out profiles:

    public static void Configure()     {         Mapper.Initialize(x =>                               {                                   // My Custom profile                                   x.AddProfile<MyMappingProfile>();                                    // Scan Assembly                                   x.SelfConfigure(Assembly.GetCallingAssembly());                               });     } 

Another question is how can I report all maps/profiles already initialized (something like ObjectFactory.WhatDoIHave() in StructureMap)?

like image 367
Wojciech Markowski Avatar asked Apr 16 '10 08:04

Wojciech Markowski


People also ask

What is an AutoMapper profile?

automapper Profiles Basic Profile Profiles permit the programmer to organize maps into classes, enhancing code readability and maintainability. Any number of profiles can be created, and added to one or more configurations as needed. Profiles can be used with both the static and instance-based APIs.


2 Answers

I found this post while searching as well, but this is how I implemented an auto mapping scheme:

public class MyCustomMap : Profile {     protected override void Configure()     {         CreateMap<MyCustomViewModel, MyCustomObject>()             .ForMember(dest => dest.Phone,                         opt => opt.MapFrom(                         src => src.PhoneAreaCode + src.PhoneFirstThree + src.PhoneLastFour));     } }  public static class AutoMapperConfiguration {     public static void Configure()     {         Mapper.Initialize(x => GetConfiguration(Mapper.Configuration));     }      private static void GetConfiguration(IConfiguration configuration)     {         var profiles = typeof(MyCustomMap).Assembly.GetTypes().Where(x => typeof(Profile).IsAssignableFrom(x));         foreach (var profile in profiles)         {             configuration.AddProfile(Activator.CreateInstance(profile) as Profile);         }     } } 

So when my application starts, all I call is

AutoMapperConfiguration.Configure();  

And all my maps are registered.

like image 140
Jason More Avatar answered Sep 29 '22 16:09

Jason More


In the latest versions of AutoMapper it's possible to register multiple Profile scanning one or more assemblies :

 Mapper.Initialize(x => x.AddProfiles(typeof(MyMappingProfile).Assembly)); 

Tested with AutoMapper v. 6.0.2.0

like image 28
Martino Bordin Avatar answered Sep 29 '22 18:09

Martino Bordin