Example #1
0
        private ResultWithEnum SaveCustomer(Customer customer)
        {
            try
            {
                using (var context = new MyContext())
                {
                    context.Customers.Add(customer);
                    context.SaveChanges();
                }

                return(ResultWithEnum.Ok());
            }
            catch (DbUpdateException ex)
            {
                // Обрабатываем только те исключения, которые можем.
                if (ex.Message == "Unable to open the DB connection")
                {
                    return(ResultWithEnum.Fail(ErrorType.DatabaseIsOffline));
                }

                if (ex.Message.Contains("IX_Customer_Name"))
                {
                    return(ResultWithEnum.Fail(ErrorType.CustomerAlreadyExists));
                }

                // Все остальные исключения - это аварийные случаи и ловятся только
                // на самом верхнем уровне.
                throw;
            }
        }
        private ResultWithEnum SaveCustomer(Customer customer)
        {
            try
            {
                using (var context = new MyContext())
                {
                    context.Customers.Add(customer);
                    context.SaveChanges();
                }
                return(ResultWithEnum.Ok());
            }
            catch (DbUpdateException ex)
            {
                if (ex.Message == "Unable to open the DB connection")
                {
                    ResultWithEnum.Fail(ErrorType.DatabaseIsOffline);
                }

                if (ex.Message.Contains("IX_Customer_Name"))
                {
                    return(ResultWithEnum.Fail(ErrorType.CustomerAlreadyExists));
                }

                throw;
            }
        }
Example #3
0
        private ResultWithEnum <Customer> GetCustomer(int id)
        {
            try
            {
                using (var context = new MyContext())
                {
                    return(ResultWithEnum.Ok(context.Customers.Single(x => x.Id == id)));
                }
            }
            catch (DbUpdateException ex)
            {
                if (ex.Message == "Unable to open the DB connection")
                {
                    return(ResultWithEnum.Fail <Customer>(ErrorType.DatabaseIsOffline));
                }

                throw;
            }
        }
        public void CreateCustomer(string name)
        {
            var            customer = new Customer(name);
            ResultWithEnum result   = SaveCustomer(customer);

            if (result.IsFailure)
            {
                switch (result.ErrorType)
                {
                case ErrorType.DatabaseIsOffline:
                    MessageBox.Show("Unable to connect to the database. Please try again later");
                    break;

                case ErrorType.CustomerAlreadyExists:
                    MessageBox.Show("A customer with the name " + name + " already exists");
                    break;

                default:
                    throw new ArgumentException();
                }
            }
        }