mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 07:20:51 +08:00
Updated Repository (markdown)
@@ -121,7 +121,9 @@ repo.DbContextOptions.AuditValue 适合与 Ioc AddScoped 信息结合。
|
||||
|
||||
```csharp
|
||||
services.AddSingleton(fsql);
|
||||
services.AddScoped<Action<DbContextAuditValueEventArgs>>(r => e => {
|
||||
services.AddScoped(r => new MyRepositoryOptions
|
||||
{
|
||||
AuditValue = e => {
|
||||
var user = r.GetService<User>();
|
||||
if (user == null) return;
|
||||
if (e.AuditValueType == AuditValueType.Insert &&
|
||||
@@ -134,6 +136,7 @@ services.AddScoped<Action<DbContextAuditValueEventArgs>>(r => e => {
|
||||
obj2.ModifiedUserId = user.Id;
|
||||
obj2.ModifiedUserName = user.Username;
|
||||
}
|
||||
}
|
||||
});
|
||||
services.AddScoped(typeof(IBaseRepository<>), typeof(MyRepository<>));
|
||||
services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
||||
@@ -141,15 +144,18 @@ services.AddScoped(typeof(IBaseRepository<,>), typeof(MyRepository<,>));
|
||||
//以下实现 MyRepository
|
||||
class MyRepository<TEntity, TKey> : BaseRepository<TEntity, TKey> where TEntity : class
|
||||
{
|
||||
public MyRepository(IFreeSql fsql, Action<DbContextAuditValueEventArgs> auditValue) : base(fsql, null, null)
|
||||
public MyRepository(IFreeSql fsql, MyRepositoryOptions options) : base(fsql, null, null)
|
||||
{
|
||||
uowManager?.Binding(this);
|
||||
if (auditValue != null) DbContextOptions.AuditValue += (_, e) => auditValue(e);
|
||||
if (options?.AuditValue != null) DbContextOptions.AuditValue += (_, e) => options.AuditValue(e);
|
||||
}
|
||||
}
|
||||
class MyRepository<TEntity> : MyRepository<TEntity, long> where TEntity : class
|
||||
{
|
||||
public MyRepository(IFreeSql fsql, Action<DbContextAuditValueEventArgs> auditValue) : base(fsql, auditValue) { }
|
||||
public MyRepository(IFreeSql fsql, MyRepositoryOptions options) : base(fsql, options) { }
|
||||
}
|
||||
class MyRepositoryOptions
|
||||
{
|
||||
public Action<DbContextAuditValueEventArgs> AuditValue { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user