Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions XCode/Code/EntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest.XCode/Code/Entity/租户关系.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace XCode.Membership666;

public partial class TenantUser : Entity<TenantUser>
public partial class TenantUser : Entity<TenantUser>, ITenantScope
{
#region 对象操作
// 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest.XCode/Code/Entity/角色.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace XCode.Membership666;

public partial class Role : Entity<Role>
public partial class Role : Entity<Role>, ITenantScope
{
#region 对象操作
// 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest.XCode/Code/Entity/部门.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace XCode.Membership666;

public partial class Department : Entity<Department>
public partial class Department : Entity<Department>, ITenantScope
{
#region 对象操作
// 控制最大缓存数量,Find/FindAll查询方法在表行数小于该值时走实体缓存
Expand Down
46 changes: 43 additions & 3 deletions XUnitTest.XCode/Code/EntityBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ namespace XUnitTest.XCode.Code;

public class EntityBuilderTests
{
private IList<IDataTable> _tables;
private IDataTable _table;
private IDataTable _tableLog;
private BuilderOption _option;

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)
Expand Down Expand Up @@ -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<Role>, ITenantScope", code);
Assert.Contains("Meta.Interceptors.Add<TenantInterceptor>();", 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<Tenant>, ITenantScope", code);
}

[Fact]
public void FixModelFile()
{
Expand Down