【发布时间】:2016-02-05 00:20:11
【问题描述】:
我正在尝试为 Web API 背面的存储库的每个 Web 请求安全地注入数据库上下文。消费类调用存储库以检索对象,如果它返回 null,则它从不同的数据存储中获取对象并将其保存在数据库中以便以后更快地访问。这意味着它试图从数据库中获取,然后做一些事情,然后创建一条新记录。
目前我有这个存储库
public class OrganisationRepository : IOrganisationRepository
{
public Func<IOrganisationDomainDbContext> ContextFactory { get; set; }
public Organisation GetDetailByIdentifier(int id)
{
using (var context = ContextFactory.Invoke())
{
var org = context.Organisations.SingleOrDefault(x => x.Id == id);
return org;
}
}
public void Create(Organisation orgToCreate)
{
using (var context = ContextFactory.Invoke())
{
context.Organisations.Add(orgToCreate);
context.SaveChanges();
}
}
}
并且存储库被注入到具有短暂生活方式的消费类中。每个 Web 请求都会注入 DbContext。
以前,Repository 被注入了单例生活方式,这在 Create 操作上中断了。
我的问题是,我是否通过使存储库瞬态来进行廉价的黑客攻击?这会给我带来麻烦吗?如果是这样,我应该怎么做?
编辑:有关更多信息,使用中的 DI 容器是 Castle Windsor 编辑:DI 安装程序的相关部分
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IOrganisationDomainDbContext>().ImplementedBy<OrganisationDomainDbContext>().LifeStyle.PerWebRequest,
Component.For<Func<IOrganisationDomainDbContext>>().Instance(container.Resolve<IOrganisationDomainDbContext>),
Component.For<IOrganisationRepository>().ImplementedBy<OrganisationRepository>().LifeStyle.Transient,
Classes.FromThisAssembly().BasedOn<ApiController>().LifestylePerWebRequest());
}
更新: 临时存储库没有解决问题,这是我的错误 - 我只是忘记了我正在查看的记录实际上已提交到数据库,因此未调用创建操作。我的错,道歉。
【问题讨论】:
-
为什么在
Create是 Singleton 时,存储库会中断? -
嗨,马克,这是一个 InvalidOperationException,因为 DbContext 已经被释放
-
ContextFactory.Invoke()不是在每次调用时都创建一个新的DbContext吗? -
这就是我的想法,但它似乎无法正常工作。它到达“context.Organisations.Add(orgToCreate)”并抛出异常。
-
那个工厂是怎么实现的?
标签: c# entity-framework asp.net-web-api dependency-injection