2011-03-08 2 views
0

다음은 P_BUDGET 클래스를 사용하여 내 ADO.NET 엔터티 데이터 모델에 뷰를 연결하는 데 사용되는 VM 클래스입니다. 이것은 잘 작동하고, 내 데이터를 얻으며, 모든 것을 멋지게 만듭니다. 나는 이것과 같은 구조를 기반으로하는 약 15-20 페이지를 가지고 있으며 코드는 EntityType (P_BUDGET, P_ACCOUNT, P_CIRCUIT 등)을 제외하고 거의 동일 할 것입니다. 이것을 추상화 할 방법이 있어야한다고 생각하지만, 노력했지만 비참하게 실패했습니다! 또한 하나의 뷰, 하나의 뷰 모델을 사용할 수 있어야하고 GV에 바인딩하는 엔티티를 스왑 할 수 있어야한다고 생각합니다 ... 전체 뷰 모델에 스며드는 유형을 가변화 할 수있는 방법을 찾지 못했습니다. . 당신의 도움을 주셔서 감사합니다DomainCollectionView (Entity Framework) 코드를 자체 서비스 (MVVM Silverlight4)로 변환

,

스콧

public class TestViewModel : ViewModelBase 
{ 
    private readonly ODADomainContext _context = new ODADomainContext(); 
    private readonly DomainCollectionView<P_BUDGET> _view; 
    private readonly DomainCollectionViewLoader<P_BUDGET> _loader; 
    private readonly EntityList<P_BUDGET> _source; 
    private bool _isGridEnabled; 
    /// <summary> 
    /// Initializes a new instance of the TestViewModel class. 
    /// </summary> 
    public TestViewModel() 
    { 
     this._source = new EntityList<P_BUDGET>(this._context.P_BUDGETs); 
     this._loader = new DomainCollectionViewLoader<P_BUDGET>(
      this.LoadSampleEntities, 
      this.OnLoadSampleEntitiesCompleted); 
     this._view = new DomainCollectionView<P_BUDGET>(this._loader, this._source); 

     INotifyCollectionChanged notifyingSortDescriptions = 
    (INotifyCollectionChanged)this.CollectionView.SortDescriptions; 
     notifyingSortDescriptions.CollectionChanged += 
      (sender, e) => this._view.MoveToFirstPage(); 

     using (this.CollectionView.DeferRefresh()) 
     { 
      this._view.PageSize = 10; 
      this._view.MoveToFirstPage(); 
     } 
    } 
    #region View Properties 

    public bool IsGridEnabled 
    { 
     get 
     { 
      return this._isGridEnabled; 
     } 

     private set 
     { 
      if (this._isGridEnabled != value) 
      { 
       this._isGridEnabled = value; 
       this.RaisePropertyChanged("IsGridEnabled"); 
      } 
     } 
    } 

    public ICollectionView CollectionView 
    { 
     get { return this._view; } 
    } 

    #endregion 

    private LoadOperation<P_BUDGET> LoadSampleEntities() 
    { 
     this.IsGridEnabled = false; 

     return this._context.Load(
      this._context.GetBudgetsQuery()); 
    } 

    private void OnLoadSampleEntitiesCompleted(LoadOperation<P_BUDGET> op) 
    { 
     this.IsGridEnabled = true; 

     if (op.HasError) 
     { 
      // TODO: handle errors 
      _view.PageSize = 0; 
      op.MarkErrorAsHandled(); 
     } 
     else if (!op.IsCanceled) 
     { 
      this._source.Source = op.Entities; 
      _view.PageSize = 10; 
      this._view.MoveToFirstPage(); 
      if (op.TotalEntityCount != -1) 
      { 
       this._view.SetTotalItemCount(op.TotalEntityCount); 
      } 
     } 
    } 
    ////public override void Cleanup() 
    ////{ 
    //// // Clean own resources if needed 

    //// base.Cleanup(); 
    ////} 
} 

답변

0

같은 것을보십시오. 이것은 테스트되지 않았으며 (분명히) 준수되지 않았습니다. 또한 EntityType (P_BUDGET, P_ACCOUNT, P_CIRCUIT 등)이 POCO가 아니라고 가정합니다.

public class TestViewModel<TEntity> : ViewModelBase 
{ 
    private readonly ODADomainContext _context = new ODADomainContext(); 
    private readonly DomainCollectionView<TEntity> _view; 
    private readonly DomainCollectionViewLoader<TEntity> _loader; 
    private readonly EntityList<TEntity> _source; 
    private bool _isGridEnabled; 
    /// <summary> 
    /// Initializes a new instance of the TestViewModel class. 
    /// </summary> 
    public TestViewModel() 
    { 
     this._source = new EntityList<TEntity>(this._context.GetEntitySet<TEntity>); 
     this._loader = new DomainCollectionViewLoader<TEntity>(
      this.LoadSampleEntities, 
      this.OnLoadSampleEntitiesCompleted); 
     this._view = new DomainCollectionView<TEntity>(this._loader, this._source); 

     INotifyCollectionChanged notifyingSortDescriptions = 
    (INotifyCollectionChanged)this.CollectionView.SortDescriptions; 
     notifyingSortDescriptions.CollectionChanged += 
      (sender, e) => this._view.MoveToFirstPage(); 

     using (this.CollectionView.DeferRefresh()) 
     { 
      this._view.PageSize = 10; 
      this._view.MoveToFirstPage(); 
     } 
    } 
    #region View Properties 

    public bool IsGridEnabled 
    { 
     get 
     { 
      return this._isGridEnabled; 
     } 

     private set 
     { 
      if (this._isGridEnabled != value) 
      { 
       this._isGridEnabled = value; 
       this.RaisePropertyChanged("IsGridEnabled"); 
      } 
     } 
    } 

    public ICollectionView CollectionView 
    { 
     get { return this._view; } 
    } 

    #endregion 

    private LoadOperation<TEntity> LoadSampleEntities() 
    { 
     this.IsGridEnabled = false; 

     return this._context.Load(
      this._context.GetBudgetsQuery()); 
    } 

    private void OnLoadSampleEntitiesCompleted(LoadOperation<TEntity> op) 
    { 
     this.IsGridEnabled = true; 

     if (op.HasError) 
     { 
      // TODO: handle errors 
      _view.PageSize = 0; 
      op.MarkErrorAsHandled(); 
     } 
     else if (!op.IsCanceled) 
     { 
      this._source.Source = op.Entities; 
      _view.PageSize = 10; 
      this._view.MoveToFirstPage(); 
      if (op.TotalEntityCount != -1) 
      { 
       this._view.SetTotalItemCount(op.TotalEntityCount); 
      } 
     } 
    } 
    ////public override void Cleanup() 
    ////{ 
    //// // Clean own resources if needed 

    //// base.Cleanup(); 
    ////} 
} 

// http://blog.zoolutions.se/post/2010/04/05/Generic-Repository-for-Entity-Framework-for-Pluralized-Entity-Set.aspx 

public static class ObjectContextExtensions 
{ 
    internal static EntitySetBase GetEntitySet<TEntity>(this ObjectContext context) 
    { 
     EntityContainer container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace); 
     Type baseType = GetBaseType(typeof(TEntity)); 
     EntitySetBase entitySet = container.BaseEntitySets 
      .Where(item => item.ElementType.Name.Equals(baseType.Name)) 
      .FirstOrDefault(); 

     return entitySet; 
    } 

    private static Type GetBaseType(Type type) 
    { 
     var baseType = type.BaseType; 
     if (baseType != null && baseType != typeof(EntityObject)) 
     { 
      return GetBaseType(type.BaseType); 
     } 
     return type; 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. 마지막으로 나온 블로그 게시물을 기반으로 한 ObjectContextExtensions 클래스에는 사용할 수없는 system.data 클래스가 3 개 필요합니다. silverlight는 system.data를 사용할 수 없으며 사용할 수 없습니다. 그것에 관한 어떤 생각? –