mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-03 23:10:54 +08:00
update
@@ -50,14 +50,23 @@ public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//示范全局过滤的仓储类注入,如果实体中不存在 Title 属性,则条件不生效
|
||||
builder.RegisterFreeRepositoryAddFilter<Song>(() => a => a.Title == DateTime.Now.ToString() + System.Threading.Thread.CurrentThread.ManagedThreadId);
|
||||
builder.RegisterFreeRepository(filter =>
|
||||
filter.Apply<Song>("test", a => a.Title == DateTime.Now.ToString() + Thread.CurrentThread.ManagedThreadId)
|
||||
);
|
||||
|
||||
builder.Populate(services);
|
||||
var container = builder.Build();
|
||||
|
||||
return new AutofacServiceProvider(container);
|
||||
}
|
||||
|
||||
public class xxxx {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
public class Song {
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
//在控制器使用
|
||||
public SongsController(GuidRepository<Song> repos1, GuidRepository<xxxx> repos2) {
|
||||
}
|
||||
@@ -65,6 +74,34 @@ public SongsController(GuidRepository<Song> repos1, GuidRepository<xxxx> repos2)
|
||||
|
||||
> Autofac 注入方式实现了全局【过滤与验证】的设定,方便租户功能的设计;
|
||||
|
||||
第一次请求:
|
||||
|
||||
repos1.Select.ToSql()
|
||||
> "SELECT a."Id", a."Title" \r\nFROM "Song" a \r\nWHERE (a."Title" = strftime('%Y-%m-%d %H:%M.%f',datetime(current_timestamp,'localtime')) || 21)"
|
||||
|
||||
repos2.Select.ToSql()
|
||||
>"SELECT a."Id" \r\nFROM "xxxx" a"
|
||||
|
||||
第二次请求:
|
||||
|
||||
repos1.Select.ToSql()
|
||||
>"SELECT a."Id", a."Title" \r\nFROM "Song" a \r\nWHERE (a."Title" = strftime('%Y-%m-%d %H:%M.%f',datetime(current_timestamp,'localtime')) || 4)"
|
||||
|
||||
repos2.Select.ToSql()
|
||||
>"SELECT a."Id" \r\nFROM "xxxx" a"
|
||||
|
||||
//禁用过滤器
|
||||
repos1.DataFilter.Disable("test")
|
||||
|
||||
repos1.Select.ToSql()
|
||||
> "SELECT a."Id", a."Title" \r\nFROM "Song" a"
|
||||
|
||||
1、注入的变量值在使用时有了动态变化,每次获取时都是新的(Thread.CurrentThread.ManagedThreadId);
|
||||
|
||||
2、设定的全局过滤,若某实体不存在表达式函数中的字段时,不会生效(如上xxxx不存在Title);
|
||||
|
||||
3、使用 DataFilter.Disable("test") 可临时关闭过滤器的效果,使用 DataFilter.Enable("test") 可重新开启;
|
||||
|
||||
## 过滤与验证
|
||||
|
||||
假设我们有User(用户)、Topic(主题)两个实体,在领域类中定义了两个仓储:
|
||||
|
||||
4
更新日志.md
4
更新日志.md
@@ -1,6 +1,10 @@
|
||||
|
||||
完整版本:年数-月-日-当日版本号,FreeSql 与 FreeSql.Repository 版本号相同。
|
||||
|
||||
## v0.3.13
|
||||
|
||||
- 修改 FreeSql.Repository Autofac 注入方式,真正的实现全局过滤功能;
|
||||
|
||||
## v0.3.12
|
||||
|
||||
- 增加 ICodeFirst.IsConfigEntityFromDbFirst,若无配置实体类主键、自增,可从数据库导入;
|
||||
|
||||
47
租户.md
47
租户.md
@@ -62,7 +62,7 @@ LEFT JOIN "TopicType_1" b ON a."TypeId" = b."Id"
|
||||
|
||||
### 实现全局控制租户
|
||||
|
||||
FreeSql.Repository Autofac 注入方式实现了全局【过滤与验证】、【分表分库】的设定,方便租户功能的设计;
|
||||
FreeSql.Repository Autofac 注入方式实现了全局【过滤与验证】的设定,方便租户功能的设计;
|
||||
|
||||
```csharp
|
||||
public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||
@@ -72,21 +72,58 @@ public IServiceProvider ConfigureServices(IServiceCollection services) {
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//这里示范全局过滤的仓储类注入,如果实体中不存在 Title 属性,则条件不生效
|
||||
builder.RegisterFreeRepositoryAddFilter<Song>(() => a => a.Title == DateTime.Now.ToString() + System.Threading.Thread.CurrentThread.ManagedThreadId);
|
||||
//示范全局过滤的仓储类注入,如果实体中不存在 Title 属性,则条件不生效
|
||||
builder.RegisterFreeRepository(filter =>
|
||||
filter.Apply<Song>("test", a => a.Title == DateTime.Now.ToString() + Thread.CurrentThread.ManagedThreadId)
|
||||
);
|
||||
|
||||
builder.Populate(services);
|
||||
var container = builder.Build();
|
||||
|
||||
return new AutofacServiceProvider(container);
|
||||
}
|
||||
|
||||
public class xxxx {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
public class Song {
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
}
|
||||
//在控制器使用
|
||||
public SongsController(GuidRepository<Song> repos1, GuidRepository<xxxx> repos2) {
|
||||
//repos1, repos2 都会附带注入时的 Filter 规则;
|
||||
}
|
||||
```
|
||||
|
||||
第一次请求:
|
||||
|
||||
repos1.Select.ToSql()
|
||||
> "SELECT a."Id", a."Title" \r\nFROM "Song" a \r\nWHERE (a."Title" = strftime('%Y-%m-%d %H:%M.%f',datetime(current_timestamp,'localtime')) || 21)"
|
||||
|
||||
repos2.Select.ToSql()
|
||||
>"SELECT a."Id" \r\nFROM "xxxx" a"
|
||||
|
||||
第二次请求:
|
||||
|
||||
repos1.Select.ToSql()
|
||||
>"SELECT a."Id", a."Title" \r\nFROM "Song" a \r\nWHERE (a."Title" = strftime('%Y-%m-%d %H:%M.%f',datetime(current_timestamp,'localtime')) || 4)"
|
||||
|
||||
repos2.Select.ToSql()
|
||||
>"SELECT a."Id" \r\nFROM "xxxx" a"
|
||||
|
||||
//禁用过滤器
|
||||
repos1.DataFilter.Disable("test")
|
||||
|
||||
repos1.Select.ToSql()
|
||||
> "SELECT a."Id", a."Title" \r\nFROM "Song" a"
|
||||
|
||||
1、注入的变量值在使用时有了动态变化,每次获取时都是新的(Thread.CurrentThread.ManagedThreadId);
|
||||
|
||||
2、设定的全局过滤,若某实体不存在表达式函数中的字段时,不会生效(如上xxxx不存在Title);
|
||||
|
||||
3、使用 DataFilter.Disable("test") 可临时关闭过滤器的效果,使用 DataFilter.Enable("test") 可重新开启;
|
||||
```
|
||||
|
||||
## 参考资料
|
||||
|
||||
- [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb)
|
||||
|
||||
Reference in New Issue
Block a user