From 3624fc58ac542f18113f54765d0f3a58ae69de56 Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Wed, 28 Apr 2021 16:30:44 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20impl=C3=A9mentations=20Entity=20Framewo?= =?UTF-8?q?rk=20Core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apps/Api/Startup.cs | 2 +- Apps/Website/Startup.cs | 3 +- .../EntityTypes/CommentEntityType.cs | 21 ++++++ Infrastructure/EntityTypes/LinkEntityType.cs | 16 ++++ Infrastructure/HNDbContext.cs | 48 ++++++++++++ Infrastructure/Infrastructure.csproj | 8 ++ ...428140332_CreateLinkAndComment.Designer.cs | 74 +++++++++++++++++++ .../20210428140332_CreateLinkAndComment.cs | 58 +++++++++++++++ .../Migrations/HNDbContextModelSnapshot.cs | 72 ++++++++++++++++++ .../EntityFramework/CommentRepository.cs | 20 +++++ .../EntityFramework/LinkRepository.cs | 27 +++++++ Infrastructure/ServiceCollectionExtensions.cs | 27 ++++++- 12 files changed, 373 insertions(+), 3 deletions(-) create mode 100644 Infrastructure/EntityTypes/CommentEntityType.cs create mode 100644 Infrastructure/EntityTypes/LinkEntityType.cs create mode 100644 Infrastructure/HNDbContext.cs create mode 100644 Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs create mode 100644 Infrastructure/Migrations/20210428140332_CreateLinkAndComment.cs create mode 100644 Infrastructure/Migrations/HNDbContextModelSnapshot.cs create mode 100644 Infrastructure/Repositories/EntityFramework/CommentRepository.cs create mode 100644 Infrastructure/Repositories/EntityFramework/LinkRepository.cs diff --git a/Apps/Api/Startup.cs b/Apps/Api/Startup.cs index e7aef1e..83430b7 100644 --- a/Apps/Api/Startup.cs +++ b/Apps/Api/Startup.cs @@ -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(); diff --git a/Apps/Website/Startup.cs b/Apps/Website/Startup.cs index b0d73b7..9c5fc09 100644 --- a/Apps/Website/Startup.cs +++ b/Apps/Website/Startup.cs @@ -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(); diff --git a/Infrastructure/EntityTypes/CommentEntityType.cs b/Infrastructure/EntityTypes/CommentEntityType.cs new file mode 100644 index 0000000..d28cc24 --- /dev/null +++ b/Infrastructure/EntityTypes/CommentEntityType.cs @@ -0,0 +1,21 @@ +using Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace Infrastructure.EntityTypes +{ + public class CommentEntityType : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Content).IsRequired(); + builder.Property(o => o.CreatedAt).IsRequired(); + builder.HasOne() + .WithMany() + .HasForeignKey(o => o.LinkId) + .IsRequired() + .OnDelete(DeleteBehavior.Cascade); + } + } +} \ No newline at end of file diff --git a/Infrastructure/EntityTypes/LinkEntityType.cs b/Infrastructure/EntityTypes/LinkEntityType.cs new file mode 100644 index 0000000..0798d76 --- /dev/null +++ b/Infrastructure/EntityTypes/LinkEntityType.cs @@ -0,0 +1,16 @@ +using Domain; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace Infrastructure.EntityTypes +{ + public class LinkEntityType : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(o => o.Id); + builder.Property(o => o.Url).HasMaxLength(250).IsRequired(); + builder.Property(o => o.CreatedAt).IsRequired(); + } + } +} \ No newline at end of file diff --git a/Infrastructure/HNDbContext.cs b/Infrastructure/HNDbContext.cs new file mode 100644 index 0000000..6997ea6 --- /dev/null +++ b/Infrastructure/HNDbContext.cs @@ -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 Links { get; set; } + public DbSet Comments { get; set; } + + IQueryable IData.Links => Links; + + IQueryable IData.Comments => Comments; + + public HNDbContext() : base() + { + + } + + public HNDbContext(DbContextOptions 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:"); + } + } + } +} \ No newline at end of file diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index 1ba9e05..496090f 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -8,6 +8,14 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + net5.0 diff --git a/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs b/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs new file mode 100644 index 0000000..4a19f9e --- /dev/null +++ b/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.Designer.cs @@ -0,0 +1,74 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("LinkId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("LinkId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("Domain.Link", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("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 + } + } +} diff --git a/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.cs b/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.cs new file mode 100644 index 0000000..e13abcd --- /dev/null +++ b/Infrastructure/Migrations/20210428140332_CreateLinkAndComment.cs @@ -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(type: "TEXT", nullable: false), + Url = table.Column(type: "TEXT", maxLength: 250, nullable: false), + CreatedAt = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Links", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + LinkId = table.Column(type: "TEXT", nullable: false), + Content = table.Column(type: "TEXT", nullable: false), + CreatedAt = table.Column(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"); + } + } +} diff --git a/Infrastructure/Migrations/HNDbContextModelSnapshot.cs b/Infrastructure/Migrations/HNDbContextModelSnapshot.cs new file mode 100644 index 0000000..baf7ca5 --- /dev/null +++ b/Infrastructure/Migrations/HNDbContextModelSnapshot.cs @@ -0,0 +1,72 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Content") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("LinkId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("LinkId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("Domain.Link", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedAt") + .HasColumnType("TEXT"); + + b.Property("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 + } + } +} diff --git a/Infrastructure/Repositories/EntityFramework/CommentRepository.cs b/Infrastructure/Repositories/EntityFramework/CommentRepository.cs new file mode 100644 index 0000000..a9b5755 --- /dev/null +++ b/Infrastructure/Repositories/EntityFramework/CommentRepository.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/Infrastructure/Repositories/EntityFramework/LinkRepository.cs b/Infrastructure/Repositories/EntityFramework/LinkRepository.cs new file mode 100644 index 0000000..850bf25 --- /dev/null +++ b/Infrastructure/Repositories/EntityFramework/LinkRepository.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Infrastructure/ServiceCollectionExtensions.cs b/Infrastructure/ServiceCollectionExtensions.cs index a6dd769..21fb6eb 100644 --- a/Infrastructure/ServiceCollectionExtensions.cs +++ b/Infrastructure/ServiceCollectionExtensions.cs @@ -1,12 +1,30 @@ 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) + public static IServiceCollection AddHNServicesEF(this IServiceCollection services) + { + services.AddDbContext((provider, options) => + { + var configuration = provider.GetRequiredService(); + + options.UseSqlite(configuration.GetConnectionString("Default")); + }); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + return services.AddCommon(); + } + + 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 +45,13 @@ namespace Infrastructure var memoryCommentRepository = serviceProvider.GetRequiredService() as Infrastructure.Repositories.Memory.CommentRepository; return new Infrastructure.Repositories.Memory.Data(memoryLinkRepository, memoryCommentRepository); }); + + + return services.AddCommon(); + } + + private static IServiceCollection AddCommon(this IServiceCollection services) + { services.AddTransient(); services.AddTransient();