Exemple #1
0
        private void UpdateAccount(AccountDomainModel item)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var entity = ctx.AccountSet.SingleOrDefault(b => b.AggregateId == item.Id);
                if (entity == null)
                {
                    throw new AggregateNotFoundException("account");
                }

                var customerEntityId =
                    ctx.CustomerSet.SingleOrDefault(c => c.AggregateId == item.CustomerId);
                if (customerEntityId == null)
                {
                    throw new AggregateNotFoundException("Bank account");
                }

                entity.Version             = item.Version;
                entity.Currency            = item.Currency;
                entity.CustomerAggregateId = item.CustomerId;
                entity.AccountState        = item.State;

                ctx.Entry(entity).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
 public void Create(CustomerEntity item)
 {
     using (var ctx = new BankAccountDbContext())
     {
         ctx.CustomerSet.Add(item);
         ctx.SaveChanges();
     }
 }
Exemple #3
0
 private void AddCustomer(CustomerDomainModel item)
 {
     using (var ctx = new BankAccountDbContext())
     {
         ctx.CustomerSet.Add(new CustomerEntity
         {
             AggregateId   = item.Id,
             Version       = item.Version,
             CustomerState = State.Open
         });
         ctx.SaveChanges();
     }
 }
        public void Update(Guid aggregateId, State customerState, int version)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var entity = ctx.CustomerSet.SingleOrDefault(cust => cust.AggregateId == aggregateId);
                if (entity == null)
                {
                    throw new ArgumentNullException($"Customer entity not found");
                }

                entity.CustomerState = customerState;
                entity.Version       = version;

                ctx.Entry(entity).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
        public TransferViewModel GetAccountById(Guid aggregateId)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var model = ctx.AccountSet.SingleOrDefault(a => a.AggregateId == aggregateId);
                if (model == null)
                {
                    throw new ArgumentNullException("account");
                }

                return(new TransferViewModel
                {
                    AggregateId = model.AggregateId,
                    CustomerId = model.CustomerAggregateId,
                    Version = model.Version
                });
            }
        }
Exemple #6
0
        public void Save(AccountDomainModel item)
        {
            AccountEntity entity;

            using (var ctx = new BankAccountDbContext())
            {
                entity = ctx.AccountSet.SingleOrDefault(a => a.AggregateId == item.Id);
            }

            if (entity == null)
            {
                this.AddAccount(item);
            }
            else
            {
                this.UpdateAccount(item);
            }
        }
Exemple #7
0
        public void Save(CustomerDomainModel item)
        {
            CustomerEntity entity;

            using (var ctx = new BankAccountDbContext())
            {
                entity = ctx.CustomerSet.SingleOrDefault(b => b.AggregateId == item.Id);
            }

            if (entity == null)
            {
                this.AddCustomer(item);
            }
            else
            {
                this.UpdateCustomer(item);
            }
        }
        private static async Task TestWithoutConcurrencyControl()
        {
            using (var dbContext = new BankAccountDbContext())
            {
                var account = await dbContext.NonConcurrentAccounts.FindAsync(1);

                ConsoleUtils.WriteInf($"Account Balance (Before):{account.Balance}");
            };

            var threads = new Thread[2];

            threads[0] = new Thread(async() =>
            {
                using (var dbContext = new BankAccountDbContext())
                {
                    var account = await dbContext.NonConcurrentAccounts.FindAsync(1);
                    account.Credit(100);
                    await dbContext.SaveChangesAsync();
                };
            });

            threads[1] = new Thread(async() =>
            {
                using (var dbContext = new BankAccountDbContext())
                {
                    var account = await dbContext.NonConcurrentAccounts.FindAsync(1);
                    account.Debit(200);
                    await dbContext.SaveChangesAsync();
                };
            });

            foreach (var t in threads)
            {
                t.Start();
            }

            Thread.Sleep(1000);
            using (var dbContext = new BankAccountDbContext())
            {
                var account = await dbContext.NonConcurrentAccounts.FindAsync(1);

                ConsoleUtils.WriteInf($"Account Balance (After):{account.Balance}");
            };
        }
        public IEnumerable <BankAccountViewModel> GetCustomers()
        {
            List <Guid> aggregates;

            using (var ctx = new BankAccountDbContext())
            {
                aggregates = ctx.CustomerSet.Select(c => c.AggregateId).ToList();
            }

            return(aggregates
                   .Select(this.RehydrateDomainModel)
                   .Select(customer =>
                           new BankAccountViewModel
            {
                FirstName = customer.Person.FirstName,
                LastName = customer.Person.LastName,
                Id = customer.Id
            })
                   .ToList());
        }
 public IEnumerable <AccountViewModel> GetAccountsByCustomerId(Guid customerId)
 {
     using (var ctx = new BankAccountDbContext())
     {
         var model = ctx.AccountSet.Where(a => a.CustomerAggregateId == customerId);
         var list  = new List <AccountViewModel>();
         foreach (var a in model)
         {
             list.Add(
                 new AccountViewModel
             {
                 Currency     = a.Currency,
                 CustomerId   = a.CustomerAggregateId,
                 AggregateId  = a.AggregateId,
                 AccountState = a.AccountState
             });
         }
         return(list);
     }
 }
Exemple #11
0
        private void AddAccount(AccountDomainModel item)
        {
            using (var ctx = new BankAccountDbContext())
            {
                var customerEntityId =
                    ctx.CustomerSet.SingleOrDefault(c => c.AggregateId == item.CustomerId);
                if (customerEntityId == null)
                {
                    throw new AggregateNotFoundException("Bank account");
                }

                ctx.AccountSet.Add(new AccountEntity
                {
                    AggregateId         = item.Id,
                    Version             = item.Version,
                    CustomerAggregateId = item.CustomerId,
                    Currency            = item.Currency,
                    AccountState        = State.Open
                });
                ctx.SaveChanges();
            }
        }
        static async Task Main(string[] args)
        {
            using (var dbContext = new BankAccountDbContext())
            {
                dbContext.Database.Migrate();
                await dbContext.NonConcurrentAccounts.AddAsync(new NonConcurrentAccount { Balance = 1000.0m });

                await dbContext.ConcurrentAccountsWithToken.AddAsync(new ConcurrentAccountWithToken { Balance = 1000.0m });

                await dbContext.ConcurrentAccountsWithRowVersion.AddAsync(new ConcurrentAccountWithRowVersion { Balance = 1000.0m });

                await dbContext.SaveChangesAsync();
            };

            Console.WriteLine("========== Concurrency Test with NonConcurrent Account ==============================");
            await TestWithoutConcurrencyControl();

            Console.WriteLine("\n\n========== Concurrency Test with Concurrent Account using Concurrent Token ==========");
            await ConcurrencyControlByConcurrencyToken();

            Console.WriteLine("\n\n========== Concurrency Test with Concurrent Account using Row Version ===============");
            await ConcurrencyControlByRowVersion();
        }
 public HomeController(BankAccountDbContext context)
 {
     _dbContext = context;
 }
        private static async Task ConcurrencyControlByConcurrencyToken()
        {
            using (var dbContext = new BankAccountDbContext())
            {
                var account = await dbContext.ConcurrentAccountsWithToken.FindAsync(1);

                ConsoleUtils.WriteInf($"Account Balance (Before):{account.Balance}");
            };

            var threads = new Thread[2];

            threads[0] = new Thread(async() =>
            {
                using (var dbContext = new BankAccountDbContext())
                {
                    var account = await dbContext.ConcurrentAccountsWithToken.FindAsync(1);
                    account.Credit(100);

                    try
                    {
                        await dbContext.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        ConsoleUtils.WriteErr(ex.Message);
                    }
                };
            });

            threads[1] = new Thread(async() =>
            {
                using (var dbContext = new BankAccountDbContext())
                {
                    var account = await dbContext.ConcurrentAccountsWithToken.FindAsync(1);
                    account.Debit(200);

                    try
                    {
                        await dbContext.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        ConsoleUtils.WriteErr(ex.Message);
                    }
                };
            });

            foreach (var t in threads)
            {
                t.Start();
            }

            Thread.Sleep(1000);

            using (var dbContext = new BankAccountDbContext())
            {
                var account = await dbContext.ConcurrentAccountsWithToken.FindAsync(1);

                ConsoleUtils.WriteInf($"Account Balance (After):{account.Balance}");
            };
        }