Ajout de la méthode d'extension AddHNServices

This commit is contained in:
YuukanOO 2021-04-27 13:43:42 +02:00
parent 4960449ed2
commit c82e8d7f26
4 changed files with 45 additions and 23 deletions

View File

@ -12,7 +12,8 @@ namespace CLI
new Domain.Link("http://default.website"), new Domain.Link("http://default.website"),
new Domain.Link("http://another.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); var service = new LinkService(linkRepository, data);
service.PublishLink(new PublishLinkCommand service.PublishLink(new PublishLinkCommand

View File

@ -1,5 +1,6 @@
using Application; using Application;
using Domain; using Domain;
using Infrastructure;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; 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. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
var link1 = new Domain.Link("http://default.website"); // ServiceCollectionExtensions.AddHNServices(services); // strictement équivalent à la ligne du dessous
var link2 = new Domain.Link("http://another.website"); services.AddHNServices();
var link3 = new Domain.Link("http://a.final.website");
services.AddSingleton<ILinkRepository>(new Infrastructure.Repositories.Memory.LinkRepository(
link1,
link2,
link3
));
services.AddSingleton<ICommentRepository>(new Infrastructure.Repositories.Memory.CommentRepository(
link1.AddComment("my first comment"),
link3.AddComment("another comment")
));
services.AddSingleton<IData>(serviceProvider =>
{
var memoryLinkRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository;
var memoryCommentRepository = serviceProvider.GetRequiredService<ICommentRepository>() as Infrastructure.Repositories.Memory.CommentRepository;
return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository);
});
services.AddTransient<LinkService>();
services.AddTransient<CommentService>();
services.AddControllersWithViews(options => services.AddControllersWithViews(options =>
{ {
options.Filters.Add<CustomExceptionFilter>(); options.Filters.Add<CustomExceptionFilter>();

View File

@ -4,6 +4,10 @@
<ProjectReference Include="..\Application\Application.csproj" /> <ProjectReference Include="..\Application\Application.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>

View File

@ -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<ILinkRepository>(new Infrastructure.Repositories.Memory.LinkRepository(
link1,
link2,
link3
));
services.AddSingleton<ICommentRepository>(new Infrastructure.Repositories.Memory.CommentRepository(
link1.AddComment("my first comment"),
link3.AddComment("another comment")
));
services.AddSingleton<IData>(serviceProvider =>
{
var memoryLinkRepository = serviceProvider.GetRequiredService<ILinkRepository>() as Infrastructure.Repositories.Memory.LinkRepository;
var memoryCommentRepository = serviceProvider.GetRequiredService<ICommentRepository>() as Infrastructure.Repositories.Memory.CommentRepository;
return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository);
});
services.AddTransient<LinkService>();
services.AddTransient<CommentService>();
return services;
}
}
}