我想知道是否有办法在使用依赖注入时全局配置 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.
});
我想这些不起作用,因为它ServiceMapper
正在创建自己的实例而不使用我配置的任何东西。