나는 다음과 같은 일반적인 저장소 있습니다폐기 컨텍스트
public class BaseBLL <TEntity>
where TEntity : class
{
protected MyEntities Context { get; set; }
public BaseBLL()
{
this.Context = DataAccessHelper.Context;
this.Context.MetadataWorkspace.LoadFromAssembly(typeof(TEntity).Assembly);
}
}
:이 비즈니스 계층 여기
public class UserBLL : BaseBLL<User>
{
EFRepository<User, MyEntities> userRepo = null;
public UserBLL() : base()
{
//Context is created in the consructor of the base class and passed to repository
userRepo = new EFRepository<User, MyEntities>(Context);
}
}
에서 클래스는 기본 비즈니스 클래스입니다
public class EFRepository<TEntity, TContext> : IRepository<TEntity, TContext>, IDisposable
where TEntity : class
where TContext : ObjectContext
{
protected TContext context;
public EFRepository(TContext context)
{
this.context = context;
}
//CRUD methods...
public void Dispose()
{
if (null != context)
{
context.Dispose();
}
}
}
을
이 디자인에서는 내부 클래스가 아닌 비즈니스 클래스 생성자에서 저장소의 인스턴스를 만들고 있기 때문에 using 절을 사용하면 저장소의 dispose 메소드가 기본적으로 호출되지 않습니다. 내 주요 질문은 컨텍스트/저장소가 삭제되었는지 확인하는 방법입니다.
나는 생성자가 아니라 각 메소드 내부에서 using 절로 저장소를 만들 수 있다는 것을 알고 있지만보다 우아한 방법이 있는지 궁금합니다.
일반적인 디자인에 대해서도 자유롭게 의견을 말하십시오.
찾는 http://stackoverflow.com/questions/6987908/what-is-the-best-way-to-instantiate-and-dispose-dbcontext-in -mvc/6990244 # 6990244) – Eranga