【问题标题】:What's the best way to use an item-specific ViewModel with Realm?将特定于项目的 ViewModel 与 Realm 一起使用的最佳方式是什么?
【发布时间】:2017-09-17 18:49:28
【问题描述】:

我已将我的应用移植为使用 Realm 数据库而不是 SQLite。但是,我在将 IRealmCollection 与 MVVM 结合使用时遇到了问题。

让我们想象以下模型:

public class Item : RealmObject
{
    public string Id { get; }
}

我MainViewModel 上的当前方法是这样的:

public class MainViewModel
{
    public IRealmCollection<Item> { get; private set; }
}

我现在面临的问题是Item 具有特定的命令,位于ItemViewModel 中。我想在保留IRealmCollection 的功能的同时使用这些。

ItemViewModel:

public class ItemViewModel
{
    public Item Model { get; private set; }
    public ICommand MyCommand { get; private set; }
}

我一直在考虑几种方法,但我想知道是否有更简单的解决方案:

  • 创建一个订阅领域的自定义ObservableCollection
  • 使用 LINQ .Select() 扩展从列表中创建一个 ViewModel
  • 将ItemViewModel 保存在领域中,并将[Ignored] 属性添加到每个附加属性或命令
  • 将逻辑从ItemViewModel 移到一个单独的类(如ItemHelper)并从MainViewModel 调用它

解决这个问题的最佳方法是什么,可能是最类似于 MVVM 的方法?

【问题讨论】:

    标签: c# mvvm uwp realm


    【解决方案1】:

    如果您的命令和附加属性很简单并且可以嵌入到持久化的Item 中,我会采用该解决方案。这可能并不总是有效,例如,因为 RealmObjects 是在访问集合中的索引时动态创建的。例如:

    var collection = realm.All<Item>();
    var a = collection.ElementAt(0);
    var b = collection.ElementAt(0);
    

    将为您提供 a 和 b 的不同实例。这意味着您不能依赖 RealmObject 继承者中的非持久状态。如果这是一个破坏者,那么我会建议一个自定义 ObservableCollection 包装 IRealmCollection 并处理对您的 ViewModel 的投影,然后可以使用组合来公开 RealmObject:

    public interface IViewModel<T> where T : RealmObject
    {
        T Item { get; set; }
    }
    
    public class MyObservableCollection<TViewModel, TRealmObject> : IReadOnlyList<TViewModel>, INotifyCollectionChanged
        where TRealmObject : RealmObject
        where TViewModel : IViewModel<TRealmObject>, new()
    {
        private readonly IRealmCollection<TRealmObject> _collection;
    
        public TViewModel this[int index] => Project(_collection[index]);
    
        private event PropertyChangedEventHandler _propertyChanged;
        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add
            {
                UpdateCollectionChangedSubscriptionIfNecessary(isSubscribed: true);
                _collectionChanged += value;
            }
            remove
            {
                _collectionChanged -= value;
    
                UpdateCollectionChangedSubscriptionIfNecessary(isSubscribed: false);
            }
        }
    
        public MyObservableCollection(IRealmCollection<TRealmObject> collection)
        {
            _collection = collection;
        }
    
        private TViewModel Project(TRealmObject obj)
        {
            return new TViewModel
            {
                Item = obj
            };
        }
    
        private void UpdateCollectionChangedSubscriptionIfNecessary(bool isSubscribed)
        {
            if (_collectionChanged == null)
            {
                if (isSubscribed)
                {
                    // Subscribe via _collection
                }
                else
                {
                    // Unsubscribe via _collection
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多