Where to place AutoMapper map registration in referenced dll

前端 未结 2 1327
终归单人心
终归单人心 2021-01-14 22:29

This is my first AutoMapper project and may be obvious to some but the tutorials and examples are not clicking with me. I am trying to understand where and to a certain degr

2条回答
  •  借酒劲吻你
    2021-01-14 22:59

    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().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()
                  .ForMember(...
                  ... // so on
        }
    }
    
    public class AnotherFooMapperProfile:Profile
    {
        protected override void Configure()
        {
            Mapper.CreateMap()
                  .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().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 FindAllDerivedTypes(this Assembly assembly)
            {
                var derivedType = typeof(T);
                return assembly.GetTypes()
                              .Where(t => t != derivedType && derivedType.IsAssignableFrom(t))
                              .ToList();
    
            }
        }
    

提交回复
热议问题