Click here to Skip to main content
15,886,519 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.7K   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.Collections.Generic;
using System.Linq;
using System.Web.DomainServices;
using System.Web.DomainServices.Providers;
using System.Web.Ria;
using Microsoft.Practices.Unity;
using BizApp.Services.Server.Models;

namespace BizApp.Services.Server.DomainServices
{
    /// <summary>
    /// User Registration Domain Service.
    /// </summary>
    /// <remarks>
    /// LinqToSqlMetadataProviderAttribute enables this domain service 
    /// to reference the Linq To SQL generated entities.
    /// </remarks>
    [LinqToSqlMetadataProvider(typeof(AppDatabaseDataContext))]
    [EnableClientAccess]
    public class UserRegistrationService : DomainServiceBase
    {
        // Users will be added to this role by default
        public const string DefaultRole = "Registered User";

        /// <summary>
        /// Gets or sets a repository for the UserAccount entities.
        /// </summary>
        /// <remarks>It is resolved in the DomainServiceFactory. 
        /// IRepository is mapped to the LinqToSqlRepository class in the unity.config.
        /// </remarks>
        [Dependency]
        public IRepository<UserAccount> UserAccountRepository { get; set; }

        /// <summary>
        /// Gets or sets a repository for the UserRole entities.
        /// </summary>
        /// <remarks>It is resolved in the DomainServiceFactory. 
        /// IRepository is mapped to the LinqToSqlRepository class in the unity.config.
        /// </remarks>
        [Dependency]
        public IRepository<UserRole> UserRoleRepository { get; set; }

        /// <summary>
        /// Add a new UserAccount to the database.
        /// </summary>
        /// <param name="user">Registration data.</param>
        /// <returns>UserCreateStatus.</returns>
        /// <exception cref="DomainException" />
        [Invoke]
        public UserCreateStatus AddUser(RegistrationData user)
        {
            if (user == null)
                throw new DomainException("Registration data is empty.");

            if (string.IsNullOrEmpty(user.UserName))
                return UserCreateStatus.InvalidUserName;
            
            if (string.IsNullOrEmpty(user.Email))
                return UserCreateStatus.InvalidEmail;
            
            if (string.IsNullOrEmpty(user.Password))
                return UserCreateStatus.InvalidPassword;

            // Check if an account with this UserName already exists
            var userAccounts = from u in UserAccountRepository.Query()
                               where u.UserName == user.UserName
                               select u;

            if (userAccounts.Count() > 0)
                return UserCreateStatus.DuplicateUserName;

            // Check if an account with this Email already exists
            userAccounts = from u in UserAccountRepository.Query()
                           where u.Email == user.Email
                           select u;

            if (userAccounts.Count() > 0)
                return UserCreateStatus.DuplicateEmail;

            // Get default role ID
            var roles = from r in UserRoleRepository.Query()
                        where r.RoleName == DefaultRole
                        select r;

            UserRole role = roles.FirstOrDefault();

            if(role == null)
                throw new DomainException("Default Role does not exist in the database.");

            UserAccount userAccount = new UserAccount
            {
                UserName = user.UserName,
                FullName = user.FullName,
                Password = user.Password,
                Email = user.Email,
                RoleID = role.ID,
                Created = DateTime.Now
            };

            UserAccountRepository.Add(userAccount);
            UserAccountRepository.SubmitChanges();

            return UserCreateStatus.Success;
        }

        // This is never used but without it RIA Services will complain RegistrationData
        // is not exposed as an entity
        public IEnumerable<RegistrationData> GetUsers()
        {
            throw new NotSupportedException();
        }
    }
}

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