UnitOfWork for different DbContext

105 views Asked by At

I have something like following

 //Following is the Constructor
    public UnitOfWork(IEmployeeContext context)
    {
        this._context = context;
    }

    #endregion

    public void Dispose()
    {
        this._context.Dispose();
    }

    public void Commit()
    {
        this._context.SaveChanges();
    }

    public IEmployeeRepository EmployeeRepository
    {
        {
        return _employeeRepository??
            (_employeeRepository= new EmployeeRepository(_context));
    }

    }

Now the Problem is i have another repository which is not based on IEmployeeContext. Lets call that context IOfficeContext so it will be like

     get
        {
            return _officeRepository??
                (_officeRepository= new OfficeRepository(_context));
        }

The Context passed to OfficeRepository is IOfficeContext . Should i have two separate UnitOfWork for them? or some other clever thing can be done over here?

1

There are 1 answers

7
Andrei On

You could have a common interface:

public interface IContext
{
    void Dispose();
    void SaveChanges();
}

And have both IEmployeeContext and IOfficeContext inherit from it. Then UnitOfWork could know only about IContext and will be able to handle both.