Where to place AutoMapper map registration in referenced dll

前端 未结 2 1326
终归单人心
终归单人心 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<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();
    
            }
        }
    
    0 讨论(0)
  • 2021-01-14 23:10

    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).

    0 讨论(0)
提交回复
热议问题