53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using HackerNet.Application;
|
|
using HackerNet.Domain;
|
|
using HackerNet.Infrastructure.Repositories.EntityFramework;
|
|
using HackerNet.Infrastructure.Repositories.Memory;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace HackerNet.Infrastructure.AspNet;
|
|
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
public static IServiceCollection AddHackerNetServicesMemory(this IServiceCollection services)
|
|
{
|
|
var link = new Link("", "https://localhost:7050/", "Youhouuu");
|
|
var comment = new Comment("", link.Id, "Wow!");
|
|
var linksRepository = new MemoryLinkRepository(link);
|
|
var commentsRepository = new MemoryCommentRepository(comment);
|
|
|
|
services.AddSingleton<ILinkRepository>(linksRepository);
|
|
services.AddSingleton<ICommentRepository>(commentsRepository);
|
|
services.AddSingleton<IReadStore>(new MemoryReadStore(linksRepository, commentsRepository));
|
|
|
|
services.AddSingleton<LinkService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddHackerNetServicesEntityFramework(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
// var connString = configuration["ConnectionStrings:Default"];
|
|
|
|
services.AddDbContext<HackerContext>(options
|
|
=> options.UseSqlite(configuration.GetConnectionString("Default")));
|
|
services.AddScoped<ICurrentUser, HttpCurrentUser>();
|
|
services.AddScoped<ILinkRepository, EFLinkRepository>();
|
|
services.AddScoped<ICommentRepository, EFCommentRepository>();
|
|
services.AddScoped<IReadStore, EFReadStore>();
|
|
|
|
services.AddScoped<LinkService>();
|
|
|
|
return services;
|
|
}
|
|
|
|
public static void MigrateDatabase(this IApplicationBuilder app)
|
|
{
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
using var ctx = scope.ServiceProvider.GetRequiredService<HackerContext>();
|
|
|
|
ctx.Database.Migrate();
|
|
}
|
|
} |