그것은 확인정 매핑되는 특성으로 저장소?
-
03-07-2019 - |
문제
어떤 잠재적인 문제에서 설정 매핑되는 재산으로 다음과 같다:
저장소
public Repository()
{
public DataContext dc {get;set;}
public GetOrders(int id)
{ ...from dc.Orders...}
}
서비스 층:
public GetNewOrders()
{
....
Repository rep=new Repository();
using {DataContext dc=new DataContext())
{
rep.dc=dc;
rep.GetOrders(id);
}
}
해결책
에 DDD,당신이 없는 더 큰 그림이 여기를 참조하여 concret 클래스입니다.당신은 인터페이스하지 사 저장소 및"서비스를 레이어에 의해"모범 사례를 설명합니다.해야 하는 경우가 매핑되으로 주입된 저장소에,나는 추천 refactoring to:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
private IDataContext _dataContext;
public Repository(IDataContext dataContext)
{
_dataContext = dataContext;
}
public IList<Orders> GetNewOrders()
{
// perform your actions on _dataContext here
}
}
더 나은 솔루션도록 핸들을 저장소에 매핑되는 자신의 유지의 분리에는 콘서트에 의해 유효한 마스킹 기본 요구 사항:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
private IDataContext _dataContext;
public Repository(String connectionString)
{
_dataContext = new DataContext(connectionString);
}
public IList<Orders> GetNewOrders()
{
// perform your actions on _dataContext here
}
}
해야 하는 경우에는 유의 제어를 매핑되는(또는 다른 클래스)자신은(아마도 당신을 유지하려는 정적 참조,또는 설정 변경에 따라 WebRequest,etc.),당신은 당신을 사용할 필요가"공장"입니다.
공는 다음과 같이 보일 것이다:
public static class DataContextFactory
{
public static IDataContext GetInstance()
{
// return either a static instance,
// or threaded instance, a GlobalContext instance
// or whatever your preference is here
//
}
}
그런 식으로,당신은 전체를 제어할 수있는 방법을 통해 인스턴스에 매핑되는 제어부는 멀리에서"서비스"층이다.그래서,당신은 당신을 사용하는 것이 DataContextFactory 같은 다음과 같다:
public interface IRepository
{
IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
public IList<Orders> GetNewOrders()
{
using (var dataContext = DataContextFactory.GetInstance())
{
// dataContext is now your IDataContext to work with
}
}
}
"에 액세스하는 방법 irepository 를?"요청할 수 있습니다?
귀하의 서비스를 계층을 할 것 다음과 같습니다.
public void GetNewOrdersForServices()
{
// Not recommended!
// IRepository repo = new Repository()
//
// The following is recommended instead; because, it removes the
// the Concret reference from your Services layer completely!
//
IRepository repo = ServiceLocator.InstanceOf<IRepository>();
IList myList = repo.GetNewOrders();
}
또는,당신은 주사하는 것으로 그것의 생성자를 사용하여 서비스의 좋아하는 맛을 Inversion of Control 컨테이너는 다음과 같이
public class OrderService
{
private IRepository _repo;
public OrderService(IRepository repo)
{
_repo = repo;
}
public void GetNewOrdersForServices()
{
IList myList = _repo.GetNewOrders();
}
하지 않은 경우 fimilar 으로 서비스 거주 개념을 확인,성 윈저 그것으로 캡슐화 단지에 대한 귀하의 모든 필요합니다.
다른 팁
내가 읽은 내용에서 DataContext를 사용하여 "둘 이상의 비즈니스 대화가 있습니다 일반적으로 잘못된 일입니다. "아래로 스크롤하십시오 이것이 중요한 이유는 무엇입니까? 견적 섹션. 캐싱 및 기타 요인으로 인해 데이터 컨텍스트가 즉시 부실한 것을 고려해야합니다. 그로부터, 당신은 당신이 모든 방법에 의해 재사용되는 속성으로 데이터 컨텍스트를 유지하고 싶지 않다고 말하는 것이 안전합니다. Eric Duncan의 제안을 사용하여 각 쿼리에 대한 새로운 컨텍스트를 얻기 위해 어떤 종류의 DataContext 공장을 전달하고 싶을 것입니다.
DataContext에 중점을 둔 토론의 경우 Apress Pro Linq 책에는 an이 있습니다 전체 장 DataContext에서 매우 마지막 페이지 그 중 "Datacontext 부티를 즉시 고려하는 것"도 조언합니다.