Compare commits

...

1 Commits

Author SHA1 Message Date
YuukanOO
3624fc58ac Ajout implémentations Entity Framework Core 2021-04-28 16:30:44 +02:00
12 changed files with 373 additions and 3 deletions

View File

@ -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>();

View File

@ -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>();

View 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);
}
}
}

View 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();
}
}
}

View 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:");
}
}
}
}

View File

@ -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>

View 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
}
}
}

View File

@ -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");
}
}
}

View 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
}
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<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();
}
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<ICommentRepository>() 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<LinkService>();
services.AddTransient<CommentService>();