hn-dotnet/Infrastructure/ServiceCollectionExtensions.cs

42 lines
1.4 KiB
C#

using HN.Application;
using HN.Domain;
using HN.Infrastructure.Behaviors;
using HN.Infrastructure.Repositories;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace HN.Infrastructure
{
public sealed class HNServicesBuilder
{
private readonly IServiceCollection _services;
internal HNServicesBuilder(IServiceCollection services)
{
_services = services;
}
public void ResolveConnectedUserWith<TExecutingUserProvider>() where TExecutingUserProvider : class, IExecutingUserProvider
{
_services.AddScoped<IExecutingUserProvider, TExecutingUserProvider>();
}
}
public static class ServiceCollectionExtensions
{
public static HNServicesBuilder AddHN(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<HNDbContext>(options => options.UseSqlite(configuration.GetConnectionString("Default")));
services.AddScoped<IHNContext, HNDbContext>();
services.AddScoped<ILinkRepository, LinkRepository>();
services.AddScoped<ICommentRepository, CommentRepository>();
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(UnitOfWorkBehavior<,>));
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggerBehavior<,>));
services.AddMediatR(typeof(HN.Application.IHNContext));
return new HNServicesBuilder(services);
}
}
}