Click here to Skip to main content
12,699,883 members (35,841 online)
Click here to Skip to main content

Stats

893.5K views
30.6K downloads
305 bookmarked
Posted

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.
IssueVision for Silverlight Setup
IssueVisionSetup.msi
setup.exe
IssueVisionSetup.msi
setup.exe
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
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
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
IssueVisionSetup.msi
setup.exe
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 += _authenticationModel_LoginComplete;
            _passwordResetModel.GetUserComplete += _passwordResetModel_GetUserComplete;
            _passwordResetModel.SaveUserComplete += _passwordResetModel_SaveUserComplete;

            // Show user login screen first
            IsFlipped = false;

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

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

        #region "Public Properties"

        private bool _isFlipped;

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

        private LoginUser _currentUser;

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

        private PasswordResetUser _currentPasswordResetUser;

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

        private string _loginScreenErrorMessage;

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

        private string _resetPasswordScreenErrorMessage;

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

        #endregion "Public Properties"

        #region "Public Commands"

        private RelayCommand<LoginUser> _loginCommand;

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

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

        private RelayCommand<LoginUser> _resetNowCommand;

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

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

        private RelayCommand _backToLoginCommand;

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

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

        private RelayCommand<PasswordResetUser> _resetPasswordCommand;

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

        private void OnResetPasswordCommand(PasswordResetUser g)
        {
            if (!_passwordResetModel.IsBusy)
            {
                // clear any previous error message
                ResetPasswordScreenErrorMessage = null;
                // only call SaveUserAsync when all input are validated
                if (g.TryValidateProperty("Name") && g.TryValidateProperty("NewPassword")
                    && g.TryValidateProperty("PasswordConfirmation") && g.TryValidateProperty("PasswordAnswer"))
                    _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
                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
                CurrentUser = new LoginUser();
                //populate the password reset screen
                if (e.Results.Count() == 1)
                {
                    var enumerator = e.Results.GetEnumerator();
                    enumerator.MoveNext();
                    CurrentPasswordResetUser = enumerator.Current;

                    // and flip the screen.
                    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
                CurrentPasswordResetUser = new PasswordResetUser();
                // and flip the screen.
                IsFlipped = false;
            }
            else
            {
                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

You may also be interested in...

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