Browse Source

Refactor shared entity implementation and tests

pull/23588/head
maliming 1 year ago
parent
commit
00d6120fdc
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 52
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs
  2. 114
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/SharedEntity_Repository_Tests.cs
  3. 14
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs
  4. 15
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs
  5. 26
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs

52
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs

@ -54,55 +54,3 @@ public abstract class Entity<TKey> : Entity, IEntity<TKey>
return $"[ENTITY: {GetType().Name}] Id = {Id}"; return $"[ENTITY: {GetType().Name}] Id = {Id}";
} }
} }
/// <inheritdoc/>
[Serializable]
public abstract class SharedEntity : Dictionary<string, object>, IEntity
{
protected SharedEntity()
{
EntityHelper.TrySetTenantId(this);
}
/// <inheritdoc/>
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Keys = {GetKeys().JoinAsString(", ")}";
}
public abstract object?[] GetKeys();
public bool EntityEquals(IEntity other)
{
return EntityHelper.EntityEquals(this, other);
}
}
/// <inheritdoc cref="IEntity{TKey}" />
[Serializable]
public abstract class SharedEntity<TKey> : SharedEntity, IEntity<TKey>
{
/// <inheritdoc/>
public virtual TKey Id { get; protected set; } = default!;
protected SharedEntity()
{
}
protected SharedEntity(TKey id)
{
Id = id;
}
public override object?[] GetKeys()
{
return new object?[] { Id };
}
/// <inheritdoc/>
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Id = {Id}";
}
}

114
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/SharedEntity_Repository_Tests.cs

@ -15,13 +15,13 @@ public class SharedEntity_Repository_Tests : EntityFrameworkCoreTestBase
{ {
protected readonly IRepository<TestSharedEntity> TestSharedTypeEntityRepository; protected readonly IRepository<TestSharedEntity> TestSharedTypeEntityRepository;
protected readonly ICurrentTenant CurrentTenant; protected readonly ICurrentTenant CurrentTenant;
protected readonly IDataFilter<ISoftDelete> dataFilter; protected readonly IDataFilter<ISoftDelete> DataFilter;
public SharedEntity_Repository_Tests() public SharedEntity_Repository_Tests()
{ {
TestSharedTypeEntityRepository = GetRequiredService<IRepository<TestSharedEntity>>(); TestSharedTypeEntityRepository = GetRequiredService<IRepository<TestSharedEntity>>();
CurrentTenant = GetRequiredService<ICurrentTenant>(); CurrentTenant = GetRequiredService<ICurrentTenant>();
dataFilter = GetRequiredService<IDataFilter<ISoftDelete>>(); DataFilter = GetRequiredService<IDataFilter<ISoftDelete>>();
} }
[Fact] [Fact]
@ -38,90 +38,86 @@ public class SharedEntity_Repository_Tests : EntityFrameworkCoreTestBase
{ {
TenantId = null, TenantId = null,
IsDeleted = false, IsDeleted = false,
Name = "Test Person1",
["Name"] = "Test Person1", Age = 10,
["Age"] = 10, Birthday = DateTime.Now
["Birthday"] = DateTime.Now
}.SetProperty("testProperty", "Test Value1"), }.SetProperty("testProperty", "Test Value1"),
new TestSharedEntity(Guid.NewGuid()) new TestSharedEntity(Guid.NewGuid())
{ {
TenantId = tenantId, TenantId = tenantId,
IsDeleted = false, IsDeleted = false,
Name = "Test Person2",
["Name"] = "Test Person2", Age = 20,
["Age"] = 20, Birthday = DateTime.Now
["Birthday"] = DateTime.Now
}, },
new TestSharedEntity(Guid.NewGuid()) new TestSharedEntity(Guid.NewGuid())
{ {
TenantId = tenantId, TenantId = tenantId,
IsDeleted = true, IsDeleted = true,
Name = "Test Person3",
["Name"] = "Test Person3", Age = 30,
["Age"] = 30, Birthday = DateTime.Now
["Birthday"] = DateTime.Now
}, },
new TestSharedEntity(Guid.NewGuid()) new TestSharedEntity(Guid.NewGuid())
{ {
TenantId = null, TenantId = null,
IsDeleted = true, IsDeleted = true,
Name = "Test Person4",
["Name"] = "Test Person4", Age = 40,
["Age"] = 40, Birthday = DateTime.Now
["Birthday"] = DateTime.Now
} }
}, true); }, true);
var entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x["Name"]).ToList(); var entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x.Name).ToList();
entities.Count.ShouldBe(1); entities.Count.ShouldBe(1);
entities[0].TenantId.ShouldBeNull(); entities[0].TenantId.ShouldBeNull();
entities[0].IsDeleted.ShouldBe(false); entities[0].IsDeleted.ShouldBe(false);
entities[0]["Name"].ShouldBe("Test Person1"); entities[0].Name.ShouldBe("Test Person1");
entities[0]["Age"].ShouldBe(10); entities[0].Age.ShouldBe(10);
entities[0].GetProperty("testProperty").ShouldBe("Test Value1"); entities[0].GetProperty("testProperty").ShouldBe("Test Value1");
using (CurrentTenant.Change(tenantId)) using (CurrentTenant.Change(tenantId))
{ {
entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x["Name"]).ToList(); entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x.Name).ToList();
entities.Count.ShouldBe(1); entities.Count.ShouldBe(1);
entities[0].TenantId.ShouldBe(tenantId); entities[0].TenantId.ShouldBe(tenantId);
entities[0].IsDeleted.ShouldBe(false); entities[0].IsDeleted.ShouldBe(false);
entities[0]["Name"].ShouldBe("Test Person2"); entities[0].Name.ShouldBe("Test Person2");
entities[0]["Age"].ShouldBe(20); entities[0].Age.ShouldBe(20);
} }
using (dataFilter.Disable()) using (DataFilter.Disable())
{ {
entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x["Name"]).ToList(); entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x.Name).ToList();
entities.Count.ShouldBe(2); entities.Count.ShouldBe(2);
entities[0].TenantId.ShouldBeNull(); entities[0].TenantId.ShouldBeNull();
entities[0].IsDeleted.ShouldBe(false); entities[0].IsDeleted.ShouldBe(false);
entities[0]["Name"].ShouldBe("Test Person1"); entities[0].Name.ShouldBe("Test Person1");
entities[0]["Age"].ShouldBe(10); entities[0].Age.ShouldBe(10);
entities[1].TenantId.ShouldBeNull(); entities[1].TenantId.ShouldBeNull();
entities[1].IsDeleted.ShouldBe(true); entities[1].IsDeleted.ShouldBe(true);
entities[1]["Name"].ShouldBe("Test Person4"); entities[1].Name.ShouldBe("Test Person4");
entities[1]["Age"].ShouldBe(40); entities[1].Age.ShouldBe(40);
} }
using (CurrentTenant.Change(tenantId)) using (CurrentTenant.Change(tenantId))
{ {
using (dataFilter.Disable()) using (DataFilter.Disable())
{ {
entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x["Name"]).ToList(); entities = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x.Name).ToList();
entities.Count.ShouldBe(2); entities.Count.ShouldBe(2);
entities[0].TenantId.ShouldBe(tenantId); entities[0].TenantId.ShouldBe(tenantId);
entities[0].IsDeleted.ShouldBe(false); entities[0].IsDeleted.ShouldBe(false);
entities[0]["Name"].ShouldBe("Test Person2"); entities[0].Name.ShouldBe("Test Person2");
entities[0]["Age"].ShouldBe(20); entities[0].Age.ShouldBe(20);
entities[1].TenantId.ShouldBe(tenantId); entities[1].TenantId.ShouldBe(tenantId);
entities[1].IsDeleted.ShouldBe(true); entities[1].IsDeleted.ShouldBe(true);
entities[1]["Name"].ShouldBe("Test Person3"); entities[1].Name.ShouldBe("Test Person3");
entities[1]["Age"].ShouldBe(30); entities[1].Age.ShouldBe(30);
} }
} }
@ -130,18 +126,52 @@ public class SharedEntity_Repository_Tests : EntityFrameworkCoreTestBase
{ {
new TestSharedEntity(Guid.NewGuid()) new TestSharedEntity(Guid.NewGuid())
{ {
["Name"] = "Test Person1 from Second Table", Name = "Test Person1 from Second Table",
["Age"] = 110, Age = 110,
["Birthday"] = DateTime.Now Birthday = DateTime.Now
} }
}, true); }, true);
var entitiesFromSecondTable = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x["Name"]).ToList(); var entitiesFromSecondTable = (await TestSharedTypeEntityRepository.GetListAsync()).OrderBy(x => x.Name).ToList();
entitiesFromSecondTable.Count.ShouldBe(1); entitiesFromSecondTable.Count.ShouldBe(1);
entitiesFromSecondTable[0].TenantId.ShouldBeNull(); entitiesFromSecondTable[0].TenantId.ShouldBeNull();
entitiesFromSecondTable[0].IsDeleted.ShouldBe(false); entitiesFromSecondTable[0].IsDeleted.ShouldBe(false);
entitiesFromSecondTable[0]["Name"].ShouldBe("Test Person1 from Second Table"); entitiesFromSecondTable[0].Name.ShouldBe("Test Person1 from Second Table");
entitiesFromSecondTable[0]["Age"].ShouldBe(110); entitiesFromSecondTable[0].Age.ShouldBe(110);
});
}
[Fact]
public async Task SharedEntity_DynamicProperty_Test()
{
await WithUnitOfWorkAsync(async () =>
{
TestSharedTypeEntityRepository.SetCustomEntityName("TestSharedEntity1");
var entity = new TestSharedEntity(Guid.NewGuid())
{
TenantId = null,
IsDeleted = false,
Name = "Test Person1",
Age = 10,
Birthday = DateTime.Now
};
entity["DynamicProperty"] = "Test Value1";
await TestSharedTypeEntityRepository.InsertAsync(entity, true);
entity = await TestSharedTypeEntityRepository.FindAsync(x => x.Id == entity.Id!);
entity.ShouldNotBeNull();
entity.Name.ShouldBe("Test Person1");
entity.Age.ShouldBe(10);
entity.Birthday.ShouldNotBeNull();
entity["DynamicProperty"].ShouldBe("Test Value1");
TestSharedTypeEntityRepository.SetCustomEntityName("TestSharedEntity2");
entity = await TestSharedTypeEntityRepository.FindAsync(x => x.Id == entity.Id!);
entity.ShouldBeNull();
}); });
} }
} }

14
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs

@ -52,12 +52,14 @@ public class TestMigrationsDbContext : AbpDbContext<TestMigrationsDbContext>
Action<EntityTypeBuilder<TestSharedEntity>> sharedEntityBuildAction = b => Action<EntityTypeBuilder<TestSharedEntity>> sharedEntityBuildAction = b =>
{ {
b.ConfigureByConvention(); b.ConfigureByConvention();
b.Property<Guid>("Id"); b.Property(x => x.Id);
b.Property<Guid?>("TenantId"); b.Property(x => x.TenantId);
b.Property<bool>("IsDeleted"); b.Property(x => x.IsDeleted);
b.Property<string>("Name"); b.Property(x => x.Name);
b.Property<int>("Age"); b.Property(x => x.Age);
b.Property<DateTime?>("Birthday"); b.Property(x => x.Birthday);
b.Property<string>("DynamicProperty");
}; };
modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction);
modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction);

15
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs

@ -2,7 +2,6 @@ using System;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling; using Volo.Abp.EntityFrameworkCore.Modeling;
@ -65,12 +64,14 @@ public class TestAppDbContext : AbpDbContext<TestAppDbContext>, IThirdDbContext,
Action<EntityTypeBuilder<TestSharedEntity>> sharedEntityBuildAction = b => Action<EntityTypeBuilder<TestSharedEntity>> sharedEntityBuildAction = b =>
{ {
b.ConfigureByConvention(); b.ConfigureByConvention();
b.Property<Guid>("Id"); b.Property(x => x.Id);
b.Property<Guid?>("TenantId"); b.Property(x => x.TenantId);
b.Property<bool>("IsDeleted"); b.Property(x => x.IsDeleted);
b.Property<string>("Name"); b.Property(x => x.Name);
b.Property<int>("Age"); b.Property(x => x.Age);
b.Property<DateTime?>("Birthday"); b.Property(x => x.Birthday);
b.Property<string>("DynamicProperty");
}; };
modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction);
modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction);

26
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs

@ -1,28 +1,38 @@
using System; using System;
using Volo.Abp.Data; using System.Collections.Generic;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
namespace Volo.Abp.TestApp.Domain; namespace Volo.Abp.TestApp.Domain;
public class TestSharedEntity : SharedEntity<Guid>, IMultiTenant, ISoftDelete, IHasExtraProperties public class TestSharedEntity : AggregateRoot<Guid>, IMultiTenant, ISoftDelete
{ {
private readonly Dictionary<string, object> _dynamicPropertites = new();
public object this[string key]
{
get => _dynamicPropertites.GetValueOrDefault(key);
set => _dynamicPropertites[key] = value;
}
public Guid? TenantId { get; set; } public Guid? TenantId { get; set; }
public virtual string Name { get; set; }
public virtual int Age { get; set; }
public virtual DateTime? Birthday { get; set; }
public bool IsDeleted { get; set; } public bool IsDeleted { get; set; }
public TestSharedEntity() public TestSharedEntity()
{ {
ExtraProperties = new ExtraPropertyDictionary();
this.SetDefaultsForExtraProperties();
} }
public TestSharedEntity(Guid id) public TestSharedEntity(Guid id)
: base(id) : base(id)
{ {
ExtraProperties = new ExtraPropertyDictionary();
this.SetDefaultsForExtraProperties();
}
public ExtraPropertyDictionary ExtraProperties { get; set; } }
} }

Loading…
Cancel
Save