Important Clarify

Jul 12, 2011 at 7:10 AM

Ok maybe it's a simple answer but i think it's important to understand this. I'm trying to migrate some part of a sample application from light toolkit to simple because I think it's a real and deep sample but i can't understand some difference between this and the ones with sample. The use of "INotifyPropertyChanged" In the interface of service agent i don't see any use but  i dunno exactly why. Take this for example

public interface IAuthenticationModel : INotifyPropertyChanged
    {
        void LoadUserAsync();
        event EventHandler<LoadUserOperationEventArgs> LoadUserComplete;
        void LoginAsync(LoginParameters loginParameters);
        event EventHandler<LoginOperationEventArgs> LoginComplete;
        void LogoutAsync();
        event EventHandler<LogoutOperationEventArgs> LogoutComplete;

        IPrincipal User { get; }
        Boolean IsBusy { get; }
        Boolean IsLoadingUser { get; }
        Boolean IsLoggingIn { get; }
        Boolean IsLoggingOut { get; }
        Boolean IsSavingUser { get; }

        event EventHandler<AuthenticationEventArgs> AuthenticationChanged;

Don't think to the somethingEventArg there are implemantations we could not care. i think i would translate in a interface service agent as

public interface IAuthenticationServiceAgent
    {
        void LoadUserAsync(Action<Exception> completed);
        void LoginAsync(LoginParameters loginParameters, Action<Exception> completed);
        void LogoutAsync(Action<Exception> completed);
        

        IPrincipal User { get; }
        Boolean IsBusy { get; }
        Boolean IsLoadingUser { get; }
        Boolean IsLoggingIn { get; }
        Boolean IsLoggingOut { get; }
        Boolean IsSavingUser { get; }

        event EventHandler<AuthenticationEventArgs> AuthenticationChanged;
    }
Is all right? And why in samples there is no use of "INotifyPropertyChange? This is just one class but every class (from model to view) sample in lighttoolkit is like this. Could you help me with an explanation? Sorry for english and thanks.

Jul 12, 2011 at 4:27 PM

@Emanuele: Glad you asked the question about INotifyPropertyChanged.  The purpose of this interface, which has only one member (a PropertyChanged event), is to support two-way data binding.  Without it, updates you make to properties on an entity are not reflected in the UI.  The reason is that the user interface has no way of knowing that a property on the underlying object has changed. However, if the object exposes a PropertyChanged event, the binding can subscribe to it and respond by calling the property getter on the object to retrieve the latest data and display it to the user.

There are two kinds of objects that a View can bind to: a Model or a ViewModel.  Some toolkits do not advocate binding directly to Models and only bind to ViewModels.  However, my experience is that this purist approach can result in a lot of redundant code with ViewModels whose sole purpose is to wrap an underlying Model, without necessarily adding anything of value.  Simple MVVM Toolkit addresses this with the ViewModelDetailBase class that has a Model property of type TModel.  In this scenario, the binding in the View specifies a path that begins with Model, for example: <TextBlock Text="{Binding Path=Model.CustomerName}" />.  If there are any properties that the CustomerViewModel wishes to add, the View can bind directly to those. For this reason, both the CustomerModel and CustomerViewModel classes would need to implement INotifyPropertyChanged. The good news, however, is that they don't have to, because it is implemented by the Model and ViewModel base classes in the toolkit.

Because the service agent does not interact directly with the View, there is no reason for this class to implement INotifyPropertyChanged.

I hope this helps clarify things!

Cheers,

Tony

Jul 13, 2011 at 7:23 AM
Edited Jul 13, 2011 at 7:51 AM

@Tony thanks for answer. The example i wrote before was an interface for the model in client side of authentication service (i think it was clear :) ). So If i understand what you mean, i could rewrite it using toolkit's model base class and so on.

Jul 13, 2011 at 2:07 PM
Yes, just derive AuthenticationModel from ModelBase, then use the mvvmprop code snippy to create properties that call NotifyPropertyChange in the setter. If you are doing validation, you can also call ValidateProperty in the setter as well.

To see a sample of using the service agent, I suggest you look at the Main sample, parts 1, 2 & 3, and also look at the Real World screen cast I did (linked to from the CodePlex project home page).

Cheers,
Tony
Sent from my iPhone

On Jul 13, 2011, at 1:23 AM, "emanuelec70"<notifications@codeplex.com> wrote:

From: emanuelec70

@Tony thanks for answer. The example i wrote before was an interface for the model in client side of authentication service (i think it was clear :) ). So If i understand what you mean, i could rewrite it using toolkit's model base class and so on. Is there any complete examble of how to use these classes, because i see that you use most service agent class.