Entity Framework 4를 사용하고 있고 컨텍스트를 만들고 공용 속성을 통해 IContext 인터페이스로 노출하는 UnitOfWork 클래스를 만들었습니다. Context 클래스는 ObjectContext를 상속하며 내 Poco 엔터티를 공용 속성으로 노출합니다. 리포지토리 및 작업 단위 패턴에 ObjectContext를 올바르게 처리하는 방법
public IObjectSet<User> Users
{
get { return _users ?? (_users = CreateObjectSet<User>("Users")); }
}
private IObjectSet<User> _users;
는 또한 리포지토리 클래스에 쿼리를 실행할 때 컨텍스트 생성자 파라미터로서 그 컨텍스트를 받아 사용하는 몇 종류 저장소를 만들었다.
using(var uow = new UnitOfWork(connectionstring))
{
using(var repository = new UserRepository(uio.Context))
{
//This is the place where a connection is opened in the database
var user = repository.GetUserByName(username);
}
}
//The connection is still open here even though
의 UnitOfWork 클래스는는 IDisposable 인터페이스를 implementes하고 폐기 방법의 내부 Context.Dispose()를 호출 : 이것은 내가 함께 전체를 사용하는 방법입니다.
내 응용 프로그램을 닫으면 내 데이터베이스의 열린 연결이 끊어졌습니다. 그래서 제 질문은 : 여기 무슨 일이 일어나고 있습니까? :-) 내 UnitOfWork 클래스에 Context (ObjectContext) 인스턴스를 올바르게 처리해야 데이터베이스에서 열린 연결을 닫아야합니까?