如何将 Automapper 与 Autofac 一起使用
How to use Automapper with Autofac
我已经升级到最新版本的 AutoMapper (9.0) 并将静态配置更改为:
public static IMapper RegisterAutoMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<MyModel MyDto>;
//etc...
});
var mapper = config.CreateMapper();
return mapper;
}
使用之前的静态 API 我曾经在 Global.asax
中执行以下操作:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AutoMapping.Map();
}
WebApiConfig.Register
注册路由并且 Autofac
如何使用 Autofac 注册 AutoMapper,因为目前我在这些行上遇到编译器错误:
var myDto = Mapper.Map<MyModel>(model);
编译错误:
An object reference is required for the non-static field, method, or property 'Mapper.Map(object)'
虽然我自己不熟悉 Autofac,here is a recent article 描述了如何使用 Autofac 为自动映射器设置注入。
祝你好运!
这是我之前制作的:
public class YourAutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//Also register any custom type converter/value resolvers
builder.RegisterType<CustomValueResolver>().AsSelf();
builder.RegisterType<CustomTypeConverter>().AsSelf();
builder.Register(context => new MapperConfiguration(cfg =>
{
cfg.CreateMap<MyModel MyDto>;
//etc...
})).AsSelf().SingleInstance();
builder.Register(c =>
{
//This resolves a new context that can be used later.
var context = c.Resolve<IComponentContext>();
var config = context.Resolve<MapperConfiguration>();
return config.CreateMapper(context.Resolve);
})
.As<IMapper>()
.InstancePerLifetimeScope();
}
}
在global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterModule<MyAutofacModule>();
// Register anything else needed
var container = builder.Build();
// MVC resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
然后你需要做的就是注入IMapper
还有一个 nuget-package 可以为您完成所有这些工作。
您需要做的就是在 ContainerBuilder
上调用扩展方法并传入程序集,程序集应该扫描 AutoMapper 类型。
var containerBuilder = new ContainerBuilder();
containerBuilder.AddAutoMapper(typeof(MvcApplication).Assembly);
// more registrations here
你也可以找到它here. You can find an official example in the AutoMapper docs。
编辑: 有 ASP.NET 核心和控制台应用程序的示例 here。
我已经升级到最新版本的 AutoMapper (9.0) 并将静态配置更改为:
public static IMapper RegisterAutoMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<MyModel MyDto>;
//etc...
});
var mapper = config.CreateMapper();
return mapper;
}
使用之前的静态 API 我曾经在 Global.asax
中执行以下操作:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AutoMapping.Map();
}
WebApiConfig.Register
注册路由并且 Autofac
如何使用 Autofac 注册 AutoMapper,因为目前我在这些行上遇到编译器错误:
var myDto = Mapper.Map<MyModel>(model);
编译错误:
An object reference is required for the non-static field, method, or property 'Mapper.Map(object)'
虽然我自己不熟悉 Autofac,here is a recent article 描述了如何使用 Autofac 为自动映射器设置注入。
祝你好运!
这是我之前制作的:
public class YourAutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//Also register any custom type converter/value resolvers
builder.RegisterType<CustomValueResolver>().AsSelf();
builder.RegisterType<CustomTypeConverter>().AsSelf();
builder.Register(context => new MapperConfiguration(cfg =>
{
cfg.CreateMap<MyModel MyDto>;
//etc...
})).AsSelf().SingleInstance();
builder.Register(c =>
{
//This resolves a new context that can be used later.
var context = c.Resolve<IComponentContext>();
var config = context.Resolve<MapperConfiguration>();
return config.CreateMapper(context.Resolve);
})
.As<IMapper>()
.InstancePerLifetimeScope();
}
}
在global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterModule<MyAutofacModule>();
// Register anything else needed
var container = builder.Build();
// MVC resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
然后你需要做的就是注入IMapper
还有一个 nuget-package 可以为您完成所有这些工作。
您需要做的就是在 ContainerBuilder
上调用扩展方法并传入程序集,程序集应该扫描 AutoMapper 类型。
var containerBuilder = new ContainerBuilder();
containerBuilder.AddAutoMapper(typeof(MvcApplication).Assembly);
// more registrations here
你也可以找到它here. You can find an official example in the AutoMapper docs。
编辑: 有 ASP.NET 核心和控制台应用程序的示例 here。