首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVVM :如何使clean模型的视图模型集字段持久化到数据库中

MVVM :如何使clean模型的视图模型集字段持久化到数据库中
EN

Stack Overflow用户
提问于 2013-01-05 01:47:10
回答 2查看 342关注 0票数 1

在具有干净模型的MVVM应用程序中(没有实现像INotifyPropertyChabged这样的接口),视图模型包含绑定到视图的属性,这些属性从视图模型中包含的模型对象中获取它的值,并且当视图更改绑定到这些属性的控件之一时,应该设置它的属性的值。问题是当视图改变时;这些改变被绑定的视图模型属性捕获,但是属性不能设置模型对象字段,模型不变。我需要模型字段接受视图模型属性的设置,然后我可以将更新后的模型持久化到数据库中,同时考虑到它是一个干净的模型。

这里是视图模型代码的一部分

代码语言:javascript
复制
public class SubsystemDetailsViewModel: INotifyPropertyChanged, ISubsystemDetailsViewModel
    {
        #region Fields
        //Properties to which View is bound
        private int? _serial;
        public int? Serial
        {
            get { return Subsystem.Serial; }
            set
            {
                //Subsystem.Serial=value;
                _serial = value;
                OnPropertyChanged("Serial");
            }
        }

        private string _type;
        public string Type
        {
            get { return Subsystem.Type; }
            set
            {
                //Subsystem.Type = value;
                _type = value;
                OnPropertyChanged("Type");
            }
        }


       //remaining properties ....


        #endregion

        //Service
        private readonly ISubsystemService _subsystemService;



        //Reference to the View
        public ISubsystemDetailsView View { get; set; }

        //Event Aggregator Event
        private readonly IEventAggregator eventAggregator;

        //Commands
        public ICommand ShowTPGCommand { get; set; }
        public DelegateCommand UpdateCommand { get; set; }

       //
        private bool _isDirty;

        //Constructor ************************************************************************************************
        public SubsystemDetailsViewModel(ISubsystemDetailsView View, ISubsystemService subsystemService, IEventAggregator eventAggregator)
        {
            _subsystemService = subsystemService;

            this.View = View;
            View.VM = this;

            //EA-3
            if (eventAggregator == null) throw new ArgumentNullException("eventAggregator");
            this.eventAggregator = eventAggregator;
            //Commands
            this.ShowTPGCommand = new DelegateCommand<PreCommissioning.Model.Subsystem>(this.ShowTestPacks);
            this.UpdateCommand = new DelegateCommand(this.UpdateSubsystem, CanUpdateSubsystem);


        }


        //****************************************************************************************************************
        //ICommand-3 Event Handler 
        //this handler publish the Payload "SelectedSubsystem" for whoever subscribe to this event
        private void ShowTestPacks(PreCommissioning.Model.Subsystem subsystem)
        {
            eventAggregator.GetEvent<ShowTestPacksEvent>().Publish(SelSubsystem);
        }
        //===============================================================================================
        private void UpdateSubsystem()
        {

            _subsystemService.SaveChanges(Subsystem);
        }

        private bool CanUpdateSubsystem()
        {
            return _isDirty;
        }
        //*******************************************************************************************
        public void SetSelectedSubsystem(PreCommissioning.Model.Subsystem subsystem)
        {
            this.SelSubsystem = subsystem;

        }

        //************************************************************************************************************
        /// <summary>
        /// Active subsystem >> the ItemSource for the View
        /// </summary>
        private PreCommissioning.Model.Subsystem _subsystem;
        public PreCommissioning.Model.Subsystem Subsystem
        {
            get
            { 
                //return this._subsystem;
                GetSubsystem(SelSubsystem.SubsystemNo);
                return this._subsystem;

            }

            set
            {
                if (_subsystem != value)
                {
                    _subsystem = value;
                    OnPropertyChanged("Subsystem");
                }

            }

        }



        //Call the Service to get the Data form the Database
        private void GetSubsystem(string SSNo)
        {
            this._subsystem = _subsystemService.GetSubsystem(SSNo);


        }





        #region Implementation of INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            _isDirty = true;
            UpdateCommand.RaiseCanExecuteChanged();

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}

子系统是使用GetSubsystem()方法填充的模型对象。像Serial这样的视图模型属性从模型中获取它的值,如下所示。我尝试设置模型属性,如set part of the property中注释掉的行所示,但Subsystem对象没有发生任何更改,始终保持其原始值

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-05 05:43:04

如果GetSubsystem每次都返回一个新的子系统,那就是你的问题。在绑定到视图的属性的“set”中,您调用的是公共属性"Subsystem",而不是您创建的私有字段。因此,每次从视图中设置属性时,都会调用调用GetSubsystem(SelSubsystem.SubsystemNo);的Subsystem.get。

我认为,在您的ViewModel属性中,您希望将其更改为:

代码语言:javascript
复制
//Properties to which View is bound
public int? Serial
{
    get { return _subsystem.Serial; }
    set
    {
        _subsystem.Serial=value; // NOTE THE USE OF THE PRIVATE FIELD RATHER THAN THE PROPERTY
        OnPropertyChanged("Serial");
    }
}

public string Type
{
    get { return _subsystem.Type; }
    set
    {
        _subsystem.Type = value; // NOTE THE USE OF THE PRIVATE FIELD RATHER THAN THE PROPERTY
        OnPropertyChanged("Type");
}
票数 1
EN

Stack Overflow用户

发布于 2013-01-05 02:06:06

您需要在视图模型中有一个对模型的引用,视图模型会将值传递给模型。视图模型将实现INotifyPropertyChanged,并将成为视图的数据上下文。在视图模型中,像这样编写绑定属性:

代码语言:javascript
复制
private string yourProperty;
public string YourProperty
{
get { return yourProperty; }
set
{
if (value == yourProperty)
    return;
yourProperty= value;
YOUR_MODEL_REFERENCE.YourProperty= yourProperty;
this.RaisePropertyChanged(() => this.YourProperty);
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14162168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档