[英]specifying assembly in layered architecture
我在分層架構中的 web program.cs 中指定了 mapster 服務操作,但我想在另一層中使用此應用程序。 而且我在選擇組裝時遇到了麻煩。
Web層Program.cs
var config = TypeAdapterConfig.GlobalSettings;
config.Scan(Assembly.GetAssembly(typeof(UserMappingConfig)));
builder.Services.AddSingleton(config);
builder.Services.AddScoped<IMapper, ServiceMapper>();
服務層映射類
namespace Exams.Service.Mapping
{
public class QuestionMappingConfig : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<QuestionViewModel, Question>().IgnoreNullValues(true);
config.NewConfig<List<QuestionViewModel>,List<Question>>().IgnoreNullValues(true);
config.NewConfig<Question, QuestionViewModel>().IgnoreNullValues(true);
}
}
}
我收到這樣的警告
嚴重性代碼 描述 項目文件行抑制 State 警告 CS8604 可能的 null 參數“程序集”在“IList TypeAdapterConfig.Scan(參數程序集 []程序集)”中的參考參數。
綜上所述,對於我必須在 Web 層中定義的 Mapster 應用程序,我想在 Service 層中使用我在 Service 層中定義的配置,但是我在選擇程序集時遇到了麻煩。 我該如何解決?
Assembly.GetAssembly
返回Assembly?
(請參閱可空引用類型),您可以使用容空運算符 ( !
):
config.Scan(Assembly.GetAssembly(typeof(UserMappingConfig))!);
或者只使用類型實例中的Assembly
:
config.Scan(typeof(UserMappingConfig).Assembly);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.