Example #1
0
        /// <summary>
        /// Removes the customer from the database.
        /// </summary>
        /// <param name="customer">The customer to be removed.</param>
        /// <returns>If successful returns datetime of the remove operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > RemoveCustomer(Customer customer)
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.FirstOrDefault(c => c.Id == customer.Id);

                    if (customerDb == null)
                    {
                        return(null);
                    }

                    context.CustomerDbs.Remove(customerDb);

                    await context.SaveChangesAsync();
                    await EnterModificationHistory(context, customer, ModificationType.Remove);

                    this.lastDataBaseModificationTime = DateTime.Now;

                    return(this.lastDataBaseModificationTime);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
        }
        /// <summary>
        /// Adds the customer to the database.
        /// </summary>
        /// <param name="customer">The customer to be added.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the add operation, otherwise null.</returns>
        public async Task<Nullable<DateTime>> AddCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be added");
                }

                if (!customer.IsValid)
                {
                    return null;
                }

                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = new CustomerDb()
                    {
                        AddressLineOne = customer.AddressLineOne,
                        Category = customer.Category,
                        Country = customer.Country,
                        DateOfBirth = customer.DateOfBirth,
                        Gender = customer.Gender,
                        HouseNumber = customer.HouseNumber,
                        Name = customer.Name,
                        State = customer.State
                    };

                    context.CustomerDbs.Add(customerDb);

                    await context.SaveChangesAsync(cancellationToken);
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        // The Id is created by the database so before
                        // logging modification history entry update the customer with the new Id.
                        customer.Id = customerDb.Id;
                        await EnterModificationHistory(context, customer, ModificationType.Add);
                        this.lastDataBaseModificationTime = DateTime.Now;
                        return this.lastDataBaseModificationTime;
                    }
                    return null;
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Adds the customer to the database.
        /// </summary>
        /// <param name="customer">The customer to be added.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the add operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > AddCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be added");
                }

                if (!customer.IsValid)
                {
                    return(null);
                }

                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = new CustomerDb()
                    {
                        AddressLineOne = customer.AddressLineOne,
                        Category       = customer.Category,
                        Country        = customer.Country,
                        DateOfBirth    = customer.DateOfBirth,
                        Gender         = customer.Gender,
                        HouseNumber    = customer.HouseNumber,
                        Name           = customer.Name,
                        State          = customer.State
                    };

                    context.CustomerDbs.Add(customerDb);

                    await context.SaveChangesAsync(cancellationToken);

                    if (!cancellationToken.IsCancellationRequested)
                    {
                        // The Id is created by the database so before
                        // logging modification history entry update the customer with the new Id.
                        customer.Id = customerDb.Id;
                        await EnterModificationHistory(context, customer, ModificationType.Add);

                        this.lastDataBaseModificationTime = DateTime.Now;
                        return(this.lastDataBaseModificationTime);
                    }
                    return(null);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }
Example #4
0
        /// <summary>
        /// Updates the customer in the database
        /// </summary>
        /// <param name="customer">The customer to be updated.</param>
        /// <param name="cancellationToken">Cancellation token to cancel save.</param>
        /// <returns>If successful returns datetime of the update operation, otherwise null.</returns>
        public async Task <Nullable <DateTime> > UpdateCustomer(Customer customer, CancellationToken cancellationToken = new CancellationToken())
        {
            try
            {
                if (customer == null)
                {
                    throw new ArgumentNullException("Null Customer cannot be updated.");
                }
                if (!customer.IsValid)
                {
                    return(null);
                }
                using (CustomersContext context = new CustomersContext())
                {
                    CustomerDb customerDb = context.CustomerDbs.Find(customer.Id);

                    if (customerDb == null)
                    {
                        return(null);
                    }

                    customerDb.AddressLineOne = customer.AddressLineOne;
                    customerDb.Category       = customer.Category;
                    customerDb.Country        = customer.Country;
                    customerDb.DateOfBirth    = customer.DateOfBirth;
                    customerDb.Gender         = customer.Gender;
                    customerDb.HouseNumber    = customer.HouseNumber;
                    customerDb.Name           = customer.Name;
                    customerDb.State          = customer.State;

                    await context.SaveChangesAsync(cancellationToken);

                    if (!cancellationToken.IsCancellationRequested)
                    {
                        await EnterModificationHistory(context, customer, ModificationType.Update);

                        this.lastDataBaseModificationTime = DateTime.Now;
                        return(this.lastDataBaseModificationTime);
                    }
                    return(null);
                }
            }
            catch (InvalidOperationException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (NullReferenceException e)
            {
                DbLog.LogException(e, "DataProxy.cs");
                throw;
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Cancelletion exception can be wrapped in several other exceptions.
                // Instead of unwrapping it check if cancellation requested.
                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                throw;
            }
        }