As with most people I've been coming to grips with certain ins and outs of the Entity Framework. One of those things is the lifetime of the context. I'm using a repositories and have decided that a context will live for the length of the request. So the context would need to be injected from the web tier wherever that repository gets used.
I've written some code which I'm sure could be refactored (in fact, definitely!). So based on the concept above, how would you optimise the following repository helper?
public class RepositoryHelper
{
public static CountryRepository GetCountryRepository() {
return new CountryRepository(HttpContext.Current.GetObjectContext());
}
public static CurrencyRepository GetCurrencyRepository()
{
return new CurrencyRepository(HttpContext.Current.GetObjectContext());
}
public static SettingRepository GetSettingRepository()
{
return new SettingRepository(HttpContext.Current.GetObjectContext());
}
}
A repository is pretty simple and would look like
public class CountryRepository
{
private Context _context = null;
public CountryRepository(Context context)
{
_context = context;
}
public Country GetById(int id)
{
// Would return a country
}
public IEnumerable<Country> All()
{
// Would return a list of countries
}
}