IServiceCollection Extension Methods - denmitchell/EDennis.AspNetCore.Base GitHub Wiki

(under construction) In ASP.NET Core web applications, there is often a significant amount of configuration work done in the Startup class. Although this configuration work is often not very code-intensive, there is often a lot of copy-paste-and-tweak moments with the configuration work.

EDennis.AspNetCore.Base provides a few IServiceCollection extension methods that can reduce configuration code, sometimes substantially. The extension methods can be organized around the functionality that they provide.

Dependency-Injection-Related Methods

In Startup.ConfigureServices, the developer tells the ASP.NET Core web application framework how to setup dependency injection for various classes.

DbContext

Without EDennis.AspNetCore.Base

Typically speaking, one configures Entity Framework DbContext classes using the AddDbContext extension method. This method accepts a DbContextOptionsBuilder argument, which can be used to configure the database or connection.

services.AddDbContext<SomeDbContext>(options => { 
   options.UseSqlServer(Configuration["ConnectionStrings:SomeDbContext"]);
});

With EDennis.AspNetCore.Base

As long as the developer uses SQL Server and follows the convention of configuring connection strings with the key ConnectionStrings:SomeDbContext, the EDennis.AspNetCore.Base library provides a much simpler configuration. NOTE: the method name is the plural form of AddDbContext -- AddDbContexts.

services.AddDbContexts<SomeDbContext>();

The code is simplified even more if the developer needs to add two or more DbContexts. For example, to add contexts for both current records and history records, you can use the following overload of AddDbContexts:

services.AddDbContexts<SomeDbContext,SomeHistoryDbContext>();

There are overloads supporting up to 5 DbContexts per method invocation.

Repo Classes

Without EDennis.AspNetCore.Base

Typically speaking, one configures dependency injection of custom classes using one of the service-lifetime-specific extension methods -- AddTransient<...>(), AddScoped<...>(), AddSingleton<...>() or AddSingleton(...). For repository classes, typically the AddTransient or AddScoped extension method would work. When you have just one repository class, the dependency injection code is fairly simple:

services.AddScoped<ScopeProperties>(); //only needed for the writeable repos
services.AddScoped<ISomeRepo,SomeRepo>(); //note: you can replace ISomeRepo with SomeRepo
});

When you have multiple repos, then the code requires more lines:

services.AddScoped<ScopeProperties>(); //only needed for the writeable repos
services.AddScoped<ISomeRepo1,SomeRepo1>(); //note: you can replace ISomeRepo1 with SomeRepo1
services.AddScoped<ISomeRepo2,SomeRepo2>(); //note: you can replace ISomeRepo2 with SomeRepo2
services.AddScoped<ISomeRepo3,SomeRepo3>(); //note: you can replace ISomeRepo3 with SomeRepo3
});

With EDennis.AspNetCore.Base

EDennis.AspNetCore.Base provides an AddRepos extension method that sets up injection for ScopeProperties, as well as one to five repos (per method invocation). Note that any subsequent calls to AddRepos will not re-setup injection for ScopeProperties.

services.AddRepos<SomeRepo1,SomeRepo2,SomeRepo3>();
});

ApiClient classes

Without EDennis.AspNetCore.Base

Typed HttpClient classes are setup using the AddHttpClient extension method.

services.AddHttpClient<ISomeApiClient,SomeApiClient>(client => {
        client.BaseAddress = new Uri(SOME_URI);
        //...any other configurations
    });

With EDennis.AspNetCore.Base

ApiClient classes are setup using the AddApiClients extension method.

services.AddApiClients<SomeApiClient>();

The BaseAddress for the client is drawn from Configuration, where the key is "Apis:SomeApiClient:BaseAddress". By externalizing the BaseAddress, the host/port can be determined dynamically during startup. Such dynamic port assignment is especially helpful when an application has many descendant APIs -- APIs that the main application uses directly or indirectly.

The AddApiClients method has overloads for one-to-five ApiClient classes per method invocation. So, setup configuration for four ApiClients would look like the following:

services.AddApiClients<SomeApiClient1,SomeApiClient2,SomeApiClient3,SomeApiClient4>();

(_to be continued.)

⚠️ **GitHub.com Fallback** ⚠️