Compare commits
1 Commits
3624fc58ac
...
323a5c0941
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
323a5c0941 |
@ -18,7 +18,7 @@ namespace Api
|
||||
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddHNServices();
|
||||
services.AddHNServicesInMemory();
|
||||
services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<CustomExceptionFilter>();
|
||||
|
||||
@ -23,7 +23,8 @@ namespace Website
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// ServiceCollectionExtensions.AddHNServices(services); // strictement équivalent à la ligne du dessous
|
||||
services.AddHNServices();
|
||||
// services.AddHNServicesInMemory();
|
||||
services.AddHNServicesEF();
|
||||
services.AddControllersWithViews(options =>
|
||||
{
|
||||
options.Filters.Add<CustomExceptionFilter>();
|
||||
|
||||
21
Infrastructure/EntityTypes/CommentEntityType.cs
Normal file
21
Infrastructure/EntityTypes/CommentEntityType.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.EntityTypes
|
||||
{
|
||||
public class CommentEntityType : IEntityTypeConfiguration<Comment>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Comment> builder)
|
||||
{
|
||||
builder.HasKey(o => o.Id);
|
||||
builder.Property(o => o.Content).IsRequired();
|
||||
builder.Property(o => o.CreatedAt).IsRequired();
|
||||
builder.HasOne<Link>()
|
||||
.WithMany()
|
||||
.HasForeignKey(o => o.LinkId)
|
||||
.IsRequired()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
Infrastructure/EntityTypes/LinkEntityType.cs
Normal file
16
Infrastructure/EntityTypes/LinkEntityType.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Infrastructure.EntityTypes
|
||||
{
|
||||
public class LinkEntityType : IEntityTypeConfiguration<Link>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Link> builder)
|
||||
{
|
||||
builder.HasKey(o => o.Id);
|
||||
builder.Property(o => o.Url).HasMaxLength(250).IsRequired();
|
||||
builder.Property(o => o.CreatedAt).IsRequired();
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Infrastructure/HNDbContext.cs
Normal file
48
Infrastructure/HNDbContext.cs
Normal file
@ -0,0 +1,48 @@
|
||||
using System.Linq;
|
||||
using Application;
|
||||
using Domain;
|
||||
using Infrastructure.EntityTypes;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
public class HNDbContext : DbContext, IData
|
||||
{
|
||||
public DbSet<Link> Links { get; set; }
|
||||
public DbSet<Comment> Comments { get; set; }
|
||||
|
||||
IQueryable<Link> IData.Links => Links;
|
||||
|
||||
IQueryable<Comment> IData.Comments => Comments;
|
||||
|
||||
public HNDbContext() : base()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public HNDbContext(DbContextOptions<HNDbContext> options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Soit
|
||||
// modelBuilder.ApplyConfiguration(new LinkEntityType());
|
||||
// modelBuilder.ApplyConfiguration(new CommentEntityType());
|
||||
|
||||
// Ou
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly);
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (!optionsBuilder.IsConfigured)
|
||||
{
|
||||
optionsBuilder.UseSqlite("Data Source=:memory:");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,14 @@
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
74
Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs
generated
Normal file
74
Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs
generated
Normal file
@ -0,0 +1,74 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(HNDbContext))]
|
||||
[Migration("20210428140332_CreateLinkAndComment")]
|
||||
partial class CreateLinkAndComment
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.5");
|
||||
|
||||
modelBuilder.Entity("Domain.Comment", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("LinkId")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("LinkId");
|
||||
|
||||
b.ToTable("Comments");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Link", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Url")
|
||||
.IsRequired()
|
||||
.HasMaxLength(250)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Links");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Comment", b =>
|
||||
{
|
||||
b.HasOne("Domain.Link", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("LinkId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
public partial class CreateLinkAndComment : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Links",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
Url = table.Column<string>(type: "TEXT", maxLength: 250, nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Links", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Comments",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
LinkId = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||
Content = table.Column<string>(type: "TEXT", nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Comments", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Comments_Links_LinkId",
|
||||
column: x => x.LinkId,
|
||||
principalTable: "Links",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Comments_LinkId",
|
||||
table: "Comments",
|
||||
column: "LinkId");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Comments");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Links");
|
||||
}
|
||||
}
|
||||
}
|
||||
72
Infrastructure/Migrations/HNDbContextModelSnapshot.cs
Normal file
72
Infrastructure/Migrations/HNDbContextModelSnapshot.cs
Normal file
@ -0,0 +1,72 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(HNDbContext))]
|
||||
partial class HNDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "5.0.5");
|
||||
|
||||
modelBuilder.Entity("Domain.Comment", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Content")
|
||||
.IsRequired()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<Guid>("LinkId")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("LinkId");
|
||||
|
||||
b.ToTable("Comments");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Link", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.Property<string>("Url")
|
||||
.IsRequired()
|
||||
.HasMaxLength(250)
|
||||
.HasColumnType("TEXT");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Links");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Domain.Comment", b =>
|
||||
{
|
||||
b.HasOne("Domain.Link", null)
|
||||
.WithMany()
|
||||
.HasForeignKey("LinkId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using Domain;
|
||||
|
||||
namespace Infrastructure.Repositories.EntityFramework
|
||||
{
|
||||
public class CommentRepository : ICommentRepository
|
||||
{
|
||||
private readonly HNDbContext _context;
|
||||
|
||||
public CommentRepository(HNDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void Add(Comment comment)
|
||||
{
|
||||
_context.Comments.Add(comment);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Domain;
|
||||
|
||||
namespace Infrastructure.Repositories.EntityFramework
|
||||
{
|
||||
public class LinkRepository : ILinkRepository
|
||||
{
|
||||
private readonly HNDbContext _context;
|
||||
|
||||
public LinkRepository(HNDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void Add(Link link)
|
||||
{
|
||||
_context.Links.Add(link);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public Link GetById(Guid id)
|
||||
{
|
||||
return _context.Links.Single(link => link.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,42 @@
|
||||
using Application;
|
||||
using Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Infrastructure
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddHNServices(this IServiceCollection services)
|
||||
/// <summary>
|
||||
/// Ajout des services pour la gestion de notre domaine fonctionnel avec les implémentations
|
||||
/// à base d'Entity Framework Core.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddHNServicesEF(this IServiceCollection services)
|
||||
{
|
||||
services.AddDbContext<HNDbContext>((provider, options) =>
|
||||
{
|
||||
var configuration = provider.GetRequiredService<IConfiguration>();
|
||||
|
||||
options.UseSqlite(configuration.GetConnectionString("Default"));
|
||||
});
|
||||
|
||||
services.AddScoped<ILinkRepository, Infrastructure.Repositories.EntityFramework.LinkRepository>();
|
||||
services.AddScoped<ICommentRepository, Infrastructure.Repositories.EntityFramework.CommentRepository>();
|
||||
services.AddScoped<IData, HNDbContext>();
|
||||
|
||||
return services.AddCommon();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ajout des services pour la gestion de notre domaine fonctionnel avec les implémentations
|
||||
/// en mémoire, parfait pour des tests.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddHNServicesInMemory(this IServiceCollection services)
|
||||
{
|
||||
var link1 = new Domain.Link("http://default.website");
|
||||
var link2 = new Domain.Link("http://another.website");
|
||||
@ -27,6 +57,18 @@ namespace Infrastructure
|
||||
var memoryCommentRepository = serviceProvider.GetRequiredService<ICommentRepository>() as Infrastructure.Repositories.Memory.CommentRepository;
|
||||
return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository);
|
||||
});
|
||||
|
||||
|
||||
return services.AddCommon();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ajout des services communs peu importe la persistance choisie.
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
private static IServiceCollection AddCommon(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<LinkService>();
|
||||
services.AddTransient<CommentService>();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user