From c82e8d7f26695428f809e126b151b9561328b422 Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Tue, 27 Apr 2021 13:43:42 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20m=C3=A9thode=20d'extension=20?= =?UTF-8?q?AddHNServices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apps/CLI/Program.cs | 3 +- Apps/Website/Startup.cs | 25 ++----------- Infrastructure/Infrastructure.csproj | 4 +++ Infrastructure/ServiceCollectionExtensions.cs | 36 +++++++++++++++++++ 4 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 Infrastructure/ServiceCollectionExtensions.cs diff --git a/Apps/CLI/Program.cs b/Apps/CLI/Program.cs index 0aeb205..6f068d5 100644 --- a/Apps/CLI/Program.cs +++ b/Apps/CLI/Program.cs @@ -12,7 +12,8 @@ namespace CLI new Domain.Link("http://default.website"), new Domain.Link("http://another.website") ); - var data = new Data(linkRepository); + var commentRepository = new CommentRepository(); + var data = new Data(linkRepository, commentRepository); var service = new LinkService(linkRepository, data); service.PublishLink(new PublishLinkCommand diff --git a/Apps/Website/Startup.cs b/Apps/Website/Startup.cs index 0598c52..94e38aa 100644 --- a/Apps/Website/Startup.cs +++ b/Apps/Website/Startup.cs @@ -1,5 +1,6 @@ using Application; using Domain; +using Infrastructure; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -20,28 +21,8 @@ namespace Website // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - var link1 = new Domain.Link("http://default.website"); - var link2 = new Domain.Link("http://another.website"); - var link3 = new Domain.Link("http://a.final.website"); - - services.AddSingleton(new Infrastructure.Repositories.Memory.LinkRepository( - link1, - link2, - link3 - )); - services.AddSingleton(new Infrastructure.Repositories.Memory.CommentRepository( - link1.AddComment("my first comment"), - link3.AddComment("another comment") - )); - services.AddSingleton(serviceProvider => - { - var memoryLinkRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.LinkRepository; - var memoryCommentRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.CommentRepository; - return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository); - }); - services.AddTransient(); - services.AddTransient(); - + // ServiceCollectionExtensions.AddHNServices(services); // strictement équivalent à la ligne du dessous + services.AddHNServices(); services.AddControllersWithViews(options => { options.Filters.Add(); diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index a9fb990..1ba9e05 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -4,6 +4,10 @@ + + + + net5.0 diff --git a/Infrastructure/ServiceCollectionExtensions.cs b/Infrastructure/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..a6dd769 --- /dev/null +++ b/Infrastructure/ServiceCollectionExtensions.cs @@ -0,0 +1,36 @@ +using Application; +using Domain; +using Microsoft.Extensions.DependencyInjection; + +namespace Infrastructure +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddHNServices(this IServiceCollection services) + { + var link1 = new Domain.Link("http://default.website"); + var link2 = new Domain.Link("http://another.website"); + var link3 = new Domain.Link("http://a.final.website"); + + services.AddSingleton(new Infrastructure.Repositories.Memory.LinkRepository( + link1, + link2, + link3 + )); + services.AddSingleton(new Infrastructure.Repositories.Memory.CommentRepository( + link1.AddComment("my first comment"), + link3.AddComment("another comment") + )); + services.AddSingleton(serviceProvider => + { + var memoryLinkRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.LinkRepository; + var memoryCommentRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.CommentRepository; + return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository); + }); + services.AddTransient(); + services.AddTransient(); + + return services; + } + } +} \ No newline at end of file