public async Task UpgradeMembership(Guid id)
        {
            var entity = await GenericEntityFactory <Membership> .GetEntityAsync(_simpleEventStoreDbContext, id.ToString());

            entity.UpgradeMembership();

            await _simpleEventStoreDbContext.SavePendingEventsAsync(entity.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(entity.PendingEvents);
        }
        public async Task PointsEarned(Guid id, double points, MembershipPointsType type)
        {
            var entity = await GenericEntityFactory <Membership> .GetEntityAsync(_simpleEventStoreDbContext, id.ToString());

            entity.PointsEarned(points, type);

            await _simpleEventStoreDbContext.SavePendingEventsAsync(entity.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(entity.PendingEvents);
        }
Beispiel #3
0
        public async Task <bool> ChangeCustomerNameAsync(Guid customerId, string firstName, string lastName)
        {
            var customer = await GenericEntityFactory <Customer> .GetEntityAsync(_eventStoreDbContext, customerId.ToString());

            customer.ChangeName(firstName, lastName);
            await _eventStoreDbContext.SavePendingEventsAsync(customer.PendingEvents, 1, "Sample");

            await _eventBusService.Publish(customer.PendingEvents);

            return(true);
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            // NOTE: this must go at the end of Configure
            // ensure db is created
            var serviceScopeFactory = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>();

            using (var serviceScope = serviceScopeFactory.CreateScope())
            {
                var eventStoreDbContext = serviceScope.ServiceProvider.GetService <SimpleEventStoreDbContext>();
                eventStoreDbContext.Database.EnsureCreated();

                // readonly initialization hack for sample purpose
                var readModelDbContext = serviceScope.ServiceProvider.GetService <ReadModelDbContext>();
                readModelDbContext.Database.EnsureCreated();

                var customerFactory    = new GenericEntityFactory <Customer>(eventStoreDbContext);
                var customerEntities   = customerFactory.GetAllEntitiesAsync().Result;
                var customerReadModels = customerEntities.Select(c =>
                {
                    var readModel = new CustomerReadModel();
                    readModel.FromAggregate(c);
                    return(readModel);
                });
                readModelDbContext.Customers.AddRange(customerReadModels);

                // As aggregate membership is a readmodel, we initialize it like this.
                var aggregateMembershipFactory    = new GenericEntityFactory <Core.Membership>(eventStoreDbContext);
                var aggregateMembershipEntities   = aggregateMembershipFactory.GetAllEntitiesAsync().Result;
                var aggregateMembershipReadModels = aggregateMembershipEntities.Select(am =>
                {
                    var model = new MembershipReadModel();
                    model.FromAggregate(am);
                    return(model);
                });
                readModelDbContext.Memberships.AddRange(aggregateMembershipReadModels);

                readModelDbContext.SaveChanges();

                // reporting initialization

                //// readonly initialization hack for sample purpose
                //var reportingDbContext = serviceScope.ServiceProvider.GetService<ReportingReadModelDbContext>();
                //reportingDbContext.Database.EnsureCreated();

                //var reportingModelEventHandlers = new MembershipPointsReportingReadModelHandlers(reportingDbContext);
                //var customerregisteredEvents = eventStoreDbContext.FindEventsAsync<CustomerRegisteredEvent>().Result;
                //var membershipCreatedEvents = eventStoreDbContext.FindEventsAsync<MembershipCreatedEvent>().Result;
                //var membershipPointsEarnedEvents = eventStoreDbContext.FindEventsAsync<MembershipPointsEarnedEvent>().Result;

                //List<dynamic> dynamicCustomerRegistered = customerregisteredEvents
                //    .Select(e => e.EntityJson.FromJson(Type.GetType(e.EntityType)))
                //    .ToList();

                //List<dynamic> dynamicMembershipCreated = membershipCreatedEvents
                //    .Select(e => e.EntityJson.FromJson(Type.GetType(e.EntityType)))
                //    .ToList();

                //List<dynamic> dynamicMembershipPointsEarned = membershipPointsEarnedEvents
                //    .Select(e => e.EntityJson.FromJson(Type.GetType(e.EntityType)))
                //    .ToList();
                //foreach(var @event in dynamicCustomerRegistered)
                //{
                //    reportingModelEventHandlers.Handle(@event);
                //}
                //foreach(var @event in dynamicMembershipCreated)
                //{
                //    reportingModelEventHandlers.Handle(@event);
                //}
                //foreach(var @event in dynamicMembershipPointsEarned)
                //{
                //    reportingModelEventHandlers.Handle(@event);
                //}
            }
        }