infrastructure DI
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
using AipsCore.Application.Abstract;
|
||||
using AipsCore.Application.Common.Dispatcher;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace AipsCore.Infrastructure.DI;
|
||||
|
||||
public static class AipsRegistrationExtensions
|
||||
{
|
||||
public static IServiceCollection AddAips(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHandlersFromAssembly(typeof(Dispatcher).Assembly);
|
||||
services.AddSingleton<IDispatcher, Dispatcher>();
|
||||
|
||||
services.AddPersistence(configuration);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace AipsCore.Infrastructure.DI.Configuration;
|
||||
|
||||
public static class ConfigurationEnvExtensions
|
||||
{
|
||||
private const string DbConnStringKey = "DB_CONN_STRING";
|
||||
|
||||
public static string GetEnvConnectionString(this IConfiguration configuration)
|
||||
{
|
||||
return configuration.GetEnvForSure(DbConnStringKey);
|
||||
}
|
||||
|
||||
private static string GetEnvForSure(this IConfiguration configuration, string key)
|
||||
{
|
||||
var value = configuration[key];
|
||||
|
||||
if (value is null)
|
||||
{
|
||||
throw new ConfigurationException(key);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace AipsCore.Infrastructure.DI.Configuration;
|
||||
|
||||
public class ConfigurationException : Exception
|
||||
{
|
||||
public ConfigurationException(string key)
|
||||
: base($"Env configuration error, key '{key}' not set.")
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Reflection;
|
||||
using AipsCore.Application.Abstract.Command;
|
||||
using AipsCore.Application.Abstract.Query;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace AipsCore.Infrastructure.DI;
|
||||
|
||||
public static class HandlerRegistrationExtensions
|
||||
{
|
||||
public static IServiceCollection AddHandlersFromAssembly(this IServiceCollection services, Assembly assembly)
|
||||
{
|
||||
var handlerInterfaces = new[]
|
||||
{
|
||||
typeof(ICommandHandler<>),
|
||||
typeof(ICommandHandler<,>),
|
||||
typeof(IQueryHandler<,>)
|
||||
};
|
||||
|
||||
var types = assembly.GetTypes()
|
||||
.Where(t => t is { IsAbstract: false, IsInterface: false });
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var interfaces = type.GetInterfaces();
|
||||
|
||||
foreach (var @interface in interfaces)
|
||||
{
|
||||
if (!@interface.IsGenericType)
|
||||
continue;
|
||||
|
||||
var genericDef = @interface.GetGenericTypeDefinition();
|
||||
|
||||
if (!handlerInterfaces.Contains(genericDef))
|
||||
continue;
|
||||
|
||||
services.AddScoped(@interface, type);
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using AipsCore.Infrastructure.DI.Configuration;
|
||||
using AipsCore.Infrastructure.Persistence.Db;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace AipsCore.Infrastructure.DI;
|
||||
|
||||
public static class PersistenceRegistrationExtensions
|
||||
{
|
||||
public static IServiceCollection AddPersistence(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddDbContext<AipsDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(configuration.GetEnvConnectionString());
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user