Click here to Skip to main content
15,891,763 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.6K   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.Linq;
    using System.Text;
    using VAL.Model;
    using VAL.Contracts;
    using VAL.Repository;
    using VAL.Common;
    using VAL.Common.Properties;

    public class GroupTypeDomainService :
        DomainServiceBase, IGroupTypeService
    {
        public GroupTypeDomainService(IGroupTypeRepository repository) 
        {
            this.Repository = repository;
        }

        #region Private Members

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

        #endregion


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

            var groupTypes = Repository.Query
                    .Include("Groups")
                    .ToArray();

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

            return groupTypes;
        }

        /// <summary>
        /// Either inserts or updates a <see cref="Group"/> instance
        /// </summary>
        /// <param name="file">The <see cref="Group"/> to persist to the database</param>
        public GroupType SaveGroupType(GroupType groupType)
        {
            #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>(groupType == null, "Group type parameter cannot be null");
            #endregion

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

            #endregion

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

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

            return groupType;
        }

        /// <summary>
        /// Deletes a group type from the database
        /// </summary>
        /// <param name="groupTypeId">The unique ID of the group type</param>
        /// <exception cref="InvalidOperationException">
        /// InvalidOperationException is thrown if any groups are still assigned to the group type
        /// </exception>
        public void DeleteGroupType(int groupTypeId)
        {
            #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>(groupTypeId <= 0, "Group type id must be greater than zero");
            #endregion

            GroupType groupType = Repository.Query
                .Include("Groups")
                .SingleOrDefault(g => g.Id == groupTypeId);

            if (groupType.Groups.Count > 0)
            {
                throw new InvalidOperationException(Resources.CannotDeleteWhileGroupsExists);
            }

            Repository.Delete(g => g.Id == groupType.Id);

            #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