示例#1
0
        /// <summary>
        /// Apply business method.
        /// </summary>
        /// <param name="leave">A leave value.</param>
        /// <returns>Returns a Leave object.</returns>
        public Leave Apply(Leave leave)
        {
            leave.Status        = LeaveStatuses.Pending;
            leave.DateSubmitted = DateTime.Now;
            leave.IsCompleted   = false;

            LeaveStatusLog log = CreateLog(leave);

            // Data access component declarations.
            var leaveDAC          = new LeaveDAC();
            var leaveStatusLogDAC = new LeaveStatusLogDAC();

            Validations.ValidateLeaveDates(leave);

            // Check for overlapping leaves.
            if (leaveDAC.IsOverlap(leave))
            {
                throw new ApplicationException("Date range is overlapping with another leave.");
            }

            using (TransactionScope ts =
                       new TransactionScope(TransactionScopeOption.Required))
            {
                // Step 1 - Calling Create on LeaveDAC.
                leaveDAC.Create(leave);

                // Step 2 - Calling Create on LeaveStatusLogDAC.
                log.LeaveID = leave.LeaveID;
                leaveStatusLogDAC.Create(log);

                ts.Complete();
            }

            return(leave);
        }
示例#2
0
        /// <summary>
        /// Apply business method.
        /// </summary>
        /// <param name="leave">A leave value.</param>
        /// <returns>Returns a Leave object.</returns>
        public Leave Apply(Leave leave)
        {
            leave.Status        = LeaveStatuses.Pending;
            leave.DateSubmitted = DateTime.Now;
            leave.IsCompleted   = false;

            LeaveStatusLog log = CreateLog(leave);

            // Data access component declarations.
            var leaveDAC          = new LeaveDAC();
            var leaveStatusLogDAC = new LeaveStatusLogDAC();

            // Activate Data Annotation validation.
            var context          = new ValidationContext(leave);
            var validationErrors = new List <ValidationResult>();

            Validator.TryValidateObject(leave, context, validationErrors, true);

            if (validationErrors.Count > 0)
            {
                throw new ApplicationException(validationErrors[0].ErrorMessage);
            }

            // Check for overlapping leaves.
            if (leaveDAC.IsOverlap(leave))
            {
                throw new ApplicationException("Date range is overlapping with another leave.");
            }

            using (TransactionScope ts =
                       new TransactionScope(TransactionScopeOption.Required))
            {
                // Step 1 - Calling Create on LeaveDAC.
                leaveDAC.Create(leave);

                // Step 2 - Calling Create on LeaveStatusLogDAC.
                log.LeaveID = leave.LeaveID;
                leaveStatusLogDAC.Create(log);

                ts.Complete();
            }

            return(leave);
        }