根据 Mapster 文档https://github.com/MapsterMapper/Mapster/wiki/Dependency-Injection 我应该执行以下操作:
public void ConfigureServices(IServiceCollection services)
{
...
var config = new TypeAdapterConfig();
services.AddSingleton(config);
services.AddScoped<IMapper, ServiceMapper>();
...
}
以下是我尝试使用 Autofac 在我们的 MVC 4 应用程序中添加上述配置:
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
...
var config = new TypeAdapterConfig();
//services.AddSingleton(config); <- Not sure what is the equivalent of this line in Autofac?
//services.AddScoped<IMapper, ServiceMapper>();
// Not sure if the following is correct? Is AddScoped the same as InstancePerHttpRequest?
builder.RegisterType<MapsterMapper.ServiceMapper>()
.As<MapsterMapper.IMapper>()
.InstancePerHttpRequest();
- 如何添加配置实例的单例?
- 不确定我是否正确添加了 IMapper - ServiceMapper 配置以及 InstancePerHttpRequest 是否等同于 AddScoped?