locked
MVVM save object state RRS feed

  • Question

  • I have been reading up on MVVM and I have seen plenty of examples.  However, what I am seem to be missing is how and/or where to put the function that saves and/or modifies the object externally from the application.  
    Friday, August 28, 2015 6:39 PM

Answers

  • The simple answer is "the business layer", i.e. the Model, since it has nothing to do with UI.

    But managing the layers isn't always easy and obvious.  If you want share more about your application structure we might be able to provide more guidance.

    • Marked as answer by Weiwei Cai Friday, September 4, 2015 1:24 AM
    Friday, August 28, 2015 8:23 PM
  • The model is however your data is surfaced in the Silverlight layer.

    With Silverlight there is usually also going to be a component on the server because Silverlight runs in a security sandbox.

    Technically, that isn't really part of mvvm since the pattern is a UI pattern and what's happening on a server is neither here nor there.

    Most people would be using wcf ria - with entity framework.

    In Silverlight terms the user clicks a button whose command is bound to an icommand exposed from a viewmodel.

    That will then call savechanges on the objectcontext, here's a piece of code:

    public void UpdateClickThrough(ClickThrough clickThrough)
    {
         this.ObjectContext.xxxxCountClick(clickThrough.UserId, clickThrough.AppId);
         this.ObjectContext.SaveChanges();
    }

    That then propagates to the server and is converted into some sql by entity framework. But my point here is that beyond that savechanges is outside the scope of mvvm.

    .

    Please only ask one question per thread.

    The title, in this case  "MVVM save object state" is the question, the body of the post explains more about it. If you ask a question which isn't that then the usefulness of the forum as a searchable resource is reduced.  Questions / answers aren't just for the original poster, they're for everyone forever.

    .

    Your second question is kind of interesting because how would you know some data has changed? Either you just changed it, in which case change notification is often a bit pointless.

    or

    Someone else changed the data. Any model of data will be disconnected from the database. That means you need some sort of a mechanism to track changes on the server, then something like signalr to tell the clients that some data changed. 

    Once you then work out which record changed and which field is different, set that field...

    Then if you have inotifypropertychanged implemented and raise property changed for that property then the view will respond and re-read the new value. Presenting it in whatever property it is bound to.

    Some service references automagically implement inpc, by the way. If I grab a bit of code out an app, this service gets lists of files which are on the server:

    // 
    // This code was auto-generated by Microsoft.Silverlight.ServiceReference, version 5.0.61118.0
    // 
    namespace ITSupport.FileListServiceReference {
        using System.Runtime.Serialization;
        
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="FileList", Namespace="http://schemas.datacontract.org/2004/07/ITSupport.Web")]
        public partial class FileList : object, System.ComponentModel.INotifyPropertyChanged {
            
            private string FolderField;
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string Folder {
                get {
                    return this.FolderField;
                }
                set {
                    if ((object.ReferenceEquals(this.FolderField, value) != true)) {
                        this.FolderField = value;
                        this.RaisePropertyChanged("Folder");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }



    • Edited by Andy ONeill Saturday, August 29, 2015 7:58 AM
    • Marked as answer by Weiwei Cai Friday, September 4, 2015 1:23 AM
    Saturday, August 29, 2015 7:57 AM

All replies

  • The simple answer is "the business layer", i.e. the Model, since it has nothing to do with UI.

    But managing the layers isn't always easy and obvious.  If you want share more about your application structure we might be able to provide more guidance.

    • Marked as answer by Weiwei Cai Friday, September 4, 2015 1:24 AM
    Friday, August 28, 2015 8:23 PM
  • TheMethodology, 

    Thank you for the advice.  This is for any specific project, just trying to sharpen my skills.  But I do have one more, lets say, curiosity about the subject.  I figured managing modification and additions of an object from the model, but how is the best way to call it.  Could the Notify Change Property interface be utilized?  For example if I have a property called Name, could I hook into the event handler to fire an update method?

    Friday, August 28, 2015 9:30 PM
  • The model is however your data is surfaced in the Silverlight layer.

    With Silverlight there is usually also going to be a component on the server because Silverlight runs in a security sandbox.

    Technically, that isn't really part of mvvm since the pattern is a UI pattern and what's happening on a server is neither here nor there.

    Most people would be using wcf ria - with entity framework.

    In Silverlight terms the user clicks a button whose command is bound to an icommand exposed from a viewmodel.

    That will then call savechanges on the objectcontext, here's a piece of code:

    public void UpdateClickThrough(ClickThrough clickThrough)
    {
         this.ObjectContext.xxxxCountClick(clickThrough.UserId, clickThrough.AppId);
         this.ObjectContext.SaveChanges();
    }

    That then propagates to the server and is converted into some sql by entity framework. But my point here is that beyond that savechanges is outside the scope of mvvm.

    .

    Please only ask one question per thread.

    The title, in this case  "MVVM save object state" is the question, the body of the post explains more about it. If you ask a question which isn't that then the usefulness of the forum as a searchable resource is reduced.  Questions / answers aren't just for the original poster, they're for everyone forever.

    .

    Your second question is kind of interesting because how would you know some data has changed? Either you just changed it, in which case change notification is often a bit pointless.

    or

    Someone else changed the data. Any model of data will be disconnected from the database. That means you need some sort of a mechanism to track changes on the server, then something like signalr to tell the clients that some data changed. 

    Once you then work out which record changed and which field is different, set that field...

    Then if you have inotifypropertychanged implemented and raise property changed for that property then the view will respond and re-read the new value. Presenting it in whatever property it is bound to.

    Some service references automagically implement inpc, by the way. If I grab a bit of code out an app, this service gets lists of files which are on the server:

    // 
    // This code was auto-generated by Microsoft.Silverlight.ServiceReference, version 5.0.61118.0
    // 
    namespace ITSupport.FileListServiceReference {
        using System.Runtime.Serialization;
        
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="FileList", Namespace="http://schemas.datacontract.org/2004/07/ITSupport.Web")]
        public partial class FileList : object, System.ComponentModel.INotifyPropertyChanged {
            
            private string FolderField;
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string Folder {
                get {
                    return this.FolderField;
                }
                set {
                    if ((object.ReferenceEquals(this.FolderField, value) != true)) {
                        this.FolderField = value;
                        this.RaisePropertyChanged("Folder");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }



    • Edited by Andy ONeill Saturday, August 29, 2015 7:58 AM
    • Marked as answer by Weiwei Cai Friday, September 4, 2015 1:23 AM
    Saturday, August 29, 2015 7:57 AM