Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / XAML

A Silverlight application with the WCF RIA Services Class Library

Rate me:
Please Sign up or sign in to vote.
4.72/5 (11 votes)
3 Mar 2010CPOL8 min read 62.8K   1.8K   39  
A Silverlight application using the WCF RIA Services Class Library. Demonstrates how to implement a custom Authorization Service, utilize localized resources, and add unit tests for DAL.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Ria;
using System.Windows.Ria.ApplicationServices;
using BizApp.Resources;
using BizApp.Services.Server.DomainServices;
using BizApp.Services.Server.Models;

namespace BizApp.Views
{
    /// <summary>
    /// Registration window.
    /// </summary>
    public partial class RegistrationWindow : ChildWindow
    {
        private AuthenticationService authService = WebContext.Current.Authentication;
        private AuthenticationOperation authOperation;
        private UserRegistrationContext registrationContext = new UserRegistrationContext();
        private InvokeOperation<UserCreateStatus> registrationOperation;
        private RegistrationData registrationData = new RegistrationData();

        public RegistrationWindow()
        {
            InitializeComponent();

            this.Title = LocalizedStrings.RegisterWindowTitle;
            this.DataContext = registrationData;

            errorMessagePanel.Continue += new RoutedEventHandler(Continue_Click);
            errorMessagePanel.Cancel += new RoutedEventHandler(Cancel_Click);
        }

        /// <summary>
        /// OK button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Click(object sender, RoutedEventArgs e)
        {
            SubmitRegistrationData();
        }

        /// <summary>
        /// Submit registration data.
        /// </summary>
        private void SubmitRegistrationData()
        {
            // Update source explicitly. Otherwise, a user can submit empty values
            txtUserName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            txtFullName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            txtEmail.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            txtPassword.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();
            txtConfirmPassword.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();

            if (!validationSummary.HasErrors)
            {
                // Display loading indicator
                DisplayWait.Begin();

                registrationOperation = registrationContext.AddUser(registrationData, 
                    RegistrationOperation_Completed, null);
            }
        }

        /// <summary>
        /// Registration operation is completed.
        /// </summary>
        /// <param name="operation">ABizAppnous invoke operation.</param>
        private void RegistrationOperation_Completed(InvokeOperation<UserCreateStatus> operation)
        {
            if (operation.IsCanceled)
            {
                DialogResult = false;
                return;
            }

            if (operation.HasError)
            {
                errorMessagePanel.Show(LocalizedStrings.RegistrationFailedLabel, LocalizedStrings.UnableProcessRequestMessage, operation.Error.Message);
                operation.MarkErrorAsHandled();
                DisplayErrorMessage.Begin();
            }
            else
            {
                if(operation.Value == UserCreateStatus.Success)
                {
                    // User was successfully registered. Send login information
                    SubmitLoginData();
                }
                else
                {
                    string message = GetUserCreateStatusMessage(operation.Value);
                    errorMessagePanel.Show(LocalizedStrings.RegistrationFailedLabel, message);
                    DisplayErrorMessage.Begin();
                }

                operation = null;
            }
        }

        /// <summary>
        /// Submit login information.
        /// </summary>
        private void SubmitLoginData()
        {
            authOperation = authService.Login(
                new LoginParameters(registrationData.UserName, registrationData.Password, false, null),
                LoginOperation_Completed, null);
        }

        /// <summary>
        /// Login operation is completed.
        /// </summary>
        /// <param name="operation">LoginOperation.</param>
        private void LoginOperation_Completed(LoginOperation operation)
        {
            if (operation.LoginSuccess)
            {
                authOperation = null;
                DialogResult = true;
            }
            else
            {
                if (operation.HasError)
                {
                    errorMessagePanel.Show(LocalizedStrings.LoginFailedLabel, LocalizedStrings.UnableProcessRequestMessage, operation.Error.Message);
                    operation.MarkErrorAsHandled();
                }
                else
                {
                    errorMessagePanel.Show(LocalizedStrings.LoginFailedLabel, LocalizedStrings.LoginFailedMessage);
                }

                DisplayErrorMessage.Begin();
            }
        }

        /// <summary>
        /// Continue button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Continue_Click(object sender, RoutedEventArgs e)
        {
            txtUserName.Focus();
            DisplayRegistration.Begin();
        }

        /// <summary>
        /// Cancel button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            CancelOperation();
        }

        /// <summary>
        /// Cancel any operation and close the window.
        /// </summary>
        private void CancelOperation()
        {
            if (registrationOperation != null && registrationOperation.CanCancel)
                registrationOperation.Cancel();

            if (authOperation != null && authOperation.CanCancel)
                authOperation.Cancel();

            DialogResult = false;
        }

        /// <summary>
        /// Handle Esc as the Cancel button and Enter - as the OK button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RegistrationWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                    CancelOperation();
                    break;

                case Key.Enter:
                    if (registrationPanel.Visibility == Visibility.Visible)
                        SubmitRegistrationData();
                    break;
            }
        }

        /// <summary>
        /// Set the same width for all panels.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RegistrationWindow_LayoutUpdated(object sender, EventArgs e)
        {
            waitPanel.Width = registrationPanel.ActualWidth;
            errorMessagePanel.Width = registrationPanel.ActualWidth;
        }

        /// <summary>
        /// Get localized description of the UserCreateStatus code.
        /// </summary>
        /// <param name="status">UserCreateStatus code.</param>
        /// <returns>Localized description.</returns>
        private string GetUserCreateStatusMessage(UserCreateStatus status)
        {
            string message = string.Empty;
            switch (status)
            {
                case UserCreateStatus.Success:
                    message = LocalizedStrings.RegistrationStatusSuccess;
                    break;

                case UserCreateStatus.InvalidUserName:
                    message = LocalizedStrings.RegistrationStatusInvalidUserName;
                    break;

                case UserCreateStatus.InvalidPassword:
                    message = LocalizedStrings.RegistrationStatusInvalidPassword;
                    break;

                case UserCreateStatus.InvalidEmail:
                    message = LocalizedStrings.RegistrationStatusInvalidEmail;
                    break;

                case UserCreateStatus.DuplicateUserName:
                    message = LocalizedStrings.RegistrationStatusDuplicateUserName;
                    break;

                case UserCreateStatus.DuplicateEmail:
                    message = LocalizedStrings.RegistrationStatusDuplicateEmail;
                    break;

                case UserCreateStatus.UserRejected:
                    message = LocalizedStrings.RegistrationStatusUserRejected;
                    break;

                default:
                    break;
            }

            return message;
        }
    }
}

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
Latvia Latvia
Jevgenij lives in Riga, Latvia. He started his programmer's career in 1983 developing software for radio equipment CAD systems. Created computer graphics for TV. Developed Internet credit card processing systems for banks.
Now he is System Analyst in Accenture.

Comments and Discussions