mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 07:20:51 +08:00
Updated Repository (markdown)
@@ -121,28 +121,19 @@ repo.DbContextOptions.AuditValue 适合与 Ioc AddScoped 信息结合。
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
services.AddSingleton(fsql);
|
services.AddSingleton(fsql);
|
||||||
services.AddScoped(r =>
|
services.AddScoped<Action<DbContextAuditValueEventArgs>>(r => e => {
|
||||||
{
|
|
||||||
var user = r.GetService<User>();
|
var user = r.GetService<User>();
|
||||||
var options = new RepositoryOptions();
|
if (user == null) return;
|
||||||
options.AuditValue += (_, e) =>
|
if (e.AuditValueType == AuditValueType.Insert &&
|
||||||
{
|
e.Object is IEntityCreated obj1 && obj1 != null) {
|
||||||
if (user == null) return;
|
obj1.CreatedUserId = user.Id;
|
||||||
|
obj1.CreatedUserName = user.Username;
|
||||||
if (e.AuditValueType == AuditValueType.Insert &&
|
}
|
||||||
e.Object is IEntityCreated obj1 && obj1 != null)
|
if (e.AuditValueType == AuditValueType.Update &&
|
||||||
{
|
e.Object is IEntityModified obj2 && obj2 != null) {
|
||||||
obj1.CreatedUserId = user.Id;
|
obj2.ModifiedUserId = user.Id;
|
||||||
obj1.CreatedUserName = user.Username;
|
obj2.ModifiedUserName = 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<>));
|
||||||
services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
||||||
@@ -150,20 +141,15 @@ services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
|||||||
//以下实现 MyRepository
|
//以下实现 MyRepository
|
||||||
class MyRepository<TEntity, TKey> : BaseRepository<TEntity, TKey> where TEntity : class
|
class MyRepository<TEntity, TKey> : BaseRepository<TEntity, TKey> where TEntity : class
|
||||||
{
|
{
|
||||||
public MyRepository(IFreeSql fsql, RepositoryOptions options) : base(fsql, null, null)
|
public MyRepository(IFreeSql fsql, Action<DbContextAuditValueEventArgs> auditValue) : base(fsql, null, null)
|
||||||
{
|
{
|
||||||
uowManager?.Binding(this);
|
uowManager?.Binding(this);
|
||||||
if (options != null)
|
if (auditValue != null) DbContextOptions.AuditValue += (_, e) => auditValue(e);
|
||||||
{
|
|
||||||
DbContextOptions.NoneParameter = options.NoneParameter;
|
|
||||||
DbContextOptions.EnableGlobalFilter = options.EnableGlobalFilter;
|
|
||||||
DbContextOptions.AuditValue += options.AuditValueHandler;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
class MyRepository<TEntity> : MyRepository<TEntity, long> where TEntity : class
|
class MyRepository<TEntity> : MyRepository<TEntity, long> where TEntity : class
|
||||||
{
|
{
|
||||||
public MyRepository(IFreeSql fsql, RepositoryOptions options) : base(fsql, options) { }
|
public MyRepository(IFreeSql fsql, Action<DbContextAuditValueEventArgs> auditValue) : base(fsql, auditValue) { }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user