Browse Source

Resolved #684: Automatically configure audit and other interfaces for EF core model.

pull/696/head
Halil ibrahim Kalkan 8 years ago
parent
commit
219fba719d
  1. 112
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 41
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs

112
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -315,15 +316,17 @@ namespace Volo.Abp.EntityFrameworkCore
protected virtual void ConfigureBaseProperties<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) protected virtual void ConfigureBaseProperties<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class where TEntity : class
{ {
ConfigureConcurrencyStamp<TEntity>(modelBuilder, mutableEntityType); ConfigureConcurrencyStampProperty<TEntity>(modelBuilder, mutableEntityType);
ConfigureExtraProperties<TEntity>(modelBuilder, mutableEntityType); ConfigureExtraProperties<TEntity>(modelBuilder, mutableEntityType);
ConfigureAuditProperties<TEntity>(modelBuilder, mutableEntityType);
ConfigureTenantIdProperty<TEntity>(modelBuilder, mutableEntityType);
ConfigureGlobalFilters<TEntity>(modelBuilder, mutableEntityType); ConfigureGlobalFilters<TEntity>(modelBuilder, mutableEntityType);
} }
protected virtual void ConfigureConcurrencyStamp<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) protected virtual void ConfigureConcurrencyStampProperty<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class where TEntity : class
{ {
if (!typeof(IHasConcurrencyStamp).GetTypeInfo().IsAssignableFrom(typeof(TEntity))) if (!typeof(IHasConcurrencyStamp).IsAssignableFrom(typeof(TEntity)))
{ {
return; return;
} }
@ -339,7 +342,7 @@ namespace Volo.Abp.EntityFrameworkCore
protected virtual void ConfigureExtraProperties<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) protected virtual void ConfigureExtraProperties<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class where TEntity : class
{ {
if (!typeof(IHasExtraProperties).GetTypeInfo().IsAssignableFrom(typeof(TEntity))) if (!typeof(IHasExtraProperties).IsAssignableFrom(typeof(TEntity)))
{ {
return; return;
} }
@ -355,6 +358,107 @@ namespace Volo.Abp.EntityFrameworkCore
}); });
} }
protected virtual void ConfigureAuditProperties<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class
{
if (typeof(TEntity).IsAssignableTo<IHasCreationTime>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IHasCreationTime)x).CreationTime)
.IsRequired()
.HasColumnName(nameof(IHasCreationTime.CreationTime));
});
}
if (typeof(TEntity).IsAssignableTo<IMayHaveCreator>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IMayHaveCreator)x).CreatorId)
.IsRequired(false)
.HasColumnName(nameof(IMayHaveCreator.CreatorId));
});
}
if (typeof(TEntity).IsAssignableTo<IMustHaveCreator>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IMustHaveCreator)x).CreatorId)
.IsRequired()
.HasColumnName(nameof(IMustHaveCreator.CreatorId));
});
}
if (typeof(TEntity).IsAssignableTo<IHasModificationTime>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IHasModificationTime)x).LastModificationTime)
.IsRequired(false)
.HasColumnName(nameof(IHasModificationTime.LastModificationTime));
});
}
if (typeof(TEntity).IsAssignableTo<IModificationAuditedObject>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IModificationAuditedObject)x).LastModifierId)
.IsRequired(false)
.HasColumnName(nameof(IModificationAuditedObject.LastModifierId));
});
}
if (typeof(TEntity).IsAssignableTo<ISoftDelete>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((ISoftDelete) x).IsDeleted)
.IsRequired()
.HasDefaultValue(false)
.HasColumnName(nameof(ISoftDelete.IsDeleted));
});
}
if (typeof(TEntity).IsAssignableTo<IHasDeletionTime>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IHasDeletionTime)x).DeletionTime)
.IsRequired(false)
.HasColumnName(nameof(IHasDeletionTime.DeletionTime));
});
}
if (typeof(TEntity).IsAssignableTo<IDeletionAuditedObject>())
{
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IDeletionAuditedObject)x).DeleterId)
.IsRequired(false)
.HasColumnName(nameof(IDeletionAuditedObject.DeleterId));
});
}
}
protected virtual void ConfigureTenantIdProperty<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class
{
if (!typeof(TEntity).IsAssignableTo<IMultiTenant>())
{
return;
}
modelBuilder.Entity<TEntity>(b =>
{
b.Property(x => ((IMultiTenant)x).TenantId)
.IsRequired(false)
.HasColumnName(nameof(IMultiTenant.TenantId));
});
}
protected virtual void ConfigureGlobalFilters<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) protected virtual void ConfigureGlobalFilters<TEntity>(ModelBuilder modelBuilder, IMutableEntityType mutableEntityType)
where TEntity : class where TEntity : class
{ {

41
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs

@ -33,59 +33,76 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
public static void ConfigureSoftDelete<T>(this EntityTypeBuilder<T> b) public static void ConfigureSoftDelete<T>(this EntityTypeBuilder<T> b)
where T : class, ISoftDelete where T : class, ISoftDelete
{ {
b.Property(x => x.IsDeleted).IsRequired().HasDefaultValue(false).HasColumnName(nameof(ISoftDelete.IsDeleted)); b.Property(x => x.IsDeleted)
.IsRequired()
.HasDefaultValue(false)
.HasColumnName(nameof(ISoftDelete.IsDeleted));
} }
public static void ConfigureDeletionTime<T>(this EntityTypeBuilder<T> b) public static void ConfigureDeletionTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasDeletionTime where T : class, IHasDeletionTime
{ {
b.ConfigureSoftDelete(); b.ConfigureSoftDelete();
b.Property(x => x.DeletionTime).IsRequired(false).HasColumnName(nameof(IHasDeletionTime.DeletionTime)); b.Property(x => x.DeletionTime)
.IsRequired(false)
.HasColumnName(nameof(IHasDeletionTime.DeletionTime));
} }
public static void ConfigureMayHaveCreator<T>(this EntityTypeBuilder<T> b) public static void ConfigureMayHaveCreator<T>(this EntityTypeBuilder<T> b)
where T : class, IMayHaveCreator where T : class, IMayHaveCreator
{ {
b.Property(x => x.CreatorId).IsRequired(false).HasColumnName(nameof(IMayHaveCreator.CreatorId)); b.Property(x => x.CreatorId)
.IsRequired(false)
.HasColumnName(nameof(IMayHaveCreator.CreatorId));
} }
public static void ConfigureMustHaveCreator<T>(this EntityTypeBuilder<T> b) public static void ConfigureMustHaveCreator<T>(this EntityTypeBuilder<T> b)
where T : class, IMustHaveCreator where T : class, IMustHaveCreator
{ {
b.Property(x => x.CreatorId).IsRequired().HasColumnName(nameof(IMustHaveCreator.CreatorId)); b.Property(x => x.CreatorId)
.IsRequired()
.HasColumnName(nameof(IMustHaveCreator.CreatorId));
} }
public static void ConfigureDeletionAudited<T>(this EntityTypeBuilder<T> b) public static void ConfigureDeletionAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IDeletionAuditedObject where T : class, IDeletionAuditedObject
{ {
b.ConfigureDeletionTime(); b.ConfigureDeletionTime();
b.Property(x => x.DeleterId).IsRequired(false).HasColumnName(nameof(IDeletionAuditedObject.DeleterId)); b.Property(x => x.DeleterId)
.IsRequired(false)
.HasColumnName(nameof(IDeletionAuditedObject.DeleterId));
} }
public static void ConfigureCreationTime<T>(this EntityTypeBuilder<T> b) public static void ConfigureCreationTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasCreationTime where T : class, IHasCreationTime
{ {
b.Property(x => x.CreationTime).IsRequired().HasColumnName(nameof(IHasCreationTime.CreationTime)); b.Property(x => x.CreationTime)
.IsRequired()
.HasColumnName(nameof(IHasCreationTime.CreationTime));
} }
public static void ConfigureCreationAudited<T>(this EntityTypeBuilder<T> b) public static void ConfigureCreationAudited<T>(this EntityTypeBuilder<T> b)
where T : class, ICreationAuditedObject where T : class, ICreationAuditedObject
{ {
b.ConfigureCreationTime(); b.ConfigureCreationTime();
b.Property(x => x.CreatorId).IsRequired(false).HasColumnName(nameof(ICreationAuditedObject.CreatorId)); b.ConfigureMayHaveCreator();
} }
public static void ConfigureLastModificationTime<T>(this EntityTypeBuilder<T> b) public static void ConfigureLastModificationTime<T>(this EntityTypeBuilder<T> b)
where T : class, IHasModificationTime where T : class, IHasModificationTime
{ {
b.Property(x => x.LastModificationTime).IsRequired(false).HasColumnName(nameof(IHasModificationTime.LastModificationTime)); b.Property(x => x.LastModificationTime)
.IsRequired(false)
.HasColumnName(nameof(IHasModificationTime.LastModificationTime));
} }
public static void ConfigureModificationAudited<T>(this EntityTypeBuilder<T> b) public static void ConfigureModificationAudited<T>(this EntityTypeBuilder<T> b)
where T : class, IModificationAuditedObject where T : class, IModificationAuditedObject
{ {
b.ConfigureLastModificationTime(); b.ConfigureLastModificationTime();
b.Property(x => x.LastModifierId).IsRequired(false).HasColumnName(nameof(IModificationAuditedObject.LastModifierId)); b.Property(x => x.LastModifierId)
.IsRequired(false)
.HasColumnName(nameof(IModificationAuditedObject.LastModifierId));
} }
public static void ConfigureAudited<T>(this EntityTypeBuilder<T> b) public static void ConfigureAudited<T>(this EntityTypeBuilder<T> b)
@ -105,9 +122,11 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
public static void ConfigureMultiTenant<T>(this EntityTypeBuilder<T> b) public static void ConfigureMultiTenant<T>(this EntityTypeBuilder<T> b)
where T : class, IMultiTenant where T : class, IMultiTenant
{ {
b.Property(x => x.TenantId).IsRequired(false).HasColumnName(nameof(IMultiTenant.TenantId)); b.Property(x => x.TenantId)
.IsRequired(false)
.HasColumnName(nameof(IMultiTenant.TenantId));
} }
//TODO: Add other interfaces (IMultiTenant, IAuditedObject<TUser>...) //TODO: Add other interfaces (IAuditedObject<TUser>...)
} }
} }

Loading…
Cancel
Save