/// <summary>
        /// Invokes the NewSession business operation.
        /// </summary>
        private async Task<NewSessionBusinessResponse> InvokeNewSession(IDatabaseConnection databaseConnection, SessionResource resource)
        {
            try
            {
                // Build the NewSession business request.
                NewSessionBusinessRequest newSessionBusinessRequest = new NewSessionBusinessRequest();

                // Build the Session business request element.
                if (resource.Session != null)
                {
                    NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = new NewSessionBusinessRequest.SessionBusinessRequestElement();
                    sessionBusinessRequestElement.Name = resource.Session.Name;
                    sessionBusinessRequestElement.StartDate = resource.Session.StartDate;
                    newSessionBusinessRequest.Session = sessionBusinessRequestElement;
                }

                // Invoke the NewSession business operation.
                NewSessionBusinessResponse newSessionBusinessResponse = await this.schedulingBusinessLogicComponent.NewSession(databaseConnection, newSessionBusinessRequest);

                // The business operation succeeded.
                return newSessionBusinessResponse;
            }
            catch (NewSessionBusinessException newSessionBusinessException)
            {
                // Wrap the NewSession business exception into a service exception.
                ServiceException serviceException = ServiceExceptionBuilder.BuildServiceException(
                    "SchedulingServiceComponent.SessionsController.Post()",
                    newSessionBusinessException,
                    newSessionBusinessException.Errors.Select(error => error.ErrorCode.ToString()).ToArray(),
                    newSessionBusinessException.Errors.Select(error => error.ErroneousValue).ToArray());
                
                // Throw the service exception.
                throw serviceException;
            }
        }
        /// <summary>
        /// Executes the NewSession business operation.
        /// </summary>
        public async virtual Task<NewSessionBusinessResponse> NewSession(IDatabaseConnection databaseConnection, NewSessionBusinessRequest businessRequest)
        {
            // Validate the business request.
            this.ValidateNewSessionRequest(businessRequest);

            // Initialize the operation data.
            NewSessionOperationData operationData = new NewSessionOperationData();

            // Validate the business operation.
            await this.ValidateNewSessionOperation(databaseConnection, businessRequest, operationData);

            // Generate a unique Session code.
            string sessionCode = this.uniqueTokenGenerator.GenerateUniqueToken();

            // Create the Session data row.
            operationData.SessionDataRow = new SessionDataRow();
            operationData.SessionDataRow.SessionCode = sessionCode;
            operationData.SessionDataRow.Name = businessRequest.Session.Name;
            operationData.SessionDataRow.StartDate = businessRequest.Session.StartDate;
            await this.sessionDataAccessComponent.Create(databaseConnection, operationData.SessionDataRow);

            // Build the business response.
            NewSessionBusinessResponse businessResponse = new NewSessionBusinessResponse();

            // Build the Session business response element.
            NewSessionBusinessResponse.SessionBusinessResponseElement sessionBusinessResponseElement = new NewSessionBusinessResponse.SessionBusinessResponseElement();
            sessionBusinessResponseElement.SessionCode = operationData.SessionDataRow.SessionCode;
            businessResponse.Session = sessionBusinessResponseElement;

            // Return the business response.
            return businessResponse;
        }
        public void ShouldReturnSessionResponseElement()
        {
            // Build the test harness.
            SchedulingBusinessLogicComponentTestHarness testHarness = new SchedulingBusinessLogicComponentTestHarness();

            // Mock the generation of the unique token.
            testHarness.MockedUniqueTokenGenerator
                .Setup(mock => mock.GenerateUniqueToken())
                .Returns("6dk61ufcuzp3f7vs")
                .Verifiable();

            // Mock the creation of the Session data row.
            testHarness.MockedSessionDataAccessComponent
                .Setup(mock => mock.Create(
                    It.IsAny<IDatabaseConnection>(),
                    It.Is<SessionDataRow>(sessionDataRow =>
                    (
                        sessionDataRow.SessionCode == "6dk61ufcuzp3f7vs" &&
                        sessionDataRow.Name == "Session Alpha" &&
                        sessionDataRow.StartDate == new DateTime(2001, 1, 1)
                    ))))
                .Returns(Task.FromResult<object>(null))
                .Callback((IDatabaseConnection databaseConnection, SessionDataRow sessionDataRow) =>
                {
                    sessionDataRow.SessionID = 10001;
                })
                .Verifiable();

            // Build the NewSession business request.
            NewSessionBusinessRequest newSessionBusinessRequest = new NewSessionBusinessRequest();

            // Build the Session business request element.
            NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = new NewSessionBusinessRequest.SessionBusinessRequestElement();
            sessionBusinessRequestElement.Name = "Session Alpha";
            sessionBusinessRequestElement.StartDate = new DateTime(2001, 1, 1);
            newSessionBusinessRequest.Session = sessionBusinessRequestElement;

            // Invoke the NewSession business operation.
            NewSessionBusinessResponse newSessionBusinessResponse = testHarness.SchedulingBusinessLogicComponent.NewSession(testHarness.MockedDatabaseConnection, newSessionBusinessRequest).Result;

            // Verify the mocked components.
            testHarness.VerifyMockedComponents();

            // Validate the Session business response element.
            Assert.IsNotNull(newSessionBusinessResponse);
            Assert.IsNotNull(newSessionBusinessResponse.Session);
            Assert.AreEqual("6dk61ufcuzp3f7vs", newSessionBusinessResponse.Session.SessionCode);
        }
        /// <summary>
        /// Validates the NewSession business request.
        /// </summary>
        private void ValidateNewSessionRequest(NewSessionBusinessRequest businessRequest)
        {
            // Build the list of errors.
            List<NewSessionBusinessException.ErrorBusinessExceptionElement> errorBusinessExceptionElements = new List<NewSessionBusinessException.ErrorBusinessExceptionElement>();

            // Validate the Session business request element.
            this.ValidateNewSessionRequestProperty(businessRequest, "Session", businessRequest.Session, NewSessionBusinessException.ErrorCodes.InvalidSession, errorBusinessExceptionElements);
            if (businessRequest.Session != null)
            {
                // Validate the Session business request element properties.
                NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = businessRequest.Session;
                this.ValidateNewSessionRequestProperty(sessionBusinessRequestElement, "Name", sessionBusinessRequestElement.Name, NewSessionBusinessException.ErrorCodes.InvalidName, errorBusinessExceptionElements);
            }

            // Check if any errors were added to the list.
            if (errorBusinessExceptionElements.Any())
            {
                // Throw a NewSession business exception.
                NewSessionBusinessException businessException = this.BuildNewSessionBusinessException(errorBusinessExceptionElements.ToArray());
                throw businessException;
            }
        }
        /// <summary>
        /// Should throw the InvalidName error code.
        /// </summary>
        private void ShouldThrowInvalidNameErrorCode(string name)
        {
            // Build the test harness.
            SchedulingBusinessLogicComponentTestHarness testHarness = new SchedulingBusinessLogicComponentTestHarness();

            // Build the NewSession business request.
            NewSessionBusinessRequest newSessionBusinessRequest = new NewSessionBusinessRequest();

            // Build the Session business request element.
            NewSessionBusinessRequest.SessionBusinessRequestElement sessionBusinessRequestElement = new NewSessionBusinessRequest.SessionBusinessRequestElement();
            sessionBusinessRequestElement.Name = name;
            sessionBusinessRequestElement.StartDate = new DateTime(2001, 1, 1);
            newSessionBusinessRequest.Session = sessionBusinessRequestElement;

            try
            {
                // Invoke the NewSession business operation.
                testHarness.SchedulingBusinessLogicComponent.NewSession(testHarness.MockedDatabaseConnection, newSessionBusinessRequest).Wait();

                // Validate an exception was thrown.
                Assert.Fail();
            }
            catch (AggregateException ex)
            {
                // Verify the mocked components.
                testHarness.VerifyMockedComponents();

                // Validate a NewSession business exception was thrown.
                NewSessionBusinessException NewSessionBusinessException = ex.InnerExceptions[0] as NewSessionBusinessException;
                Assert.IsNotNull(NewSessionBusinessException);
                Assert.AreEqual("SchedulingBusinessLogicComponent.NewSession() has thrown a NewSession business exception. See the Errors property for details.", NewSessionBusinessException.Message);

                // Validate the NewSession business exception contains the InvalidName error code.
                Assert.IsNotNull(NewSessionBusinessException.Errors);
                Assert.AreEqual(1, NewSessionBusinessException.Errors.Length);
                Assert.AreEqual(NewSessionBusinessException.ErrorCodes.InvalidName, NewSessionBusinessException.Errors[0].ErrorCode);
                Assert.AreEqual(name, NewSessionBusinessException.Errors[0].ErroneousValue);
            }
        }
 /// <summary>
 /// Validates the NewSession business operation.
 /// </summary>
 private Task ValidateNewSessionOperation(IDatabaseConnection databaseConnection, NewSessionBusinessRequest businessRequest, NewSessionOperationData operationData)
 {
     return Task.FromResult<object>(null);
 }