Kristam.Utility
1.0.0
dotnet add package Kristam.Utility --version 1.0.0
NuGet\Install-Package Kristam.Utility -Version 1.0.0
<PackageReference Include="Kristam.Utility" Version="1.0.0" />
<PackageVersion Include="Kristam.Utility" Version="1.0.0" />
<PackageReference Include="Kristam.Utility" />
paket add Kristam.Utility --version 1.0.0
#r "nuget: Kristam.Utility, 1.0.0"
#:package Kristam.Utility@1.0.0
#addin nuget:?package=Kristam.Utility&version=1.0.0
#tool nuget:?package=Kristam.Utility&version=1.0.0
Utility Extension Library This library provides a set of utility and extension methods to simplify common service configurations in ASP.NET Core applications. It uses Autofac for dependency injection and AutoMapper for object-to-object mapping, offering a convention-based approach to register services and configure standard application components like CORS, API versioning, Swagger, JWT authentication, and database contexts. Key Features
- Convention-Based Service Registration: Automatically registers services based on custom attributes ([Scoped], [Singleton], [Transient]) using Autofac.
- AutoMapper Integration: Simplifies the setup of AutoMapper profiles across assemblies.
- ASP.NET Core Configuration: Provides convenient extension methods to configure: o CORS o API Versioning o Swagger/OpenAPI with JWT authentication support o JWT Bearer Authentication o Entity Framework Core database contexts for both MySQL and MSSQL
Classes Overview
- ContainerBuilderExtensions This static class contains extension methods for Autofac's ContainerBuilder. It automates the process of registering services and AutoMapper profiles by scanning assemblies. Methods
- AutoRegisterTypes(this ContainerBuilder builder, string businessUnit): This is the main method for automatic service registration. o It scans all loaded assemblies whose full names start with the specified businessUnit string. o It finds interfaces and their concrete implementations within these assemblies. o It registers each implementation with Autofac based on the lifetime specified by a custom attribute ([Scoped], [Singleton], or [Transient]). o It also registers AutoMapper profiles found in the scanned assemblies. o Lifetime Resolution:
- [SingletonAttribute]: Registered as a single instance (SingleInstance()).
- [TransientAttribute]: Registered as per-dependency (InstancePerDependency()).
- No Attribute: Defaults to scoped (InstancePerLifetimeScope()).
- ServiceCollectionExtensions This static class provides extension methods for IServiceCollection, the standard dependency injection container in ASP.NET Core. These methods encapsulate common setup tasks, making Program.cs or Startup.cs cleaner and more readable. Methods
- AddCorsPolicys(this IServiceCollection services): Adds a default CORS policy that allows any origin, header, and method.
- AddApiVersioningConfigurations(this IServiceCollection services): Configures API versioning with a default version of 1.0 and uses the api-version header for versioning.
- AddJsonSerializerOptions(this IServiceCollection services): Configures JsonSerializerOptions to handle circular references by preserving object references. You can add more options here as needed.
- AddSwaggerGenConfigurations(this IServiceCollection services, string title, string version): Sets up Swagger (OpenAPI) documentation. It also includes configuration for JWT Bearer token authentication in the Swagger UI.
- AddDbContexts<TContext>(this IServiceCollection services) where TContext : DbContext: Registers a database context as a scoped service.
- AddMySQLDatabaseContextPools<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStr, string migrationsAssembly) where TContext : DbContext: Configures a MySQL database context pool using the provided connection string and migration assembly.
- AddMSSQLDatabaseContextPools<TContext>(this IServiceCollection services, IConfiguration configuration, string connectionStr, string migrationsAssembly) where TContext : DbContext: Configures a SQL Server database context pool using the provided connection string and migration assembly.
- AddJwtAuthentications(this IServiceCollection services, IConfiguration configuration): Configures JWT Bearer authentication, validating the token against settings from IConfiguration.
How to Use Setup in Program.cs or Startup.cs
- Configure IServiceCollection: You can chain the extension methods from ServiceCollectionExtensions to set up your application's services. C# // Program.cs or Startup.cs var builder = WebApplication.CreateBuilder(args); var services = builder.Services; var configuration = builder.Configuration;
services.AddCorsPolicys() .AddApiVersioningConfigurations() .AddSwaggerGenConfigurations("My API", "v1") .AddJwtAuthentications(configuration) .AddMSSQLDatabaseContextPools<MyDbContext>(configuration, "DefaultConnection", "MyAssembly"); 2. Configure Autofac: You can use the ContainerBuilderExtensions.AutoRegisterTypes method within your Autofac module or in the ConfigureContainer method of a Startup class. This will automatically find and register services based on their attributes. C# // Example: Autofac setup builder.AutoRegisterTypes("YourProject.BusinessUnit"); Using Lifetime Attributes Apply the custom attributes ([Scoped], [Singleton], [Transient]) to your service implementations to control their lifetime in the dependency injection container. C# // Example of a Scoped service [Scoped] public class MyService : IMyService { // ... }
// Example of a Singleton service [Singleton] public class MySingletonService : IMySingletonService { // ... }
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Autofac (>= 8.4.0)
- AutoMapper.Contrib.Autofac.DependencyInjection (>= 10.0.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.19)
- Microsoft.AspNetCore.Mvc.Versioning (>= 5.1.0)
- Microsoft.AspNetCore.OpenApi (>= 8.0.19)
- Microsoft.EntityFrameworkCore (>= 8.0.19)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.19)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Pomelo.EntityFrameworkCore.MySql (>= 8.0.3)
- Swashbuckle.AspNetCore.Filters (>= 8.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 218 | 9/1/2025 |