2011-01-17 2 views
2

작은 독립 응용 프로그램을 여러 개 만들어야하는데,이 응용 프로그램을 USB 장치에 복사하여 상자에서 바로 실행할 수 있습니다. 그래서 WPF를 사용하려고합니다. WPF는 먼저 EF 코드를 사용하여 SQL Server CE 데이터베이스에 연결합니다.SQL Server CE를 사용하는 아키텍처

내 질문은 내가 사용해야하는 아키텍처에 관한 것입니다. 앱이 독립형이지만, 도메인에서 데이터를 분리하여 레이어를 깨끗하게 분리하고 싶습니다. 그러나 나는 또한 너무 복잡하게 만들고 싶지 않습니다.

그래서 기본 도메인 계층 (도메인 논리가있는 도메인 객체)과 저장소 (EF 코드를 먼저 사용하는)를 사용하는 UI 레이어 (WPF/MVVM)를 갖고 싶습니다.

제 질문은이 경우 EF 작업을하기 위해 어떤 패턴을 사용해야합니까? 이러한 시나리오에서 CRUD 작업을 구현하는 방법을 보여주는 예제가 있습니까? 예를 들어 하나의 컨텍스트를 만들어서 열어 두어야합니까? 또는 작업 단위 패턴을 구현하고 필요한 경우 다른 컨텍스트에 오브젝트를 연결해야합니까?

아니면 전혀 다른 방식으로 사용 하시겠습니까?

조언 해 주셔서 감사합니다.

답변

2

EF 컨텍스트는 가능한 한 짧은 시간 동안 열어야합니다. 바람직하게는 using 문에서 사용하십시오.

private static void AttachRelatedObjects(
    ObjectContext currentContext, 
    SalesOrderHeader detachedOrder, 
    List<SalesOrderDetail> detachedItems) 
{ 
    // Attach the root detachedOrder object to the supplied context. 
    currentContext.Attach(detachedOrder); 

    // Attach each detachedItem to the context, and define each relationship 
    // by attaching the attached SalesOrderDetail object to the EntityCollection on 
    // the SalesOrderDetail navigation property of the now attached detachedOrder. 
    foreach (SalesOrderDetail item in detachedItems) 
    { 
     currentContext.Attach(item); 
     detachedOrder.SalesOrderDetails.Attach(item); 
    } 
} 

http://msdn.microsoft.com/en-us/library/bb896271.aspx

:
private static void ApplyItemUpdates(SalesOrderDetail originalItem, 
    SalesOrderDetail updatedItem) 
{ 
    using (AdventureWorksEntities context = 
     new AdventureWorksEntities()) 
    { 
     context.SalesOrderDetails.Attach(updatedItem); 
     // Check if the ID is 0, if it is the item is new. 
     // In this case we need to chage the state to Added. 
     if (updatedItem.SalesOrderDetailID == 0) 
     { 
      // Because the ID is generated by the database we do not need to 
      // set updatedItem.SalesOrderDetailID. 
      context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added); 
     } 
     else 
     { 
      // If the SalesOrderDetailID is not 0, then the item is not new 
      // and needs to be updated. Because we already added the 
      // updated object to the context we need to apply the original values. 
      // If we attached originalItem to the context 
      // we would need to apply the current values: 
      // context.ApplyCurrentValues("SalesOrderDetails", updatedItem); 
      // Applying current or original values, changes the state 
      // of the attached object to Modified. 
      context.ApplyOriginalValues("SalesOrderDetails", originalItem); 
     } 
     context.SaveChanges(); 
    } 
} 

는 컨텍스트에 엔티티를 attachs 부착라는 방법입니다
관련 문제