我有这样实体框架的DbContext在每个呼叫实例模式WCF
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected DbContext _dbContext;
public BaseRepository(DbContext dbContext)
{
_dbContext = dbContext;
}
public TEntity GetByKey(object keyValue)
{
// todo
}
}
像这样
public CustomerRepository : BaseRepository<Customer> , ICustomerRepository
{
public CustomerRepository(DbContext context) : base (context) { }
public Customer FindCustomerByKey(string key)
{
_dbContext.Set<Customer>().Find(key);
}
}
具体仓库的仓库我有这样
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class CustomerSatisfactionService : ICustomerSatisfactionService
{
private ICustomerRepository _customerRepository;
private IHelpDeskRepository _helpdeskRepository;
public AccountService(ICustomerRepository customerRepository,IHelpdeskRepository helpdeskRepository)
{
_customerRepository = customerRepository;
_helpdeskRepository = helpdeskRepository;
}
public void DoSomethingUsingBothRepositories()
{
// start unit of work
// _customerRepository.DoSomething();
// _helpdeskRepository.DoSomething();
// commit unit of work
}
}
WCF服务
和我正在使用StructureMap注入这样的DbContext实例
For<DbContext>().Use(() => new MyApplicationContext());
我的问题是,当一个客户端调用服务,新CustomerSatisfactionService
实例被创建的CustomerRepository
因此新的实例和HelpdeskRepository
创建,但不同的DbContexts。
我想实现工作单元模式,但在DoSomethingWithBothRepositories
方法中,这两个存储库有不同的DbContexts。
有没有什么办法可以告诉结构图在每次调用的基础上创建一个DbContext实例?
喜拉吉斯拉夫......似乎这可能是一个解决方案...会尝试,让你知道...谢谢。 – 2011-02-15 05:01:40
我在这里找到了确切的答案http://andreasohlund.net/2009/04/27/unitofwork-in-wcf-using-structuremap/ – 2011-02-19 13:11:14
您的博客链接已损坏。 – rboarman 2012-08-13 23:11:27