DEV Community

loading...

Extracting Service Configuration in Dotnet 5

Taylor Goodall
I'm a software developer working in Adelaide Australia, spend my free time cycling, playing video games and working on side projects
Originally published at taylorgoodall.me Updated on ・2 min read

This blog posts is a simple guide on how we can extract services from our startup.cs file separate configuration files allowing us to have dedicated configuration and keep our main startup.cs file as minimal as possible.
by default in ASP.net core 5 we get Swagger out of the box, let's start with extracting this out to to its own configuration file

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "ExtractConfigBlogPost", Version = "v1" });
            });
        }
Enter fullscreen mode Exit fullscreen mode

We can extract out the above code to a SwaggerConfig.cs file which will contain all of our swagger specific configuration as required. I have created a Configurations folder in the root of my project

using Microsoft.Extensions.DependencyInjection;
public static void AddSwaggerConfiguration(this IServiceCollection services)
{
    if (services == null) throw new ArgumentNullException(nameof(services));
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "ExtractConfigBlogPost", Version = "v1" });
    });
}
Enter fullscreen mode Exit fullscreen mode

We can then go ahead and Register this to our services in startup.cs Don’t forget to import the namespace of your configuration as well.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerConfiguration();
}
Enter fullscreen mode Exit fullscreen mode

we can also extract Swagger Configuration shown below

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExtractConfigBlogPost v1"));
            }
            // Code removed for brevity
        }
Enter fullscreen mode Exit fullscreen mode

This can be done by adding the below method to our SwaggerConfig file

public static void UseSwaggerSetup(this IApplicationBuilder app)
{
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ExtractConfigBlogPost v1"));
}
Enter fullscreen mode Exit fullscreen mode

then update the ConfigureServices method to be

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwaggerSetup();
}
Enter fullscreen mode Exit fullscreen mode

We can also use this pattern to bootstrap our own services and keep our startup as minimal as possible, You can break your services out as needed, for example you may want all of your repositories in a separate configuration file

using Microsoft.Extensions.DependencyInjection;
namespace ExtractConfigBlogPost.Configurations
{
    public static class RepositoryBootStrapper
    {
        public static void RegisterRepositoryServices(this IServiceCollection services)
        {
            services.AddScoped(typeof(IOrderRepository), typeof(OrderRepository));
            services.AddScoped(typeof(ICustomerRepository), typeof(CustomerRepository));
            services.AddScoped(typeof(IStockRepository), typeof(StockRepository));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Update startup.cs as follows

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerConfiguration();
    services.RegisterRepositoryServices();
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Splitting our service configuration into separate files is an extremely useful pattern to use within your codebase and can make maintenance of your project a lot easier when it comes to knowing where to look for changes. It may not be worth it when setting up a fresh project, however if you notice your startup.cs start to become muddied I would recommend reaching for this pattern

Questions? say hello on twitter

originally published on taylorgoodall.me on 08/12/2020

Discussion (0)