小编Fos*_*sol的帖子

具有依赖注入的 Mapster 全局配置

我想知道是否有一种方法可以在使用依赖注入时全局配置Mapster?

配置选项似乎适用于静态使用,也仅适用于单例模式。

地图大师配置

Mapster 依赖注入

我创建了一个扩展方法。

// Extension method
public static IServiceCollection AddMapster(this IServiceCollection services, Action<TypeAdapterConfig> options = null)
{
    var config = new TypeAdapterConfig();
    config.Scan(Assembly.GetAssembly(typeof(Startup)));

    options?.Invoke(config);

    services.AddSingleton(config);
    services.AddScoped<IMapper, ServiceMapper>();

    return services;
}

// Called in Startup.ConfigureServices(IServiceCollection services)
services.AddMapster(options =>
{
    options.Default.IgnoreNonMapped(true); // Does not work.
    TypeAdapterConfig.GlobalSettings.Default.IgnoreNonMapped(true); // Does not work.
});
Run Code Online (Sandbox Code Playgroud)

我想这些不起作用,因为它们ServiceMapper正在创建自己的实例而不使用我配置的任何内容。

c# dependency-injection mapster

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×1

dependency-injection ×1

mapster ×1