繁体   English   中英

在分层架构中指定程序集

[英]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(参数程序集 []程序集)”中的参考参数。

程序.cs

NLayer项目

综上所述,对于我必须在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM