我在DbContext中保存实体时会动态注入家政服务。
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
var t = entityEntry.Entity.GetType();
// Housekeep
var castEntityEntry = castDbEntityEntryMethod
.MakeGenericMethod(new[] { t })
.Invoke(entityEntry, null);
housekeepEntityMethod
.MakeGenericMethod(new[] { t })
.Invoke(this, new[] { castEntityEntry });
return base.ValidateEntity(entityEntry, items);
}
private void HousekeepEntity<TEntity>(DbEntityEntry<TEntity> entity)
where TEntity : class
{
var housekeeper = DbConfiguration.DependencyResolver.GetService<IHousekeeper<TEntity>>();
if (housekeeper != null)
{
housekeeper.Maintain(entity);
}
}
我一直在IComponentContext
周围传递,所以我可以在那里解析类型,但是使用EF6我希望使用依赖注入:
private class AutofacDbDependencyResolver : AutofacWebApiDependencyResolver, IDbDependencyResolver
{
public AutofacDbDependencyResolver(ILifetimeScope container)
: base(container)
{
}
public object GetService(System.Type type, object key)
{
using (var scope = this.GetRequestLifetimeScope())
{
if (scope == null)
{
return null;
}
object service;
scope.TryResolve(type, out service);
return service;
}
}
public System.Collections.Generic.IEnumerable<object> GetServices(System.Type type, object key)
{
using (var scope = this.GetRequestLifetimeScope())
{
if (scope == null)
{
return System.Linq.Enumerable.Empty<object>();
}
object service;
if (scope.TryResolve(type, out service))
{
return new object[] { service };
}
else
{
return System.Linq.Enumerable.Empty<object>();
}
}
}
}
设置容器时:
System.Data.Entity.DbConfiguration.Loaded += (s, e) =>
e.AddDependencyResolver(new AutofacDbDependencyResolver(container), overrideConfigFile: false);
但是我的生命周期范围返回null。如何获取调用此操作的当前API请求?
答案 0 :(得分:2)
阻止在DbContext中使用service locator anti-pattern。 EF6现在允许你解决它并不重要,它仍然是一种反模式。相反,只需使用依赖注入或允许通过挂钩事件来添加此内务处理。例如,您可以为实体编写部分类:
public partial class MyEntities
{
public event Action<DbContext> SavingChanges = _ => { };
public override int SaveChanges()
{
this.SavingChanges(this);
return base.SaveChanges();
}
}
通过这种方式,您可以简单地抓住当有人拨打SavingChanges
时会引发的SaveChanges
事件:
builder.Register<MyEntities>(context =>
{
var db = new MyEntities();
db.SavingChanges += context.Resolve<IHouseKeepingService>().HousekeepEntities;
return db;
}).InstancePerLifetimeScope();