Auto register the Multi Project AutoMapper 5.0 Profile's into Autofac

153 views
Skip to first unread message

veerama...@gmail.com

unread,
Jul 11, 2016, 12:45:54 PM7/11/16
to AutoMapper-users
I am trying to register my profiles into the autofac from the different project.  I've tried the different ways which given below, but I am not successful.  

       The ways which I've tried with the help of google...

           //builder.RegisterAssemblyTypes().AssignableTo(typeof(Profile)).As<Profile>();
 
            //builder.RegisterSource(new ContravariantRegistrationSource());
            //builder.RegisterAssemblyTypes(typeof(Profile).Assembly).AsImplementedInterfaces();
 
            //builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
            //         .Where(t => typeof(Profile).IsAssignableFrom())
            //         .As<Profile>();
 
            //builder.RegisterAssemblyTypes(typeof(AutoMapperModule).Assembly).As<Profile>();
            //builder.RegisterAssemblyTypes(typeof(ServiceName).Assembly).As<Profile>();
            //builder.RegisterAssemblyTypes(typeof(RepositoryName).Assembly).As<Profile>();
 
            //builder.RegisterAssemblyTypes(System.Reflection.Assembly.GetExecutingAssembly()).AssignableTo<Profile>().As<Profile>();
 
            builder.Register(context => new MapperConfiguration(cfg =>
            {
                foreach (var profile in context.Resolve<IEnumerable<Profile>>())
                {
                    cfg.AddProfile(profile);
                }
            })).AsSelf().SingleInstance();
 
            builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve))
                 .As<IMapper>()
                 .InstancePerLifetimeScope();

    straight forward way which is working for me is 
  

        var mapperConfiguration = new MapperConfiguration(cfg =>
           {
 
               cfg.AddProfile(new APIProject.APIMappingProfile());
               cfg.AddProfile(new ServiceProject.ServiceMappingProfile());
               cfg.AddProfile(new RepositoryProject.RepositoryMappingProfile());
               //This list is keep on going...
 
           });
           var mapper = mapperConfiguration.CreateMapper();
           builder.RegisterInstance(mapper).As<IMapper>();
 
    My sample profiles

    public class APIMappingProfile : Profile
  {

      public APIMappingProfile()
      {
          CreateMap<ServiceModelAPIModel>().ReverseMap();
      }

  }

Please suggest me a way to auto register all of my profiles into autofac. 

Jimmy Bogard

unread,
Jul 11, 2016, 12:49:37 PM7/11/16
to automapp...@googlegroups.com
I don't know Autofac, sorry. But you shouldn't need it, just use regular reflection to look for Profile classes.
--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages