From 75dfe9a602dfe5049b82f3dcd32ee0e3a0fc76f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 30 Mar 2018 10:00:23 +0300 Subject: [PATCH] Added AbpEntityTypeBuilderExtensions --- .../Volo.Abp.EntityFrameworkCore.csproj | 1 + .../AbpEntityTypeBuilderExtensions.cs | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo.Abp.EntityFrameworkCore.csproj b/src/Volo.Abp.EntityFrameworkCore/Volo.Abp.EntityFrameworkCore.csproj index b6cfcc2e61..4c4bcbcc57 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo.Abp.EntityFrameworkCore.csproj +++ b/src/Volo.Abp.EntityFrameworkCore/Volo.Abp.EntityFrameworkCore.csproj @@ -15,6 +15,7 @@ + diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs new file mode 100644 index 0000000000..0893c3007b --- /dev/null +++ b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Newtonsoft.Json; +using Volo.Abp.Auditing; +using Volo.Abp.Data; + +namespace Volo.Abp.EntityFrameworkCore.Modeling +{ + public static class AbpEntityTypeBuilderExtensions + { + public static void ConfigureExtraProperties(this EntityTypeBuilder b) + where T : class, IHasExtraProperties + { + b.Property(u => u.ExtraProperties) + .HasConversion( + d => JsonConvert.SerializeObject(d, Formatting.None), + s => JsonConvert.DeserializeObject>(s) + ); + } + + public static void ConfigureSoftDelete(this EntityTypeBuilder b) + where T : class, ISoftDelete + { + b.Property(x => x.IsDeleted).IsRequired().HasDefaultValue(false).HasColumnName(nameof(ISoftDelete.IsDeleted)); + } + + public static void ConfigureDeletionTime(this EntityTypeBuilder b) + where T : class, IHasDeletionTime + { + b.ConfigureSoftDelete(); + b.Property(x => x.DeletionTime).IsRequired(false).HasColumnName(nameof(IHasDeletionTime.DeletionTime)); + } + + public static void ConfigureDeletionAudited(this EntityTypeBuilder b) + where T : class, IDeletionAudited + { + b.ConfigureDeletionTime(); + b.Property(x => x.DeleterId).IsRequired(false).HasColumnName(nameof(IDeletionAudited.DeleterId)); + } + + public static void ConfigureCreationTime(this EntityTypeBuilder b) + where T : class, IHasCreationTime + { + b.Property(x => x.CreationTime).IsRequired().HasColumnName(nameof(IHasCreationTime.CreationTime)); + } + + public static void ConfigureCreationAudited(this EntityTypeBuilder b) + where T : class, ICreationAudited + { + b.ConfigureCreationTime(); + b.Property(x => x.CreatorId).IsRequired(false).HasColumnName(nameof(ICreationAudited.CreatorId)); + } + + public static void ConfigureLastModificationTime(this EntityTypeBuilder b) + where T : class, IHasModificationTime + { + b.Property(x => x.LastModificationTime).IsRequired(false).HasColumnName(nameof(IHasModificationTime.LastModificationTime)); + } + + public static void ConfigureModificationAudited(this EntityTypeBuilder b) + where T : class, IModificationAudited + { + b.ConfigureLastModificationTime(); + b.Property(x => x.LastModifierId).IsRequired(false).HasColumnName(nameof(IModificationAudited.LastModifierId)); + } + + public static void ConfigureAudited(this EntityTypeBuilder b) + where T : class, IAudited + { + b.ConfigureCreationAudited(); + b.ConfigureModificationAudited(); + } + + public static void ConfigureFullAudited(this EntityTypeBuilder b) + where T : class, IFullAudited + { + b.ConfigureAudited(); + b.ConfigureDeletionAudited(); + } + } +}