Click here to Skip to main content
14,774,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using WPF with Entity Framework 6 (DB first), Caliburn.Micro, and MEF.

I am trying to implement IoC in my project. I am an absolute beginner in IoC, and unfortunately, I cant find too many examples where MEF is used with Repository Pattern.

So I have a few repositories, and I have created my Generic Unit of Work like this:

class GenericUoW : IUoW, IDisposable
{
    protected readonly DbContext _ctx;

    public GenericUoW(DbContext context)
    {
        _ctx = context;
    }

    public void Complete()
    {
        _ctx.SaveChanges();
    }

    public void Dispose()
    {
        _ctx.Dispose();
    }
}


My actual Unit of Work classes are implemented like this:

class InvoiceUoW : GenericUoW, IInvoiceUoW
{
    public InvoiceUoW(DbContext _ctx) : base(_ctx)
    {
        salesrepo = new SaleRepository(_ctx);
        itemsrepo = new ItemRepository(_ctx);
        materialrepo = new MaterialRepository(_ctx);
        raterepo = new RateRepository(_ctx);
        clientrepo = new ClientRepository(_ctx);
        taxrepo = new TaxRepository(_ctx);
        stockhistoryrepo = new StockHistoryRepository(_ctx);
        proformarepo = new ProformaRepository(_ctx);
    }

    public ISaleRepository salesrepo { get; private set; }
    public IItemRepository itemsrepo { get; private set; }
    public IMaterialRepository materialrepo { get; private set; }
    public IRateRepository raterepo { get; private set; }
    public IClientRepository clientrepo { get; private set; }
    public ITaxRepository taxrepo { get; private set; }
    public IStockHistoryRepository stockhistoryrepo { get; private set; }
    public IProformaRepository proformarepo { get; private set; }
}


Now, in my ViewModel, I am doing something like this:

[Export(typeof(InvoiceViewModel))]    
class InvoiceViewModel : PropertyChangedBase
{

    #region ctor
    [ImportingConstructor]
    public InvoiceViewModel(IInvoiceUoW invoiceUoW)
    {
        _uow = invoiceUoW;
        Clients = new BindableCollection<Client>(_uow.clientrepo.FetchAll());
    }

    #endregion

    private readonly IInvoiceUoW _uow;

    //other properties and methods
}


My question is related to the use of IoC/DI in InvoiceUoW. How do I implement constructor injection in that class? Because each of those repositories in that class has to be instantiated with the same DataContext. Please do note that I have several Unit of Work classes, each for one viewmodel.

What I have tried:

When I try to do this with MEF, by making the constructor of InvoiceUoW something like this

public InvoiceUoW(DbContext _ctx, ISaleRepository salerepo, IItemRepository itemsrepo,IMaterialRepository materialrepo, ... ) : base(_ctx)


it does something equivalent to this:

salesrepo = new SaleRepository(new DbContext()); 
itemsrepo = new ItemRepository(new DbContext()); 
materialrepo = new MaterialRepository(new DbContext());
...


How can I instantiate all repositories using the same DbContext? I have thought of using a factory, but that will unnecessarily complicate the code.
Posted
Comments
Tomas Takac 29-Mar-17 2:46am
   
Why do you need the repositories to be injected? What is the benefit? Maybe you are taking this too far. Just a thought.
Sabyasachi Mukherjee 2-May-17 8:09am
   
So, I should just inject the DbContext?
Tomas Takac 2-May-17 9:10am
   
I'm saying your code is just fine as it is now, do not complicate it unnecessarily.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900