使用 Mapster w/DI 我应该把地图放在哪里?
Using Mapster w/DI where should I be putting my mapping?
将 Mapster 与 Mapster DI 包一起使用我还有一个名为 MapperConfig 的静态 class,它有一个静态方法可以完成我所有的 dto 到 viewmodel 映射。
public static class MapperConfig
{
public static void Config()
{
var tenantId = MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId");
_ = TypeAdapterConfig<CountyDetail, CountyVM>.NewConfig()
.Map(d => d.Phone.Number, s => string.Format("{0:(###) ###-####}", Convert.ToInt64(s.Phone.Number)))
.Map(d => d.Postal, s => s.Postal.Trim())
.IgnoreNullValues(true);
}
}
过去我会在启动的 ConfigureServices 部分调用它 class。现在我尝试使用 DI 将一些配置值传递到 MapperConfig 文件中,所以我创建了一个扩展方法:
public static IServiceCollection AddMapster(this IServiceCollection services, Action<TypeAdapterConfig> options = null)
{
var config = TypeAdapterConfig.GlobalSettings;
config.Scan(Assembly.GetAssembly(typeof(Startup)));
options?.Invoke(config);
services.AddSingleton(config);
services.AddScoped<IMapper, ServiceMapper>();
return services;
}
然后将其添加到 Startup 的 ConfigureServices 部分 class
services.AddMapster(options =>
{
TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);
});
现在,如果我在 Startup.ConfigureServices 方法中保留对 MapperConfig.Config 的调用,我会得到一个错误 "Mapping must be called using ServiceAdapter"。
不确定 how/where 会完成..
行 tenantId
需要移动到映射配置。
var tenantId = MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId");
例如,
TypeAdapterConfig<CountyDetail, CountyVM>.NewConfig()
.Map(d => d.TanentId,
s => MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId"));
将 Mapster 与 Mapster DI 包一起使用我还有一个名为 MapperConfig 的静态 class,它有一个静态方法可以完成我所有的 dto 到 viewmodel 映射。
public static class MapperConfig
{
public static void Config()
{
var tenantId = MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId");
_ = TypeAdapterConfig<CountyDetail, CountyVM>.NewConfig()
.Map(d => d.Phone.Number, s => string.Format("{0:(###) ###-####}", Convert.ToInt64(s.Phone.Number)))
.Map(d => d.Postal, s => s.Postal.Trim())
.IgnoreNullValues(true);
}
}
过去我会在启动的 ConfigureServices 部分调用它 class。现在我尝试使用 DI 将一些配置值传递到 MapperConfig 文件中,所以我创建了一个扩展方法:
public static IServiceCollection AddMapster(this IServiceCollection services, Action<TypeAdapterConfig> options = null)
{
var config = TypeAdapterConfig.GlobalSettings;
config.Scan(Assembly.GetAssembly(typeof(Startup)));
options?.Invoke(config);
services.AddSingleton(config);
services.AddScoped<IMapper, ServiceMapper>();
return services;
}
然后将其添加到 Startup 的 ConfigureServices 部分 class
services.AddMapster(options =>
{
TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);
});
现在,如果我在 Startup.ConfigureServices 方法中保留对 MapperConfig.Config 的调用,我会得到一个错误 "Mapping must be called using ServiceAdapter"。
不确定 how/where 会完成..
行 tenantId
需要移动到映射配置。
var tenantId = MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId");
例如,
TypeAdapterConfig<CountyDetail, CountyVM>.NewConfig()
.Map(d => d.TanentId,
s => MapContext.Current.GetService<IConfiguration>().GetSection("MySettings").GetValue<int>("DefaultTenantId"));