Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / XAML

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

Rate me:
Please Sign up or sign in to vote.
4.84/5 (108 votes)
7 Jul 2011CPOL9 min read 2.1M   30.9K   298  
Part 1 of a series describing the creation of a Silverlight business application using MEF, MVVM Light, and WCF RIA Services.
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;

namespace IssueVision.Data.Web
{
    /// <summary>
    /// User class client-side extensions
    /// </summary>
    public partial class User
    {
        /// <summary>
        /// Returns each user in the format of "user (FirstName LastName)"
        /// </summary>
        public string DisplayName
        {
            get
            {
                if (this._name == null)
                    return " ";
                else
                    return this._name + " (" + this._firstName + " " + this._lastName + ")";
            }
        }

        /// <summary>
        /// Returns each user type in the format of "Admin" or "User"
        /// </summary>
        public string DisplayUserType
        {
            get
            {
                if (this.UserType == "A")
                {
                    // Admin User
                    return IssueVisionServiceConstant.UserTypeAdmin;
                }
                else if (this.UserType == "U")
                {
                    // Normal User
                    return IssueVisionServiceConstant.UserTypeUser;
                }
                else
                    return string.Empty;
            }
            set
            {
                if (value != null)
                {
                    if (value.Contains(IssueVisionServiceConstant.UserTypeAdmin))
                    {
                        // Admin User
                        this.UserType = "A";
                    }
                    else if (value.Contains(IssueVisionServiceConstant.UserTypeUser))
                    {
                        // Normal User
                        this.UserType = "U";
                    }
                    else
                        this.UserType = String.Empty;
                }
            }
        }

        /// <summary>
        /// Stores the new password the user entered in the MyProfile UI,
        /// even if it is invalid.  This way we can validate the new password
        /// confirmation adequately all the times
        /// </summary>
        public string ActualNewPassword { get; set; }

        [Display(Name = "NewPasswordConfirmationLabel", ResourceType = typeof(IssueVisionResources))]
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))]
        [CustomValidation(typeof(User), "CheckNewPasswordConfirmation")]
        public string NewPasswordConfirmation
        {
            get
            {
                return this._newPasswordConfirmation;
            }

            set
            {
                this.ValidateProperty("NewPasswordConfirmation", value);
                this._newPasswordConfirmation = value;
                this.RaisePropertyChanged("NewPasswordConfirmation");
            }
        }
        private string _newPasswordConfirmation;

        /// <summary>
        /// Stores the password answer the user entered in the MyProfile UI,
        /// even if it is invalid.  This way we can validate the password answer
        /// confirmation adequately all the times
        /// </summary>
        public string ActualPasswordAnswer { get; set; }

        [Display(Name = "PasswordAnswerConfirmationLabel", ResourceType = typeof(IssueVisionResources))]
        [Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))]
        [CustomValidation(typeof(User), "CheckPasswordAnswerConfirmation")]
        public string PasswordAnswerConfirmation
        {
            get
            {
                return this._passwordAnswerConfirmation;
            }

            set
            {
                this.ValidateProperty("PasswordAnswerConfirmation", value);
                this._passwordAnswerConfirmation = value;
                this.RaisePropertyChanged("PasswordAnswerConfirmation");
            }
        }
        private string _passwordAnswerConfirmation;

        /// <summary>
        /// Custom validation of whether new password and confirm password match
        /// </summary>
        /// <param name="newPasswordConfirmation"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckNewPasswordConfirmation(string newPasswordConfirmation, ValidationContext validationContext)
        {
            User currentUser = (User)validationContext.ObjectInstance;

            if (!string.IsNullOrEmpty(currentUser.ActualNewPassword) &&
                !string.IsNullOrEmpty(newPasswordConfirmation) &&
                currentUser.ActualNewPassword != newPasswordConfirmation)
            {
                return new ValidationResult(ErrorResources.ValidationErrorPasswordConfirmationMismatch, new string[] { "NewPasswordConfirmation" });
            }

            return ValidationResult.Success;
        }

        /// <summary>
        /// Custom validation of whether password answer and confirm password answer match
        /// </summary>
        /// <param name="passwordAnswerConfirmation"></param>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public static ValidationResult CheckPasswordAnswerConfirmation(string passwordAnswerConfirmation, ValidationContext validationContext)
        {
            User currentUser = (User)validationContext.ObjectInstance;

            if (!string.IsNullOrEmpty(currentUser.ActualPasswordAnswer) &&
                !string.IsNullOrEmpty(passwordAnswerConfirmation) &&
                currentUser.ActualPasswordAnswer != passwordAnswerConfirmation)
            {
                return new ValidationResult(ErrorResources.ValidationErrorPasswordAnswerConfirmationMismatch, new string[] { "PasswordAnswerConfirmation" });
            }

            return ValidationResult.Success;
        }

        /// <summary>
        /// Try validate the specified property for User class
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool TryValidateProperty(string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            if (propertyName == "Name" || propertyName == "FirstName"
                || propertyName == "LastName" || propertyName == "Email"
                || propertyName == "Password" || propertyName == "NewPassword"
                || propertyName == "NewPasswordConfirmation" || propertyName == "PasswordQuestion"
                || propertyName == "PasswordAnswer" || propertyName == "PasswordAnswerConfirmation"
                || propertyName == "UserType")
            {
                ValidationContext context = new ValidationContext(this, null, null) { MemberName = propertyName };

                var validationResults = new Collection<ValidationResult>();

                if (propertyName == "Name")
                    return Validator.TryValidateProperty(this.Name, context, validationResults);
                else if (propertyName == "FirstName")
                    return Validator.TryValidateProperty(this.FirstName, context, validationResults);
                else if (propertyName == "LastName")
                    return Validator.TryValidateProperty(this.LastName, context, validationResults);
                else if (propertyName == "Email")
                    return Validator.TryValidateProperty(this.Email, context, validationResults);
                else if (propertyName == "Password")
                    return Validator.TryValidateProperty(this.Password, context, validationResults);
                else if (propertyName == "NewPassword")
                    return Validator.TryValidateProperty(this.NewPassword, context, validationResults);
                else if (propertyName == "NewPasswordConfirmation")
                    return Validator.TryValidateProperty(this.NewPasswordConfirmation, context, validationResults);
                else if (propertyName == "PasswordQuestion")
                    return Validator.TryValidateProperty(this.PasswordQuestion, context, validationResults);
                else if (propertyName == "PasswordAnswer")
                    return Validator.TryValidateProperty(this.PasswordAnswer, context, validationResults);
                else if (propertyName == "PasswordAnswerConfirmation")
                    return Validator.TryValidateProperty(this.PasswordAnswerConfirmation, context, validationResults);
                else if (propertyName == "UserType")
                    return Validator.TryValidateProperty(this.UserType, context, validationResults);
            }
            return false;
        }
    }
}

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)


Written By
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

Comments and Discussions