Click here to Skip to main content
15,889,833 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 107.4K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
namespace VAL.BusinessService
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;

    using VAL.Common;
    using VAL.Common.Properties;
    using VAL.Contracts;
    using VAL.Model;
    using VAL.Repository;

    /// <summary>
    /// The user service allows the client application access to user and file information
    /// required to use the main window of the application
    /// </summary>
    public class UserDomainService : DomainServiceBase, IUserService
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public UserDomainService(IGroupService groupService, 
            IUserRepository repository)
        {
            this.Repository = repository;
        }

        /// <summary>
        /// Access to the repository
        /// </summary>
        private IUserRepository Repository { get; set; }

        private IGroupService GroupService { get; set; }


        #region IUserService Members

        /// <summary>
        /// Gets a user object based on the windows identity name
        /// </summary>
        /// <param name="userName">The users windows identity name</param>
        /// <returns>A <see cref="User"/> object</returns>
        /// <exception cref="UserInactiveException">The user is currently set to inactive</exception>
        /// <exception cref="UserNotFoundException">The specified user name was not found in the database</exception>
        public User GetUser(string userName)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            #region Guard Parameter Validation
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(userName), "You must specify a user name");
            #endregion

            var query =
            from u in Repository.Query
            where u.WindowsIdentityName.ToUpper() == userName.ToUpper()
            select u;

            var user = query.FirstOrDefault();

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return user;
        }

        public User GetUser(int id)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            #region Guard Parameter Validation
            Guard.Against<ArgumentException>(id <= 0, "Invalid user id specified");
            #endregion

            var query =
            from u in Repository.Query
            where u.Id == id
            select u;

            var user = query.FirstOrDefault();

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return user;
        }

        /// <summary>
        /// Gets a list of all active <see cref="User"/>s that are defined within a particular <see cref="Group"/>
        /// </summary>
        /// <param name="groupId">The unique ID of the group</param>
        /// <returns>List of users defined within a group</returns>
        public User[] GetGroupUsers(int groupId)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            #region Guard Parameter Validation
            Guard.Against<ArgumentException>(groupId <= 0, "You must specify a group id");
            #endregion

            var users = Repository.Query.Where(
                    u => u.GroupUsers.Any
                        (gu => gu.GroupID == groupId && u.IsActive == true))
                        .ToArray();

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return users;
        }

        #endregion

        /// <summary>
        /// Gets a list of all <see cref="User"/>s that are defined in the database
        /// </summary>
        /// <returns>List of Users</returns>
        public User[] GetUsers()
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            var users = Repository.All();

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return users;
        }

        /// <summary>
        /// Gets a list of all active <see cref="User"/>s that are defined in the database
        /// </summary>
        /// <returns>List of active Users</returns>
        public User[] GetActiveUsers()
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            var query =
            from u in Repository.Query
            where u.IsActive == true
            select u;

            var users = query.ToArray();

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return users;
        }


        /// <summary>
        /// Either inserts or updates a <see cref="User"/> instance
        /// </summary>
        /// <param name="file">The <see cref="User"/> to persist to the database</param>
        public User SaveUser(User user)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            #region Guard Parameter Validation
            Guard.Against<ArgumentNullException>(user == null, "You must specify a valid user object");
            #endregion

            #region Business Rules Enforcement
            if (!user.IsValid)
                throw new VAL.Common.BusinessRuleException(user.ErrorMessage);

            #endregion

            if (user.Id == 0)
            {
                // If this is a new user, then check for an existing user with the same
                // windows identity name and throw an exception if one exists
                var existingUser = GetUser(user.WindowsIdentityName);
                if (existingUser != null)
                {
                    throw new VAL.Common.UserAlreadyExistsException(string.Format("User {0} already exists in the database!", user.WindowsIdentityName));
                }
            }

            user = Repository.Add(user, user.Id == 0);

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            return user;
        }


        /// <summary>
        /// Deletes a user from the database that matches the specified ID
        /// </summary>
        /// <param name="userId">The unique ID of the user</param>
        public void DeleteUser(int userId)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion
 
            #region Guard Parameter Validation
            Guard.Against<ArgumentException>(userId <= 0, "You must specify a valid user id");
            #endregion

            Repository.Delete(u => u.Id == userId);

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

        }


        /// <summary>
        /// Clones a set of permissions from one user to another
        /// </summary>
        /// <param name="userId">The user ID to use when cloning permissions</param>
        /// <param name="targetUserId">The unique ID of the user to apply the permissions to</param>
        public void CloneUserPermissions(User user, int targetUserId)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            #region Guard Parameter Validation
            Guard.Against<ArgumentNullException>(user == null, "user object cannot be null");
            Guard.Against<ArgumentException>(targetUserId <= 0, "Target user id must be grater than zero");
            #endregion

            var groups = GroupService.GetUserGroups(targetUserId);

            foreach (var group in groups)
            {
                var groupUser = new GroupUser
                {
                    GroupID = group.Id,
                    UserID = user.Id
                };

                user.GroupUsers.Add(groupUser);
            }

            this.Repository.Add(user, user.Id == 0);

            #region Exit Method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion
        }
    }
}

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
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions