【问题标题】:How to inject AutoMapper with Autofac?如何用 Autofac 注入 AutoMapper?
【发布时间】:2020-04-22 10:03:00
【问题描述】:

将 AutoMapper 注入其他层的正确方法是什么?

我看了这篇博客post,但是这段代码导致下面的异常

AutoMapper.dll 中出现“AutoMapper.AutoMapperMappingException”类型的异常,但未在用户代码中处理

在服务层尝试映射时。

List<StudentViewModel> list2 = _mapper.Map<List<StudentViewModel>>(list);

我的 AutoFac 配置如下:

public static class DependencyRegistration
{
    public static void Config()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(typeof(MvcApplication).Assembly);


        builder.RegisterType<TypeMapFactory>().As<ITypeMapFactory>();
        builder.RegisterType<ConfigurationStore>().As<ConfigurationStore>().WithParameter("mappers", MapperRegistry.Mappers).SingleInstance();
        builder.Register((ctx, t) => ctx.Resolve<ConfigurationStore>()).As<IConfiguration>().As<IConfigurationProvider>();
        builder.RegisterType<MappingEngine>().As<IMappingEngine>();

        //...
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
}

【问题讨论】:

  • 您是否在要转换的类型之间创建了映射?
  • 异常信息是什么?
  • @YacoubMassad 是的,我创建了配置文件类并注册了它们。
  • @YacoubMassad {"缺少类型映射配置或不支持的映射。\r\n\r\n映射类型:\r\nStudent -> StudentViewModel\r\nDomainModels.Student -> ViewModels.StudentViewModel\r \n\r\n目标路径:\r\nList`1[0]\r\n\r\n源值:\r\nSystem.Data.Entity.DynamicProxies.Student_56CF505FEBA6F48D4BC49099754F9D134C329E2A4B8BCB2B62D9E22D00950B16"}
  • 你能显示你创建地图的代码吗?

标签: c# asp.net-mvc dependency-injection automapper autofac


【解决方案1】:

看来您需要使用在容器中注册的IConfiguration 对象来创建这样的地图:

var configuration = container.Resolve<IConfiguration>();
configuration.CreateMap<Student, StudentViewModel>();

我认为您应该在应用程序开始时执行此操作。

这是在Config 方法中配置事物的更好方法(IMO):

public static void Config()
{
    var configuration_store = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);

    var mapping_engine = new MappingEngine(configuration_store);

    configuration_store.CreateMap<Student, StudentViewModel>();

    var builder = new ContainerBuilder();

    builder.RegisterInstance(mapping_engine).As<IMappingEngine>();

    //...
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}

我假设在最后一个示例中,您的类只需要访问 IMappingEngine(而不是 IConfiguration),因为您应该已经在 Config 方法(或应用程序中的一些其他配置方法)中设置了所有映射启动)。

【讨论】:

  • 你说在个人资料课上这样做?如何获取容器?
  • 不,我建议你在 DependencyRegistration 类中这样做
  • 感谢您的回答,我有一些复杂的配置文件类,我认为将 AutoMapper 配置转移到 DependencyRegistration 类是个好主意吗?
  • @ramin_rp,我不确定我是否理解你的问题。但我认为您可以轻松地创建另一个类来注册 AutoMapper 映射并从 Config 方法中调用它。
  • 以上代码导致 AutoMapperMappingException "AutoMapper.dll 中发生'AutoMapper.AutoMapperMappingException' 类型的异常,但未在用户代码中处理"
【解决方案2】:

.netcore 3 Autofac 5.1.2 AutoMapper 9.0.0 AutoMapperProfiles -> 我的个人资料名称

protected override void Load(ContainerBuilder builder)
{
    builder.RegisterType<AutoMapperProfiles>().As<Profile>();
    builder.Register(c => new MapperConfiguration(cfg =>
    {
        foreach (var profile in c.Resolve<IEnumerable<Profile>>())
        {
            cfg.AddProfile(profile);
        }
    })).AsSelf().SingleInstance();

    builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve)).As<IMapper>().InstancePerLifetimeScope();
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 2019-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多