I have such a repository
public abstract class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class { protected DbContext _dbContext; public BaseRepository(DbContext dbContext) { _dbContext = dbContext; } public TEntity GetByKey(object keyValue) {
and a specific repository like this
public CustomerRepository : BaseRepository<Customer> , ICustomerRepository { public CustomerRepository(DbContext context) : base (context) { } public Customer FindCustomerByKey(string key) { _dbContext.Set<Customer>().Find(key); } }
I have a wcf service like this
[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() {
and I use StructureMap to input DbContext instances like
For<DbContext>().Use(() => new MyApplicationContext());
My problem is that the client calls the service, a new instance of CustomerSatisfactionService is created, so new instances of CustomerRepository and HelpdeskRepository , but with different DbContexts.
I want to implement a working template unit, but in the DoSomethingWithBothRepositories method, the two repositories have different DbContexts.
Is there a way to tell the structural map to deploy an instance of DbContext for each call?
source share