diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs index 3bec827dac..9e52d1abfd 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Entity.cs @@ -54,55 +54,3 @@ public abstract class Entity : Entity, IEntity return $"[ENTITY: {GetType().Name}] Id = {Id}"; } } - -/// -[Serializable] -public abstract class SharedEntity : Dictionary, IEntity -{ - protected SharedEntity() - { - EntityHelper.TrySetTenantId(this); - } - - /// - 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); - } -} - -/// -[Serializable] -public abstract class SharedEntity : SharedEntity, IEntity -{ - /// - public virtual TKey Id { get; protected set; } = default!; - - protected SharedEntity() - { - - } - - protected SharedEntity(TKey id) - { - Id = id; - } - - public override object?[] GetKeys() - { - return new object?[] { Id }; - } - - /// - public override string ToString() - { - return $"[ENTITY: {GetType().Name}] Id = {Id}"; - } -} diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/SharedEntity_Repository_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/SharedEntity_Repository_Tests.cs index 699266a69d..abdc7a4e6d 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/SharedEntity_Repository_Tests.cs +++ b/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 TestSharedTypeEntityRepository; protected readonly ICurrentTenant CurrentTenant; - protected readonly IDataFilter dataFilter; + protected readonly IDataFilter DataFilter; public SharedEntity_Repository_Tests() { TestSharedTypeEntityRepository = GetRequiredService>(); CurrentTenant = GetRequiredService(); - dataFilter = GetRequiredService>(); + DataFilter = GetRequiredService>(); } [Fact] @@ -38,90 +38,86 @@ public class SharedEntity_Repository_Tests : EntityFrameworkCoreTestBase { TenantId = null, IsDeleted = false, - - ["Name"] = "Test Person1", - ["Age"] = 10, - ["Birthday"] = DateTime.Now + Name = "Test Person1", + Age = 10, + Birthday = DateTime.Now }.SetProperty("testProperty", "Test Value1"), new TestSharedEntity(Guid.NewGuid()) { TenantId = tenantId, IsDeleted = false, - - ["Name"] = "Test Person2", - ["Age"] = 20, - ["Birthday"] = DateTime.Now + Name = "Test Person2", + Age = 20, + Birthday = DateTime.Now }, new TestSharedEntity(Guid.NewGuid()) { TenantId = tenantId, IsDeleted = true, - - ["Name"] = "Test Person3", - ["Age"] = 30, - ["Birthday"] = DateTime.Now + Name = "Test Person3", + Age = 30, + Birthday = DateTime.Now }, new TestSharedEntity(Guid.NewGuid()) { TenantId = null, IsDeleted = true, - - ["Name"] = "Test Person4", - ["Age"] = 40, - ["Birthday"] = DateTime.Now + Name = "Test Person4", + Age = 40, + Birthday = DateTime.Now } }, 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[0].TenantId.ShouldBeNull(); entities[0].IsDeleted.ShouldBe(false); - entities[0]["Name"].ShouldBe("Test Person1"); - entities[0]["Age"].ShouldBe(10); + entities[0].Name.ShouldBe("Test Person1"); + entities[0].Age.ShouldBe(10); entities[0].GetProperty("testProperty").ShouldBe("Test Value1"); 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[0].TenantId.ShouldBe(tenantId); entities[0].IsDeleted.ShouldBe(false); - entities[0]["Name"].ShouldBe("Test Person2"); - entities[0]["Age"].ShouldBe(20); + entities[0].Name.ShouldBe("Test Person2"); + 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[0].TenantId.ShouldBeNull(); entities[0].IsDeleted.ShouldBe(false); - entities[0]["Name"].ShouldBe("Test Person1"); - entities[0]["Age"].ShouldBe(10); + entities[0].Name.ShouldBe("Test Person1"); + entities[0].Age.ShouldBe(10); entities[1].TenantId.ShouldBeNull(); entities[1].IsDeleted.ShouldBe(true); - entities[1]["Name"].ShouldBe("Test Person4"); - entities[1]["Age"].ShouldBe(40); + entities[1].Name.ShouldBe("Test Person4"); + entities[1].Age.ShouldBe(40); } 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[0].TenantId.ShouldBe(tenantId); entities[0].IsDeleted.ShouldBe(false); - entities[0]["Name"].ShouldBe("Test Person2"); - entities[0]["Age"].ShouldBe(20); + entities[0].Name.ShouldBe("Test Person2"); + entities[0].Age.ShouldBe(20); entities[1].TenantId.ShouldBe(tenantId); entities[1].IsDeleted.ShouldBe(true); - entities[1]["Name"].ShouldBe("Test Person3"); - entities[1]["Age"].ShouldBe(30); + entities[1].Name.ShouldBe("Test Person3"); + entities[1].Age.ShouldBe(30); } } @@ -130,18 +126,52 @@ public class SharedEntity_Repository_Tests : EntityFrameworkCoreTestBase { new TestSharedEntity(Guid.NewGuid()) { - ["Name"] = "Test Person1 from Second Table", - ["Age"] = 110, - ["Birthday"] = DateTime.Now + Name = "Test Person1 from Second Table", + Age = 110, + Birthday = DateTime.Now } }, 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[0].TenantId.ShouldBeNull(); entitiesFromSecondTable[0].IsDeleted.ShouldBe(false); - entitiesFromSecondTable[0]["Name"].ShouldBe("Test Person1 from Second Table"); - entitiesFromSecondTable[0]["Age"].ShouldBe(110); + entitiesFromSecondTable[0].Name.ShouldBe("Test Person1 from Second Table"); + 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(); }); } } diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs index 20524cd662..0efef5ba1f 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/TestMigrationsDbContext.cs @@ -52,12 +52,14 @@ public class TestMigrationsDbContext : AbpDbContext Action> sharedEntityBuildAction = b => { b.ConfigureByConvention(); - b.Property("Id"); - b.Property("TenantId"); - b.Property("IsDeleted"); - b.Property("Name"); - b.Property("Age"); - b.Property("Birthday"); + b.Property(x => x.Id); + b.Property(x => x.TenantId); + b.Property(x => x.IsDeleted); + b.Property(x => x.Name); + b.Property(x => x.Age); + b.Property(x => x.Birthday); + + b.Property("DynamicProperty"); }; modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction); diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs index bc43a9ba02..b2680abdff 100644 --- a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/TestApp/EntityFrameworkCore/TestAppDbContext.cs @@ -2,7 +2,6 @@ using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; -using Microsoft.Extensions.Logging; using Volo.Abp.DependencyInjection; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; @@ -65,12 +64,14 @@ public class TestAppDbContext : AbpDbContext, IThirdDbContext, Action> sharedEntityBuildAction = b => { b.ConfigureByConvention(); - b.Property("Id"); - b.Property("TenantId"); - b.Property("IsDeleted"); - b.Property("Name"); - b.Property("Age"); - b.Property("Birthday"); + b.Property(x => x.Id); + b.Property(x => x.TenantId); + b.Property(x => x.IsDeleted); + b.Property(x => x.Name); + b.Property(x => x.Age); + b.Property(x => x.Birthday); + + b.Property("DynamicProperty"); }; modelBuilder.SharedTypeEntity("TestSharedEntity1", sharedEntityBuildAction); modelBuilder.SharedTypeEntity("TestSharedEntity2", sharedEntityBuildAction); diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs index df8f5e17fa..5b1d94a316 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/TestSharedTypeEntity.cs @@ -1,28 +1,38 @@ using System; -using Volo.Abp.Data; +using System.Collections.Generic; using Volo.Abp.Domain.Entities; using Volo.Abp.MultiTenancy; namespace Volo.Abp.TestApp.Domain; -public class TestSharedEntity : SharedEntity, IMultiTenant, ISoftDelete, IHasExtraProperties +public class TestSharedEntity : AggregateRoot, IMultiTenant, ISoftDelete { + private readonly Dictionary _dynamicPropertites = new(); + + public object this[string key] + { + get => _dynamicPropertites.GetValueOrDefault(key); + set => _dynamicPropertites[key] = value; + } + 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 TestSharedEntity() { - ExtraProperties = new ExtraPropertyDictionary(); - this.SetDefaultsForExtraProperties(); + } public TestSharedEntity(Guid id) : base(id) { - ExtraProperties = new ExtraPropertyDictionary(); - this.SetDefaultsForExtraProperties(); - } - public ExtraPropertyDictionary ExtraProperties { get; set; } + } }