簡體   English   中英

將DomainCollectionView(實體框架)代碼抽象成自己的服務(MVVM Silverlight4)

[英]Abstract DomainCollectionView (Entity Framework) code into its own service (MVVM Silverlight4)

我下面有一個VM類,該類用於使用P_BUDGET類將視圖連接到我的ADO.NET實體數據模型。 這樣可以很好地工作,獲取我的數據,使一切變得漂亮。 我大約有15-20頁都將基於相同的結構,並且除了EntityType(P_BUDGET,P_ACCOUNT,P_CIRCUIT等)之外,代碼幾乎是相同的。 我覺得應該有一種抽象的方法,但是我嘗試了,但失敗了! 我還覺得我應該能夠使用一個視圖,一個視圖模型,並且只換出綁定到GV的實體...我只是無法找到一種使類型可變的方法,這種方法會滲透到整個視圖模型中。

感謝您的幫助,

史考特

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();
    ////}
}

嘗試這樣的事情。 這是未經測試的(顯然),甚至沒有得到遵守。 這也假設EntityTypes(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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM