mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 15:30:53 +08:00
Updated Repository (markdown)
@@ -113,6 +113,60 @@ repo.CompareState(item) 可获取 item 的状态变化信息
|
|||||||
Dictionary<string, object[]> CompareState(TEntity newdata);
|
Dictionary<string, object[]> CompareState(TEntity newdata);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Ioc + 登陆信息
|
||||||
|
|
||||||
|
repo.DbContextOptions.AuditValue 适合与 Ioc AddScoped 信息结合。
|
||||||
|
|
||||||
|
如下示例:使用仓储插入/更新时自动使用登陆信息
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
services.AddSingleton(fsql);
|
||||||
|
services.AddScoped(r =>
|
||||||
|
{
|
||||||
|
var user = r.GetService<User>();
|
||||||
|
var options = new RepositoryOptions();
|
||||||
|
options.AuditValue += (_, e) =>
|
||||||
|
{
|
||||||
|
if (user == null) return;
|
||||||
|
|
||||||
|
if (e.AuditValueType == AuditValueType.Insert &&
|
||||||
|
e.Object is IEntityCreated obj1 && obj1 != null)
|
||||||
|
{
|
||||||
|
obj1.CreatedUserId = user.Id;
|
||||||
|
obj1.CreatedUserName = user.Username;
|
||||||
|
}
|
||||||
|
if (e.AuditValueType == AuditValueType.Update &&
|
||||||
|
e.Object is IEntityModified obj2 && obj2 != null)
|
||||||
|
{
|
||||||
|
obj2.ModifiedUserId = user.Id;
|
||||||
|
obj2.ModifiedUserName = user.Username;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return options;
|
||||||
|
});
|
||||||
|
services.AddScoped(typeof(IBaseRepository<>), typeof(MyRepository<>));
|
||||||
|
services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
||||||
|
|
||||||
|
//以下实现 MyRepository
|
||||||
|
class MyRepository<TEntity, TKey> : BaseRepository<TEntity, TKey> where TEntity : class
|
||||||
|
{
|
||||||
|
public MyRepository(IFreeSql fsql, RepositoryOptions options) : base(fsql, null, null)
|
||||||
|
{
|
||||||
|
uowManager?.Binding(this);
|
||||||
|
if (options != null)
|
||||||
|
{
|
||||||
|
DbContextOptions.NoneParameter = options.NoneParameter;
|
||||||
|
DbContextOptions.EnableGlobalFilter = options.EnableGlobalFilter;
|
||||||
|
DbContextOptions.AuditValue += options.AuditValueHandler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class MyRepository<TEntity> : MyRepository<TEntity, long> where TEntity : class
|
||||||
|
{
|
||||||
|
public MyRepository(IFreeSql fsql, RepositoryOptions options) : base(fsql, options) { }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 过滤与验证
|
## 过滤与验证
|
||||||
|
|
||||||
假设我们有User(用户)、Topic(主题)两个实体,定义了两个仓储:
|
假设我们有User(用户)、Topic(主题)两个实体,定义了两个仓储:
|
||||||
|
|||||||
Reference in New Issue
Block a user