Where to place AutoMapper map registration in referenced dll

人走茶凉 提交于 2019-12-01 11:15:51

Put it in the static constructor of either the source or the target type of the mapping.

public class FullData
{
    static FullData()
    {

        Mapper.CreateMap<IEnumerable<RawData>, FullData>()
            .ForMember(d => d.Acres, m => m.ResolveUsing(new RawLeadDataNameResolver("Acres")));
    }
 }

The static constructor will automatically get called the first time you try to use the type FullData for anything (for example a mapping).

Farhad Jabiyev

You can use PreApplicationStartMethod for any class and it's method in your class library which will be referenced from your startup project if you want automatically to call this on startup. And then you can register all your mappings in that method. By the way, I suggest to use AddProfile for registering all mappings.

[assembly: PreApplicationStartMethod(typeof(MyClassLibrary.Startup), "Start")]
namespace MyClassLibrary
{
    public class Startup
    {
        // Automatically will work on startup
        public static void Start()
        {
              Mapper.Initialize(cfg =>
              {
                    Assembly.GetExecutingAssembly().FindAllDerivedTypes<Profile>().ForEach(match =>
                    {
                        cfg.AddProfile(Activator.CreateInstance(match) as Profile);
                    });
              });
        }
    }
}

You just need to create new classes which derived from Profile class and then override it's Configure() method:

...
public class FooMapperProfile:Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<OtherFoo, Foo>()
              .ForMember(...
              ... // so on
    }
}

public class AnotherFooMapperProfile:Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<OtherFoo, AnotherFoo>()
              .ForMember(...
              ... // so on;
    }
}
... 
// and so on

Additional information: If you have seen I have initialized all mappings with that code:

Mapper.Initialize(cfg =>
{
        Assembly.GetExecutingAssembly().FindAllDerivedTypes<Profile>().ForEach(match =>
        {
                cfg.AddProfile(Activator.CreateInstance(match) as Profile);
        });
});

It will automatically find all types derived from Profile and will add all profiles after createing their new instances.

Update1:

As @Scott Chamberlain commented, PreApplicationStartMethod only works for ASP.NET applications. This would not work with a desktop app. If you are working with Wpf, then you can use Application.OnStartup method. Or just call Start.Startup (); in load event.

Update2:
FindAllDerivedTypes extension method:

 public static class AssemblyExtensions
    {
        public static List<Type> FindAllDerivedTypes<T>(this Assembly assembly)
        {
            var derivedType = typeof(T);
            return assembly.GetTypes()
                          .Where(t => t != derivedType && derivedType.IsAssignableFrom(t))
                          .ToList();

        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!