Пример #1
0
        protected async Task <CustomerAggregate> Prepare(
            IEventStore store,
            IAggregateRootServices services,
            ICollection <Action <CustomerAggregate> > useCaseActions)
        {
            Expect.NotNull(store, nameof(store));
            Expect.NotNull(services, nameof(services));
            Expect.NotEmpty(useCaseActions, nameof(useCaseActions));

            var streamId = Guid.NewGuid().ToString("N");
            var r        = new CustomerAggregate(streamId, services);

            using (var sess = store.OpenSession(suppressAmbientTransaction: true))
            {
                foreach (var action in useCaseActions)
                {
                    action(r);
                    var ok = await sess.SaveUncommitedEventsAsync <CustomerAggregate, Customer>(r).ConfigureAwait(false);

                    if (!ok)
                    {
                        throw new Exception("failed to prepare root aggregate");
                    }
                }
            }

            return(r);
        }
Пример #2
0
        /// <summary>
        /// This method flattens the customer data based on the Aggregate from MongoDB.
        /// </summary>
        /// <param name="aggregate"></param>
        /// <returns></returns>
        public static List <Customer> ToCustomerHierarchy(this CustomerAggregate aggregate)
        {
            List <Customer> customers      = new List <Customer>();
            var             parentCustomer = string.Empty;
            var             parentCount    = 0;

            while (aggregate.Parents.Count > parentCount)
            {
                Customer customer = aggregate.Parents.Where(x => (x.Parent ?? string.Empty).Equals(parentCustomer)).FirstOrDefault();
                customers.Add(customer);
                parentCustomer = customer.Name;
                parentCount++;
            }

            customers.Add(new Customer()
            {
                Name       = aggregate.Name,
                CustomerId = aggregate.CustomerId,
                Address    = aggregate.Address,
                Country    = aggregate.Country,
                Id         = aggregate.Id,
                Parent     = aggregate.Parent,
                RMAddress  = aggregate.RMAddress,
                RMName     = aggregate.RMName
            });

            return(customers);
        }
Пример #3
0
        public void SetCustomer(CustomerAggregate customerAggregate)
        {
            customer = customerAggregate;
            var address = addressService.FindOne(customer.AddressId);

            SetFields();
        }
Пример #4
0
        static void Main(string[] args)
        {
            var customer = new CustomerAggregate(Enumerable.Empty <dynamic>());

            customer.Deposit(new Money(20));

            Console.WriteLine("Customer account balance is {0}", customer.AccountBalance);
        }
Пример #5
0
        public void When_Making_A_Withdrawal()
        {
            var customer = new CustomerAggregate(Enumerable.Empty <dynamic>());

            customer.Withdraw(new Money(10));

            Assert.That(customer.Events.Any(x => x is CustomerWithdrawnMoney), Is.True);
            Assert.That(customer.AccountBalance, Is.EqualTo(-10));
        }
Пример #6
0
        public void When_Making_A_Deposit()
        {
            var customer = new CustomerAggregate(Enumerable.Empty <dynamic>());

            customer.Deposit(new Money(10));

            Assert.That(customer.Events.Any(x => x is CustomerDepositedMoney), Is.True);
            Assert.That(customer.AccountBalance, Is.EqualTo(10));
        }
Пример #7
0
        //  [Authorize(Operations.RegisterCustomer)] => move to Facad service
        public void Handle(RegisterCustomerCommand command)
        {
            var homeAddress = new Address(command.HomeAddress_PostalCode, command.HomeAddress_City, command.HomeAddress_Province);
            var workAddress = new Address(command.WorkAddress_PostalCode, command.WorkAddress_City, command.WorkAddress_Province);
            var aggregate   = new CustomerAggregate(command.FirstName, command.LastName, command.NationalCode, homeAddress, workAddress, bus);

            customerRepository.Create(aggregate);
            //  aggregate.EventBus.Publish(new CustomerCreatedEvent(aggregate.Id));
        }
Пример #8
0
        public void When_Changing_Customers_Name()
        {
            var customer = new CustomerAggregate(Enumerable.Empty <dynamic>());

            customer.ChangeName("Onam", "Chilwan");

            Assert.That(customer.Events.Any(x => x is CustomerNameChanged), Is.True);
            Assert.That(customer.Forename, Is.EqualTo("Onam"));
            Assert.That(customer.Surname, Is.EqualTo("Chilwan"));
        }
Пример #9
0
        public void Update(CustomerAggregate aggregate, int customerId)
        {
            var existingCustomer = FindOne(customerId);

            existingCustomer.lastUpdate   = DateTime.Now.ToUniversalTime();
            existingCustomer.lastUpdateBy = _authRepository.Username;
            existingCustomer.customerName = aggregate.CustomerName;
            existingCustomer.addressId    = aggregate.AddressId;

            _repository.Update(existingCustomer, customerId);
        }
Пример #10
0
        private void AddCustomer()
        {
            customer = new CustomerAggregate();
            UpdateFields();
            validateFields();

            var addressId = UpdateAddress();

            customer.AddressId = addressId;
            customerService.Add(customer);
        }
Пример #11
0
        public async Task SaveUncommitedEventsSucceedsOnNoEvents()
        {
            var r = new CustomerAggregate(Guid.NewGuid().ToString("N"), Fixture.AggregateRootServices);

            using (var sess = Fixture.Store.OpenSession())
            {
                var ok = await sess.SaveUncommitedEventsAsync <CustomerAggregate, Customer>(r).ConfigureAwait(false);

                Assert.True(ok, CreateMessage("saving while no uncommited events exist"));
                Assert.Equal(0, r.CurrentVersion);
            }
        }
Пример #12
0
        public static CustomerAggregate AsDirtyCustomerAggregate(
            this ICollection <Action <CustomerAggregate> > useCase,
            IAggregateRootServices services,
            string streamId = null)
        {
            var id = streamId ?? Guid.NewGuid().ToString("N");
            var r  = new CustomerAggregate(id, services);

            foreach (var action in useCase)
            {
                action(r);
            }

            return(r);
        }
Пример #13
0
        public void When_Loading_The_Customer()
        {
            var events = new List <dynamic>()
            {
                new CustomerNameChanged("Onam", "Chilwan"),
                new CustomerDepositedMoney(5.50M),
                new CustomerDepositedMoney(5M),
                new CustomerWithdrawnMoney(1.50M)
            };
            var customer = new CustomerAggregate(events);

            Assert.That(customer.Events.ToList(), Is.Empty);
            Assert.That(customer.Forename, Is.EqualTo("Onam"));
            Assert.That(customer.Surname, Is.EqualTo("Chilwan"));
            Assert.That(customer.AccountBalance, Is.EqualTo(9M));
        }
Пример #14
0
        public async Task SaveAndHydrateWithinOneSession()
        {
            var r1 = UseCases.Simple().AsDirtyCustomerAggregate(Fixture.AggregateRootServices);

            using (var sess = Fixture.Store.OpenSession())
            {
                var ok = await sess.SaveUncommitedEventsAsync <CustomerAggregate, Customer>(r1).ConfigureAwait(false);

                Assert.True(ok, CreateMessage("saving uncommited events"));

                var r2 = new CustomerAggregate(r1.StreamId, Fixture.AggregateRootServices);
                ok = await sess.HydrateAsync <CustomerAggregate, Customer>(r2).ConfigureAwait(false);

                Assert.True(ok, "hydrating previously saved user");
                var result = Comparer.Compare(r1, r2);
                Assert.True(result.AreEqual, CreateMessage(result.DifferencesString));
            }
        }
Пример #15
0
        protected async Task <(bool ok, string error)> Verify(string streamId, CustomerAggregate expected)
        {
            var r = new CustomerAggregate(streamId, Fixture.AggregateRootServices);

            using (var sess = Fixture.Store.OpenSession(suppressAmbientTransaction: true))
            {
                var ok = await sess.HydrateAsync <CustomerAggregate, Customer>(r).ConfigureAwait(false);

                if (!ok)
                {
                    return(false, "failed to load & hydrate from the store");
                }
            }

            var result = Comparer.Compare(r, expected);

            return(result.AreEqual
                ? (true, string.Empty)
                : (false, result.DifferencesString));
        }
Пример #16
0
        private async Task <CustomerAggregate> CreateExistingCustomerAggregate()
        {
            var createUnitOfWork = new CustomerUnitOfWork(Database, CollectionName);
            var createdCustomer  = new CustomerAggregate
            {
                Name             = "John Smith",
                DefaultAddresses = new CustomerDefaultAddresses
                {
                    InvoiceAddress = new AddressAggregate
                    {
                        HouseName = "Invoice Address"
                    }
                }
            };

            createUnitOfWork.Customers.Add(createdCustomer);
            await createUnitOfWork.CommitAsync();

            return(createdCustomer);
        }
Пример #17
0
        public void Add(CustomerAggregate aggregate)
        {
            if (String.IsNullOrWhiteSpace(aggregate.CustomerName))
            {
                throw new InvalidInputException("Must include name");
            }

            var date = DateTime.Now.ToUniversalTime();

            _repository.Add(new customer()
            {
                active       = true,
                addressId    = aggregate.AddressId,
                createDate   = date,
                lastUpdate   = date,
                createdBy    = _authRepository.Username,
                lastUpdateBy = _authRepository.Username,
                customerName = aggregate.CustomerName,
            });
        }
 public void Update(CustomerAggregate customerAggregate)
 {
     throw new NotImplementedException();
 }
 public async Task Handle(RegisterCustomerCommand command)
 {
     var customer = new CustomerAggregate(command.FirstName, command.LastName, command.NationalCode);
     await _customerRepository.Save(customer);
 }
 public void Create(CustomerAggregate customerAggregate)
 {
     session.Save(customerAggregate);
 }
Пример #21
0
 public void Handle(AddSubscription c, CustomerAggregate customer)
 {
     customer.ChangeSubscription(c.LevelCode, c.StartDate);
 }
Пример #22
0
 public void Handle(RemoveSubscription c, CustomerAggregate customer)
 {
     customer.RemoveSubscription(c.EndDate);
 }
Пример #23
0
 public void Handle(Create c, CustomerAggregate customer)
 {
     customer.Create(c.CustomerName, c.LevelCode, c.StartDate);
 }
Пример #24
0
        public void Add(Guid id, string name)
        {
            var customer = CustomerAggregate.Add(id, name);

            _repository.Save(customer, Guid.NewGuid(), null);
        }
 public async Task Save(CustomerAggregate customerAggregate)
 {
     await _unitOfWork.Set <CustomerAggregate>().AddAsync(customerAggregate);
 }