diff --git a/XCode/Code/EntityBuilder.cs b/XCode/Code/EntityBuilder.cs index f1910cc50..54d2dcb44 100644 --- a/XCode/Code/EntityBuilder.cs +++ b/XCode/Code/EntityBuilder.cs @@ -588,6 +588,7 @@ public override void Clear() //} var bs = baseClass?.Split(',').Select(e => e.Trim()).ToList() ?? []; + var hasTenantScope = bs.Any(e => e.EqualIgnoreCase("ITenantScope")); // 数据类的基类只有接口,业务类基类则比较复杂 var name = ""; @@ -599,6 +600,10 @@ public override void Clear() if (name.IsNullOrEmpty()) name = "Entity"; name = $"{name}<{ClassName}>"; + + // 多租户业务类实现租户作用域接口,便于 TenantInterceptor 生效 + if (Table.Columns.Any(e => e.Name.EqualIgnoreCase("TenantId")) && !hasTenantScope) + name += ", ITenantScope"; } else { diff --git "a/XUnitTest.XCode/Code/Entity/\347\247\237\346\210\267\345\205\263\347\263\273.Biz.cs" "b/XUnitTest.XCode/Code/Entity/\347\247\237\346\210\267\345\205\263\347\263\273.Biz.cs" index d415590b4..f059c8eb6 100644 --- "a/XUnitTest.XCode/Code/Entity/\347\247\237\346\210\267\345\205\263\347\263\273.Biz.cs" +++ "b/XUnitTest.XCode/Code/Entity/\347\247\237\346\210\267\345\205\263\347\263\273.Biz.cs" @@ -26,7 +26,7 @@ namespace XCode.Membership666; -public partial class TenantUser : Entity +public partial class TenantUser : Entity, ITenantScope { #region 对象操作 // 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存 diff --git "a/XUnitTest.XCode/Code/Entity/\350\247\222\350\211\262.Biz.cs" "b/XUnitTest.XCode/Code/Entity/\350\247\222\350\211\262.Biz.cs" index 81aef644e..2c01cff8b 100644 --- "a/XUnitTest.XCode/Code/Entity/\350\247\222\350\211\262.Biz.cs" +++ "b/XUnitTest.XCode/Code/Entity/\350\247\222\350\211\262.Biz.cs" @@ -26,7 +26,7 @@ namespace XCode.Membership666; -public partial class Role : Entity +public partial class Role : Entity, ITenantScope { #region 对象操作 // 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存 diff --git "a/XUnitTest.XCode/Code/Entity/\351\203\250\351\227\250.Biz.cs" "b/XUnitTest.XCode/Code/Entity/\351\203\250\351\227\250.Biz.cs" index 16c2f25f4..6096800b9 100644 --- "a/XUnitTest.XCode/Code/Entity/\351\203\250\351\227\250.Biz.cs" +++ "b/XUnitTest.XCode/Code/Entity/\351\203\250\351\227\250.Biz.cs" @@ -26,7 +26,7 @@ namespace XCode.Membership666; -public partial class Department : Entity +public partial class Department : Entity, ITenantScope { #region 对象操作 // 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存 diff --git a/XUnitTest.XCode/Code/EntityBuilderTests.cs b/XUnitTest.XCode/Code/EntityBuilderTests.cs index 0dab41b40..cee1322e3 100644 --- a/XUnitTest.XCode/Code/EntityBuilderTests.cs +++ b/XUnitTest.XCode/Code/EntityBuilderTests.cs @@ -11,6 +11,7 @@ namespace XUnitTest.XCode.Code; public class EntityBuilderTests { + private IList _tables; private IDataTable _table; private IDataTable _tableLog; private BuilderOption _option; @@ -18,9 +19,9 @@ public class EntityBuilderTests public EntityBuilderTests() { _option = new BuilderOption(); - var tables = ClassBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", _option, out _); - _table = tables.FirstOrDefault(e => e.Name == "User"); - _tableLog = tables.FirstOrDefault(e => e.Name == "Log"); + _tables = ClassBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", _option, out _); + _table = _tables.FirstOrDefault(e => e.Name == "User"); + _tableLog = _tables.FirstOrDefault(e => e.Name == "Log"); } private String ReadTarget(String file, String text) @@ -383,6 +384,45 @@ public void BuildLog() } } + [Fact] + public void BusinessTenantTable_ImplementsITenantScope() + { + var option = new EntityBuilderOption + { + ConnName = "Test", + Namespace = "Test", + Nullable = true, + }; + + var table = _tables.First(e => e.Name == "Role"); + var builder = new EntityBuilder + { + Table = table, + AllTables = _tables, + Option = option, + Business = true, + }; + + builder.Execute(); + var code = builder.ToString(); + + Assert.Contains("public partial class Role : Entity, ITenantScope", code); + Assert.Contains("Meta.Interceptors.Add();", code); + + table = _tables.First(e => e.Name == "Tenant"); + builder = new EntityBuilder + { + Table = table, + AllTables = _tables, + Option = option, + }; + + builder.Execute(); + code = builder.ToString(); + + Assert.DoesNotContain("public partial class Tenant : Entity, ITenantScope", code); + } + [Fact] public void FixModelFile() {