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

时间:2018-06-17 15:41:13

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

关注点分离(SoC)

在ConfigureServices(启动类的方法)中注册的依赖禁令包括不同的DI,如Repository,Fluent Validations等。

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

enter image description here

4 个答案:

答案 0 :(得分:4)

创建一个扩展方法以保存您想要的任何其他配置

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

        //...add services

        return services;
    }
}

然后在ConfigureServices

中的Startup中进行了调用
public void ConfigureServices(IServiceCollection services) {

    //...

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

    //...

}

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

答案 1 :(得分:2)

你好,你可以尝试这种方式

添加扩展方法

 public static class SlightDIModuleConfigExtension
{
     public static IWebHostBuilder UseSlightDIModuleconfig(this IWebHostBuilder webHostBuilder)
    {

        return webHostBuilder.ConfigureServices(services =>
        {
            foreach (Type type in Assembly.GetEntryAssembly()
             .GetTypes()
             .Where(myType => myType.IsSubclassOf(typeof(SlightModuleConfigure))))
            {
                var instance = Activator.CreateInstance(type);
                MethodInfo mi = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).FirstOrDefault();
                mi.Invoke(instance, new object[] { services });
            }
        });
    }
}

添加抽象类

public abstract class SlightModuleConfigure
{
    protected virtual void Load(IServiceCollection services) { }
}

使用方法

在Program.cs中添加UseSlightDIModuleconfig()

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>()
          .UseSlightDIModuleconfig(); //add this .

添加类像这样继承SlightModuleConfigure

public class PayModuleConfig : SlightModuleConfigure
{
    protected override void Load(IServiceCollection services)
    {
          services.AddTransient<Ipay, PayImp>();
    }
}

您可以参考SlightDIConfigure

答案 2 :(得分:1)

startup.cs

public class Startup
{
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMyScoped();
            services.AddMyTransient();
        }
}

<强> AddScopedExtension.cs

public static class AddScopedExtension
{
    public static IServiceCollection AddMyScoped(this IServiceCollection serviceCollection)
    {
        // TODO : Add your Scoped Objects here
        return serviceCollection;
    }
}

<强> AddTransientExtension.cs

public static class AddTransientExtension
{
    public static IServiceCollection AddMyTransient(this IServiceCollection serviceCollection)
    {
        // TODO : Add your Scoped Objects here
        return serviceCollection;
    }
}

答案 3 :(得分:0)

Partial课程 - 另一种方式!

// Startup.cs
public partial class Startup {
    public void ConfigureServices(IServiceCollection services) {
        ConfigureScopedServices(services);
        ConfigureTransientServices(services);
    }
}

// ScopedServices.cs
public partial class Startup {
    private static void ConfigureScopedServices(IServiceCollection services) {
        Console.WriteLine("Scoped");
    }
}

// TransientServices.cs
public partial class Startup {
    private static void ConfigureTransientServices(IServiceCollection services) {
        Console.WriteLine("Transient");
    }
}