Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Sample Silverlight 4 Application Using MEF, MVVM, and WCF RIA Services - Part 1

, 7 Jul 2011 CPOL
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
IssueVisionforSilverlightSetup20110630.zip
IssueVision for Silverlight Setup
IssueVisionSetup.msi
setup.exe
IssueVision_for_Silverlight_setup.zip
IssueVisionSetup.msi
setup.exe
IssueVision_Silverlight20110630.zip
IssueVision.Silverlight
Assemblies
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
Database
IssueVision.Client
IssueVision.Client.csproj.user
Assets
Signin.png
Controls
Properties
Views
IssueVision.Common
IssueVision.Common.csproj.user
Controls
Helpers
Models
Properties
Resources
Themes
IssueVision.Data
IssueVision.Data.csproj.user
Properties
Web
Resources
IssueVision.Data.Web
IssueVision.edmx
Helpers
MetadataClasses
Properties
Resources
Services
IssueVision.Model
IssueVision.Model.csproj.user
Properties
IssueVision.Setup
IssueVisionSetup.vdproj
IssueVision.ViewModel
IssueVision.ViewModel.csproj.user
Properties
IssueVision.Web
IssueVision.Web.csproj.user
Properties
IssueVision_Silverlight_20110316.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
DataSources
IssueVision.Data.Web.IssueVisionContext.datasource
IssueVision.Data.Web.PasswordResetContext.datasource
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
DataTypes
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_20110606.zip
GalaSoft.MvvmLight.Extras.SL4.dll
GalaSoft.MvvmLight.SL4.dll
MenuControls.dll
System.Windows.Controls.DataVisualization.Toolkit.dll
System.Windows.Controls.Toolkit.dll
System.Windows.Controls.Toolkit.Internals.dll
System.Windows.Interactivity.dll
IssueVision.Client.csproj.user
Signin.png
IssueVision.Common.csproj.user
IssueVision.Data.csproj.user
IssueVision.edmx
IssueVision.Model.csproj.user
IssueVisionSetup.vdproj
IssueVision.ViewModel.csproj.user
IssueVision.Web.csproj.user
IssueVision_Silverlight_Setup.zip
IssueVisionSetup.msi
setup.exe
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using IssueVision.Data.Web;
using IssueVision.Common;

namespace IssueVision.ViewModel
{
    [Export(ViewModelTypes.LoginFormViewModel, typeof(ViewModelBase))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LoginFormViewModel : ViewModelBase
    {
        #region "Private Data Members"
        private IAuthenticationModel _authenticationModel;
        private IPasswordResetModel _passwordResetModel;
        #endregion "Private Data Members"

        #region "Constructor"
        [ImportingConstructor]
        public LoginFormViewModel(IAuthenticationModel authModel, IPasswordResetModel passModel)
        {
            _authenticationModel = authModel;
            _passwordResetModel = passModel;

            // set up event handling
            _authenticationModel.LoginComplete += new EventHandler<LoginOperationEventArgs>(_authenticationModel_LoginComplete);

            _passwordResetModel.GetUserComplete += new EventHandler<EntityResultsArgs<PasswordResetUser>>(_passwordResetModel_GetUserComplete);
            _passwordResetModel.SaveUserComplete += new EventHandler<ResultsArgs>(_passwordResetModel_SaveUserComplete);

            // Show user login screen first
            this.IsFlipped = false;

            // Clear any previous error message
            this.LoginScreenErrorMessage = null;
            this.ResetPasswordScreenErrorMessage = null;

            // Clear the user name and password
            this.CurrentUser = new LoginUser();
        }
        #endregion "Constructor"

        #region "Public Properties"

        private bool _isFlipped;

        public bool IsFlipped {
            get { return _isFlipped; }
            private set
            {
                if (value != _isFlipped)
                {
                    _isFlipped = value;
                    this.RaisePropertyChanged("IsFlipped");
                }
            }
        }

        private LoginUser _currentUser;

        public LoginUser CurrentUser
        {
            get { return _currentUser; }
            private set
            {
                if (value != _currentUser)
                {
                    _currentUser = value;
                    this.RaisePropertyChanged("CurrentUser");
                }
            }
        }

        private PasswordResetUser _currentPasswordResetUser;

        public PasswordResetUser CurrentPasswordResetUser
        {
            get { return _currentPasswordResetUser; }
            private set
            {
                if (value != _currentPasswordResetUser)
                {
                    _currentPasswordResetUser = value;
                    this.RaisePropertyChanged("CurrentPasswordResetUser");
                }
            }
        }

        private string _loginScreenErrorMessage;

        public string LoginScreenErrorMessage
        {
            get { return _loginScreenErrorMessage; }
            private set
            {
                if (value != _loginScreenErrorMessage)
                {
                    _loginScreenErrorMessage = value;
                    RaisePropertyChanged("LoginScreenErrorMessage");
                }
            }
        }

        private string _resetPasswordScreenErrorMessage;

        public string ResetPasswordScreenErrorMessage
        {
            get { return _resetPasswordScreenErrorMessage; }
            private set
            {
                if (value != _resetPasswordScreenErrorMessage)
                {
                    _resetPasswordScreenErrorMessage = value;
                    RaisePropertyChanged("ResetPasswordScreenErrorMessage");
                }
            }
        }

        #endregion "Public Properties"

        #region "Public Commands"

        private RelayCommand<LoginUser> _loginCommand = null;

        public RelayCommand<LoginUser> LoginCommand
        {
            get
            {
                if (_loginCommand == null)
                {
                    _loginCommand = new RelayCommand<LoginUser>(
                        g => this.OnLoginCommand(g),
                        g => g != null);
                }
                return _loginCommand;
            }
        }

        private void OnLoginCommand(LoginUser g)
        {
            if (!this._authenticationModel.IsLoggingIn)
            {
                // clear any previous error message
                this.LoginScreenErrorMessage = null;
                // only call loginAsyc when all input are validated
                if (g.TryValidateProperty("Name") && g.TryValidateProperty("Password"))
                    this._authenticationModel.LoginAsync(new LoginParameters(g.Name, g.Password, g.IsPersistent, null));
            }
        }

        private RelayCommand<LoginUser> _resetNowCommand = null;

        public RelayCommand<LoginUser> ResetNowCommand
        {
            get
            {
                if (_resetNowCommand == null)
                {
                    _resetNowCommand = new RelayCommand<LoginUser>(
                        g => this.OnResetNowCommand(g),
                        g => g != null);     
                }
                return _resetNowCommand;
            }
        }

        private void OnResetNowCommand(LoginUser g)
        {
            if (!this._passwordResetModel.IsBusy)
            {
                // clear any previous error message
                this.LoginScreenErrorMessage = null;
                // only call loginAsyc when user name is valid
                if (g.TryValidateProperty("Name"))
                {
                    this._passwordResetModel.GetUserByNameAsync(g.Name);
                }
            }
        }

        private RelayCommand _backToLoginCommand = null;

        public RelayCommand BackToLoginCommand
        {
            get
            {
                if (_backToLoginCommand == null)
                {
                    _backToLoginCommand = new RelayCommand(
                        () => this.OnBackToLoginCommand());
                }
                return _backToLoginCommand;
            }
        }

        private void OnBackToLoginCommand()
        {
            // clear any previous error message
            this.ResetPasswordScreenErrorMessage = null;
            // clear the password reset screen
            this.CurrentPasswordResetUser = new PasswordResetUser();
            // reject any changes
            this._passwordResetModel.RejectChanges();
            // and flip the screen.
            this.IsFlipped = false;
        }

        private RelayCommand<PasswordResetUser> _resetPasswordCommand;

        public RelayCommand<PasswordResetUser> ResetPasswordCommand
        {
            get
            {
                if (_resetPasswordCommand == null)
                {
                    _resetPasswordCommand = new RelayCommand<PasswordResetUser>(
                        g => this.OnResetPasswordCommand(g),
                        g => g != null);
                }
                return _resetPasswordCommand;
            }
        }

        private void OnResetPasswordCommand(PasswordResetUser g)
        {
            if (!this._passwordResetModel.IsBusy)
            {
                // clear any previous error message
                this.ResetPasswordScreenErrorMessage = null;
                // only call SaveUserAsync when all input are validated
                if (g.TryValidateProperty("Name") && g.TryValidateProperty("NewPassword")
                    && g.TryValidateProperty("PasswordConfirmation") && g.TryValidateProperty("PasswordAnswer"))
                    this._passwordResetModel.SaveUserAsync();
            }
        }

        #endregion "Public Commands"

        #region "Private Methods"
        private void _authenticationModel_LoginComplete(object sender, LoginOperationEventArgs e)
        {
            if (e.LoginOp.LoginSuccess)
            {
                // login successful, clear the login screen
                this.CurrentUser = new LoginUser();
            }
            else if (e.HasError)
            {
                LoginScreenErrorMessage = e.LoginOp.Error.Message;
            }
        }

        private void _passwordResetModel_GetUserComplete(object sender, EntityResultsArgs<PasswordResetUser> e)
        {
            if (!e.HasError)
            {
                // found the user, clear the login screen before leaving
                this.CurrentUser = new LoginUser();
                //populate the password reset screen
                if (e.Results.Count() == 1)
                {
                    var enumerator = e.Results.GetEnumerator();
                    enumerator.MoveNext();
                    this.CurrentPasswordResetUser = enumerator.Current;

                    // and flip the screen.
                    this.IsFlipped = true;
                }
            }
            else
            {
                // cannot find the user ID, no need to notify the user either
                // in case someone could use this to find out valid user IDs.
            }
        }

        private void _passwordResetModel_SaveUserComplete(object sender, ResultsArgs e)
        {
            if (!e.HasError)
            {
                // reset password successful, clear the reset password screen before leaving
                this.CurrentPasswordResetUser = new PasswordResetUser();
                // and flip the screen.
                this.IsFlipped = false;
            }
            else
            {
                this.ResetPasswordScreenErrorMessage = e.Error.Message;
            }
        }
        #endregion "Private Methods"
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Weidong Shen
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 7 Jul 2011
Article Copyright 2010 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid