When doing:
using (IUnitOfWork uow = UnitOfWork.Current)
{
LocationRepository rep = new LocationRepository();
Location loc = new Location()
{
CompanyId = m_User.UserCompany.CompanyId,
Name = locationName
};
rep.Insert(loc);
uow.Commit();
}
I get the exception:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
The reason I get this exception is that m_User.UserCompany is implemented like:
public _Company UserCompany
{
get
{
using (IUnitOfWork uow = UnitOfWork.Current)
{
CompanyRepository rep = new CompanyRepository();
m_Company = rep.Get(companyId.Value);
}
return m_Company;
}
}
So actually inside the first using statement I create another using statement which dispose my context when done and I get the exception in the outer using statement.
I am using the entity framework the way described here.
What is the common solution for such case?
I wonder why do you even bother with repositories if your
Userentity access unit of work directly? Your architecture looks very inconsistent. Create central place where unit of work is instanciated and disposed – in case of web application ideal place to call these methods is beginning and ending request. Access unit of work from repositories (or pass unit of work as parameter to their constructors) and handle all data retrieving and data persisting in repositories. Don’t make your entities dependent on unit of work – that makes your repositories redundant and you can fallback to active record pattern.This all leads to dependency injection with per HTTP request lifetime for unit of work and repositories.
Complains like “it is lot of work” or “I have a lot of places to change” are not excuse. You have a problem and you have to solve it. Solution requires big amount of refactoring because your initial solution was wrong – I sad to say you did it wrong and now you must invest your time to fix it.