I have a generic repository that i used to inject in a Job
class on the start of the application. This job class intend to run forever in a loop, so the context that repository uses is created just once. The problem is after some days the application was consuming impressives 1gb of memory (in the beggining of the execution it consumes 40mb).
After that, I implemented the Dispose
of the DBContext
class in the Repository
and start to instantiate it in every loop. The problem is solved, but i lost the DIP principle. Here´s the new code:
public class Job
{
do{
//some code
using (var repository = new Repository<User>())
{
repository.Save(User);
}
}while(true);
}
The repository:
public void Save(T entity)
{
DbSet.Add(entity);
_context.SaveChanges();
}
private IDbSet<TEntity> DbSet
{
get { return _context.CreateSet<TEntity>(); }
}
Is there some workaround or tip to make the context not consume this memory, instantiating it just once? Or must I need to lost the IoC pattern in that case?
Thanks.