如何将 ConfigureServices 方法(启动)拆分为多个文件 [英] How to Split ConfigureServices method (of Startup) into multiple files

本文介绍了如何将 ConfigureServices 方法(启动)拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注点分离 (SoC)

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

Dependency injunction registrtered in ConfigureServices (method of startup class) consist of different DI's like Repository, Fluent Validations etc.

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

How would I go about separating DI registration into separate files (as shown below)

推荐答案

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

Create an extension method to hold any additional configuration you want

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

        //...add services

        return services;
    }
}

然后在Startup

public void ConfigureServices(IServiceCollection services) {

    //...

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

    //...

}

使用扩展方法填充服务集合是框架和第 3 方扩展常用的方法.

The use of extension methods for populating the services collection is commonly used by the framework and 3rd party extensions.

这篇关于如何将 ConfigureServices 方法(启动)拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆