Like most people, I came across certain problems and features of the Entity Framework. One such thing is the lifetime of the context. I use repositories and decided that the context would match the length of the request. Thus, the context will need to be entered from the web tier wherever this repository will be used.
I wrote code that I am sure can be reorganized (in fact, definitely!). So, based on the concept above, how would you optimize the next 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());
}
}
The repository is quite simple and will look like
public class CountryRepository
{
private Context _context = null;
public CountryRepository(Context context)
{
_context = context;
}
public Country GetById(int id)
{
}
public IEnumerable<Country> All()
{
}
}
source
share