This might be a strange case but I want to sometimes reuse the same instance when getting exports with MEF and sometimes create a new.
Basicly I have a WCF service class the is instance per call. Each instance imports a RepositoryFactory which will also be new instance per service class. I return a Repository in the Factory and a repository gets a IDbContext injected.
I want each instance of the Factory to inject the same instance of IDbContext but have seperate instances between Factory instances.
1) Factory1 is created
2) Factory1 creates Repository1-1 that gets IDbContext1 injected
3) Factory1 creates Repository1-2 that gets IDbContext1 injected
4) Factory2 is created
5) Factory2 creates Repository2-1 that gets IDbContext2 injected
6) Factory2 creates Repository2-2 that gets IDbContext2 injected
This should ensures that Repositories created from the same factory share a Unit of Work.
But being new to MEF I'm not sure how I would go about doing that.
This is what I got:
public class RepositoryFactory
private readonly CompositionContainer _container;
private readonly IDbContext _context;
public IRepository<T> CreateRepository<T>() where T : class, IEntity
//Somehow add the _context instance into the Repository import
public class EntityRepository<T> : IRepository<T> where T : class, IEntity
// Perhaps a contract name might help!!
protected readonly IDbContext _context;