コード例 #1
0
        /// <summary>
        /// Import Customers
        /// </summary>
        /// <param name="transaction"></param>
        public void ImportCustomers(out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                string importFileName = ConfigurationManager.AppSettings["CustomerImportData"];

                System.IO.StreamReader csv_file = File.OpenText(importFileName);

                _customerDataService.CreateSession();
                _customerDataService.BeginTransaction();

                Boolean firstLine            = true;
                int     customerRecordsAdded = 0;

                while (csv_file.Peek() >= 0)
                {
                    // read and add a line
                    string   line    = csv_file.ReadLine();
                    string[] columns = line.Split('\t');
                    if (firstLine == false)
                    {
                        if (ImportCustomer(columns) == true)
                        {
                            customerRecordsAdded++;
                        }
                    }
                    firstLine = false;
                }

                _customerDataService.CommitTransaction(true);

                csv_file.Close();

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add(customerRecordsAdded.ToString() + " provider successfully imported.");
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                transaction.ReturnMessage = new List <string>();
                string errorMessage = ex.Message;
                transaction.ReturnStatus = false;
                transaction.ReturnMessage.Add(errorMessage);
            }
            finally
            {
                _customerDataService.CloseSession();
            }
        }
コード例 #2
0
        /// <summary>
        /// Create Customer
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public Customer CreateCustomer(Customer customer, out TransactionalInformation transaction)
        {
            transaction = new TransactionalInformation();

            try
            {
                CustomerBusinessRules customerBusinessRules = new CustomerBusinessRules();
                ValidationResult      results = customerBusinessRules.Validate(customer);

                bool validationSucceeded           = results.IsValid;
                IList <ValidationFailure> failures = results.Errors;

                if (validationSucceeded == false)
                {
                    transaction = ValidationErrors.PopulateValidationErrors(failures);
                    return(customer);
                }

                _customerDataService.CreateSession();
                _customerDataService.BeginTransaction();
                _customerDataService.CreateCustomer(customer);
                _customerDataService.CommitTransaction(true);

                transaction.ReturnStatus = true;
                transaction.ReturnMessage.Add("Customer successfully created.");
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                transaction.ReturnMessage.Add(errorMessage);
                transaction.ReturnStatus = false;
            }
            finally
            {
                _customerDataService.CloseSession();
            }

            return(customer);
        }