Click here to Skip to main content
15,898,035 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 108.2K   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.Core;
    using VAL.Core.Properties;
    using VAL.Contracts;
    using VAL.Model;
    using VAL.Model.Entities;
    using VAL.Data;

    /// <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, 
            IRepository<User> repository, 
            IDbContext dataContext)
        {
            this.Repository = repository;
            this.GroupService = groupService;
            this.DataContext = dataContext;
        }

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

        private IGroupService GroupService { get; set; }

        private IDbContext DataContext { 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.Table
            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 user = Repository.GetById(id);

            #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.Table.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.Table.ToArray();

            #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.Table
            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.Core.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.Core.UserAlreadyExistsException(string.Format("User {0} already exists in the database!", user.WindowsIdentityName));
                }
            }

            Repository.Add(user);
            DataContext.SaveChanges();

            #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(GetUser(userId));
            DataContext.SaveChanges();

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

        }


        /// <summary>
        /// Creates a new user and clones a set of permissions from a target user into the newly created user
        /// </summary>
        /// <param name="domainName">Domain name of the user to create</param>
        /// <param name="firstName">First name of the user to create</param>
        /// <param name="surname">Surname of the user to create</param>
        /// <param name="targetUserId">Unique ID of the user to copy permissions from</param>
        public void CloneUserPermissions(string domainName, string firstName, string surname, int targetUserId)
        {
            #region Enter method Tracing
            if (log.IsDebugEnabled)
            {
                log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
            }
            #endregion

            var user = new User
            {
                WindowsIdentityName = domainName,
                Forename = firstName,
                Surname = surname,
                IsActive = true
            };

            #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
                };

                user.GroupUsers.Add(groupUser);
            }

            this.Repository.Add(user);
            DataContext.SaveChanges();

            #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