c# - 如何将(启动的)ConfigureServices 方法拆分为多个文件

标签 c# dependency-injection repository-pattern fluentvalidation separation-of-concerns

关注点分离 (SoC)

ConfigureServices 中注册的依赖指令(启动类的方法)由不同的 DI 组成,如 Repository、Fluent Validations 等。

我将如何将 DI 注册分离到单独的文件中(如下所示)

enter image description here

最佳答案

创建一个扩展方法来保存你想要的任何额外配置

public static class MyExtensions {
    public static IServiceCollection AddFluentValidation(this IServiceCollection services) {

        //...add services

        return services;
    }
}

然后在Startup中调用ConfigureServices

public void ConfigureServices(IServiceCollection services) {

    //...

    services.AddFluentValidation();
    services.AddRepository();

    //...

}

框架和第 3 方扩展通常使用扩展方法来填充服务集合。

关于c# - 如何将(启动的)ConfigureServices 方法拆分为多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50898144/

相关文章:

c# - VS 2010 数据库 ORM 插件

java - EJB/MDB 应用程序中的 DI

java - 嵌套类@Configuration中的spring Bean覆盖2.1.x @Bean创建失败 'A bean with that name has already been defined'

c# - 在 Windows 窗体中使用带有工作单元和存储库模式的简单注入(inject)器

c# - DTO 与领域模型、项目组织

design-patterns - 存储库设计模式

c# - 使用 ASP.NET MVC 属性路由验证和传递 Controller 级参数

c# - 如何保护仅访问器的 IEnumerable<string> 属性免于突变?

c# - 尝试将文件保存到目录时出现访问被拒绝错误

c# - .NET Core 添加/注册一个类作为自身到服务集合