Resolved #695 : Create a single method to configure EF core entity mapping by convention

pull/696/head
Yunus Emre Kalkan 6 years ago
parent 1bbd6d761b
commit d7e64a2645

@ -16,7 +16,18 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
{
b.TryConfigureConcurrencyStamp();
b.TryConfigureExtraProperties();
//TODO: Others
b.TryConfigureSoftDelete();
b.TryConfigureDeletionTime();
b.TryConfigureMayHaveCreator();
b.TryConfigureMustHaveCreator();
b.TryConfigureDeletionAudited();
b.TryConfigureCreationTime();
b.TryConfigureCreationAudited();
b.TryConfigureLastModificationTime();
b.TryConfigureModificationAudited();
b.TryConfigureAudited();
b.TryConfigureFullAudited();
b.TryConfigureMultiTenant();
}
public static void ConfigureConcurrencyStamp<T>(this EntityTypeBuilder<T> b)
@ -30,7 +41,7 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
if (b.Metadata.ClrType.IsAssignableTo<IHasConcurrencyStamp>())
{
//TODO: Max length?
b.Property<string>(nameof(IHasConcurrencyStamp.ConcurrencyStamp))
b.Property(nameof(IHasConcurrencyStamp.ConcurrencyStamp))
.IsConcurrencyToken()
.HasColumnName(nameof(IHasConcurrencyStamp.ConcurrencyStamp));
}
@ -56,103 +67,197 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
}
}
//TODO: Others like ConfigureConcurrencyStamp/TryConfigureConcurrencyStamp and ConfigureExtraProperties/TryConfigureExtraProperties
public static void ConfigureSoftDelete<T>(this EntityTypeBuilder<T> b)
where T : class, ISoftDelete
{
b.Property(x => x.IsDeleted)
.IsRequired()
.HasDefaultValue(false)
.HasColumnName(nameof(ISoftDelete.IsDeleted));
b.As<EntityTypeBuilder>().TryConfigureSoftDelete();
}
public static void TryConfigureSoftDelete(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<ISoftDelete>())
{
b.Property(nameof(ISoftDelete.IsDeleted))
.IsRequired()
.HasDefaultValue(false)
.HasColumnName(nameof(ISoftDelete.IsDeleted));
}
}
public static void ConfigureDeletionTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasDeletionTime
{
b.ConfigureSoftDelete();
b.Property(x => x.DeletionTime)
.IsRequired(false)
.HasColumnName(nameof(IHasDeletionTime.DeletionTime));
b.As<EntityTypeBuilder>().TryConfigureDeletionTime();
}
public static void TryConfigureDeletionTime(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IHasDeletionTime>())
{
b.TryConfigureSoftDelete();
b.Property(nameof(IHasDeletionTime.DeletionTime))
.IsRequired(false)
.HasColumnName(nameof(IHasDeletionTime.DeletionTime));
}
}
public static void ConfigureMayHaveCreator<T>(this EntityTypeBuilder<T> b)
where T : class, IMayHaveCreator
{
b.Property(x => x.CreatorId)
.IsRequired(false)
.HasColumnName(nameof(IMayHaveCreator.CreatorId));
b.As<EntityTypeBuilder>().TryConfigureMayHaveCreator();
}
public static void TryConfigureMayHaveCreator(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IMayHaveCreator>())
{
b.Property(nameof(IMayHaveCreator.CreatorId))
.IsRequired(false)
.HasColumnName(nameof(IMayHaveCreator.CreatorId));
}
}
public static void ConfigureMustHaveCreator<T>(this EntityTypeBuilder<T> b)
where T : class, IMustHaveCreator
{
b.Property(x => x.CreatorId)
.IsRequired()
.HasColumnName(nameof(IMustHaveCreator.CreatorId));
b.As<EntityTypeBuilder>().TryConfigureMustHaveCreator();
}
public static void TryConfigureMustHaveCreator(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IMustHaveCreator>())
{
b.Property(nameof(IMustHaveCreator.CreatorId))
.IsRequired()
.HasColumnName(nameof(IMustHaveCreator.CreatorId));
}
}
public static void ConfigureDeletionAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IDeletionAuditedObject
{
b.ConfigureDeletionTime();
b.Property(x => x.DeleterId)
.IsRequired(false)
.HasColumnName(nameof(IDeletionAuditedObject.DeleterId));
b.As<EntityTypeBuilder>().TryConfigureDeletionAudited();
}
public static void TryConfigureDeletionAudited(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IDeletionAuditedObject>())
{
b.TryConfigureDeletionTime();
b.Property(nameof(IDeletionAuditedObject.DeleterId))
.IsRequired(false)
.HasColumnName(nameof(IDeletionAuditedObject.DeleterId));
}
}
public static void ConfigureCreationTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasCreationTime
{
b.Property(x => x.CreationTime)
.IsRequired()
.HasColumnName(nameof(IHasCreationTime.CreationTime));
b.As<EntityTypeBuilder>().TryConfigureCreationTime();
}
public static void TryConfigureCreationTime(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IHasCreationTime>())
{
b.Property(nameof(IHasCreationTime.CreationTime))
.IsRequired()
.HasColumnName(nameof(IHasCreationTime.CreationTime));
}
}
public static void ConfigureCreationAudited<T>(this EntityTypeBuilder<T> b)
where T : class, ICreationAuditedObject
{
b.ConfigureCreationTime();
b.ConfigureMayHaveCreator();
b.As<EntityTypeBuilder>().TryConfigureCreationAudited();
}
public static void TryConfigureCreationAudited(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<ICreationAuditedObject>())
{
b.As<EntityTypeBuilder>().TryConfigureCreationTime();
b.As<EntityTypeBuilder>().TryConfigureMayHaveCreator();
}
}
public static void ConfigureLastModificationTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasModificationTime
{
b.Property(x => x.LastModificationTime)
.IsRequired(false)
.HasColumnName(nameof(IHasModificationTime.LastModificationTime));
b.As<EntityTypeBuilder>().TryConfigureLastModificationTime();
}
public static void TryConfigureLastModificationTime(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IHasModificationTime>())
{
b.Property(nameof(IHasModificationTime.LastModificationTime))
.IsRequired(false)
.HasColumnName(nameof(IHasModificationTime.LastModificationTime));
}
}
public static void ConfigureModificationAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IModificationAuditedObject
{
b.ConfigureLastModificationTime();
b.Property(x => x.LastModifierId)
.IsRequired(false)
.HasColumnName(nameof(IModificationAuditedObject.LastModifierId));
b.As<EntityTypeBuilder>().TryConfigureModificationAudited();
}
public static void TryConfigureModificationAudited(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IModificationAuditedObject>())
{
b.As<EntityTypeBuilder>().TryConfigureLastModificationTime();
b.Property(nameof(IModificationAuditedObject.LastModifierId))
.IsRequired(false)
.HasColumnName(nameof(IModificationAuditedObject.LastModifierId));
}
}
public static void ConfigureAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IAuditedObject
{
b.ConfigureCreationAudited();
b.ConfigureModificationAudited();
b.As<EntityTypeBuilder>().TryConfigureAudited();
}
public static void TryConfigureAudited(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IAuditedObject>())
{
b.As<EntityTypeBuilder>().TryConfigureCreationAudited();
b.As<EntityTypeBuilder>().TryConfigureModificationAudited();
}
}
public static void ConfigureFullAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IFullAuditedObject
{
b.ConfigureAudited();
b.ConfigureDeletionAudited();
b.As<EntityTypeBuilder>().TryConfigureFullAudited();
}
public static void TryConfigureFullAudited(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IFullAuditedObject>())
{
b.As<EntityTypeBuilder>().TryConfigureAudited();
b.As<EntityTypeBuilder>().TryConfigureDeletionAudited();
}
}
public static void ConfigureMultiTenant<T>(this EntityTypeBuilder<T> b)
where T : class, IMultiTenant
{
b.Property(x => x.TenantId)
.IsRequired(false)
.HasColumnName(nameof(IMultiTenant.TenantId));
b.As<EntityTypeBuilder>().TryConfigureMultiTenant();
}
public static void TryConfigureMultiTenant(this EntityTypeBuilder b)
{
if (b.Metadata.ClrType.IsAssignableTo<IMultiTenant>())
{
b.Property(nameof(IMultiTenant.TenantId))
.IsRequired(false)
.HasColumnName(nameof(IMultiTenant.TenantId));
}
}
//TODO: Add other interfaces (IAuditedObject<TUser>...)

Loading…
Cancel
Save