How to Split ConfigureServices method (of Startup) into multiple files
Asked Answered
C

4

8

Separation of concerns (SoC)

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

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

enter image description here

Contingent answered 17/6, 2018 at 15:41 Comment(4)
Separate file same project or in different projects?You can create an extension methodMoppet
@Moppet Its in same project. Surely i can use extensions, but is there a alternative way like reference delegations..Contingent
extension methods are the most common approach to reducing noise in the Startup class.Moppet
You should use the extract method refactoring.Keyte
M
17

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;
    }
}

And then called in the ConfigureServices in Startup

public void ConfigureServices(IServiceCollection services) {

    //...

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

    //...

}

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

Moppet answered 17/6, 2018 at 15:49 Comment(0)
R
2

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;
    }
}
Religieuse answered 17/6, 2018 at 15:49 Comment(0)
C
2

Hello you can try this way

Add Extension Method

 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 });
            }
        });
    }
}

Add abstract class

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

How to Use

in Program.cs add UseSlightDIModuleconfig()

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

Add class inherit SlightModuleConfigure like this

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

you can refer to SlightDIConfigure

Cordwood answered 17/10, 2018 at 7:14 Comment(0)
S
1

Partial class - Just another way!

// 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");
    }
}
Susurrate answered 17/6, 2018 at 21:34 Comment(3)
Never really considered using partials.Religieuse
Do you mean it's a bad practice? Glad to know to know more if that's the case.Susurrate
Not sure if its bad practice or just a personal preference. I generally do not use partials because it can make code harder to maintain.Religieuse

© 2022 - 2024 — McMap. All rights reserved.