Esempio n. 1
0
        public void SaveAndLoadWithEntityRepository()
        {
            Converts.Repository
            .AddJsonKey()
            .AddJsonEnumSearchHandler()
            .AddJsonPrimitivesSearchHandler()
            .AddJsonObjectSearchHandler();

            ICompositeTypeProvider       compositeTypeProvider = new ReflectionCompositeTypeProvider(new ReflectionCompositeDelegateFactory());
            IFactory <ICompositeStorage> storageFactory        = new GetterFactory <ICompositeStorage>(() => new JsonCompositeStorage());

            EventSourcingContext context    = new EventSourcingContext(@"Data Source=.\sqlexpress; Initial Catalog=EventStore;Integrated Security=SSPI");
            EntityEventStore     eventStore = new EntityEventStore(context);

            PersistentEventDispatcher eventDispatcher = new PersistentEventDispatcher(eventStore);

            AggregateRootRepository <Order> repository = new AggregateRootRepository <Order>(
                eventStore,
                new CompositeEventFormatter(compositeTypeProvider, storageFactory),
                new ReflectionAggregateRootFactory <Order>(),
                eventDispatcher,
                new NoSnapshotProvider(),
                new EmptySnapshotStore()
                );

            PersistentCommandDispatcher commandDispatcher = new PersistentCommandDispatcher(
                new SerialCommandDistributor(),
                new EntityCommandStore(context),
                new CompositeCommandFormatter(compositeTypeProvider, storageFactory)
                );

            CreateOrderHandler  createHandler  = new CreateOrderHandler(repository);
            AddOrderItemHandler addItemHandler = new AddOrderItemHandler(repository);

            commandDispatcher.Handlers
            .Add <CreateOrder>(createHandler)
            .Add <AddOrderItem>(addItemHandler);

            CreateOrder create = new CreateOrder();

            commandDispatcher.HandleAsync(create);

            eventDispatcher.Handlers.Await <OrderPlaced>().Wait();

            IEnumerable <EventModel> serializedEvents = eventStore.Get(create.OrderKey).ToList();

            Assert.AreEqual(1, serializedEvents.Count());

            AddOrderItem addItem = new AddOrderItem(create.OrderKey, GuidKey.Create(Guid.NewGuid(), "Product"), 5);

            commandDispatcher.HandleAsync(Envelope.Create(addItem).AddDelay(TimeSpan.FromMinutes(1)));

            Task <OrderTotalRecalculated> task = eventDispatcher.Handlers.Await <OrderTotalRecalculated>();

            task.Wait();
            Console.WriteLine(task.Result);

            serializedEvents = eventStore.Get(create.OrderKey).ToList();
            Assert.AreEqual(4, serializedEvents.Count());
        }
Esempio n. 2
0
        private async static Task MigrateEventSourcingAsync(string sourceConnectionString, string targetConnectionString)
        {
            Console.WriteLine("Migrate EventSourcing.");

            SchemaOptions <EventSourcingContext> sourceSchema = new SchemaOptions <EventSourcingContext>();
            SchemaOptions <EventSourcingContext> targetSchema = new SchemaOptions <EventSourcingContext>()
            {
                Name = "EventSourcing"
            };

            MigrationWithSchema.SetSchema(sourceSchema);
            MigrationWithSchema.SetSchema(targetSchema);

            using (var source = new EventSourcingContext(new DbContextOptionsBuilder <EventSourcingContext>().UseSqlite(sourceConnectionString).Options, sourceSchema))
                using (var target = new EventSourcingContext(new DbContextOptionsBuilder <EventSourcingContext>().UseSqlServer(targetConnectionString).Options, targetSchema))
                {
                    Console.WriteLine("Migrate schema.");
                    await target.Database.MigrateAsync();

                    target.Database.OpenConnection();
                    await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [EventSourcing].[Event] ON;");

                    Console.WriteLine("Migrate data.");
                    await CopyDbSetAsync(source, target, c => c.Events);

                    Console.WriteLine("Save changes.");
                    await target.SaveChangesAsync();

                    await target.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [EventSourcing].[Event] OFF;");

                    target.Database.CloseConnection();

                    Console.WriteLine("Completed.");
                }
        }
Esempio n. 3
0
 protected CommandHandler(ArchDbContext architectureContext, IDomainNotification notifications, IEventRepository eventRepository, EventSourcingContext eventSourcingContext)
 {
     _architectureContext  = architectureContext;
     _notifications        = notifications;
     _eventRepository      = eventRepository;
     _eventSourcingContext = eventSourcingContext;
 }
Esempio n. 4
0
 public CustomerCommandHandler(
     ArchDbContext architectureContext,
     IDomainNotification notifications,
     IEventRepository eventRepository,
     EventSourcingContext eventSourcingContext) : base(architectureContext, notifications, eventRepository, eventSourcingContext)
 {
     _architectureContext = architectureContext;
 }
Esempio n. 5
0
 private void EventSourcingContext()
 {
     using (var eventSourcing = new EventSourcingContext())
     {
         eventSourcing.Database.EnsureDeleted();
         eventSourcing.Database.EnsureCreated();
         eventSourcing.Database.Migrate();
     }
 }
 public ListCommandHandler(
     ArchDbContext architectureContext,
     EventSourcingContext eventSourcingContext,
     IDomainNotification notifications)
 {
     _architectureContext  = architectureContext;
     _eventSourcingContext = eventSourcingContext;
     _notifications        = notifications;
 }
 public CustomerCommandHandler(
     ArchContext context,
     IDomainNotification notifications,
     EventSourcingContext eventSourcingContext)
     : base(context, eventSourcingContext, notifications)
 {
     _context = context;
     _eventSourcingContext = eventSourcingContext;
 }
Esempio n. 8
0
 public CommandHandlerBase(
     ArchContext context,
     EventSourcingContext eventSourcingContext,
     IDomainNotification notifications)
 {
     _context              = context;
     _notifications        = notifications;
     _eventSourcingContext = eventSourcingContext;
 }
Esempio n. 9
0
 /// <summary>
 /// Save event source object using <see cref="RaraAvis.nCubed.EventSourcing.Core.Mementos.IMemento"/> if available.
 /// </summary>
 /// <param name="eventSourced">A source object.</param>
 /// <param name="correlationId">Related correlation id.</param>
 public void Save(T eventSourced, string correlationId)
 {
     CoreExceptionProcessor.ProcessInfrastructure(() =>
     {
         using (var context = new EventSourcingContext())
         {
             var eventsSet = context.Set <EventData>();
             while (eventSourced.Events.Count() > 0)
             {
                 try
                 {
                     var versionedEvent      = eventSourced.Events.Dequeue();
                     versionedEvent.SourceId = versionedEvent.SourceId == Guid.Empty ? eventSourced.Id : versionedEvent.SourceId;
                     this.messagingRandomRetry.ExecuteAction(() => this.eventBus.Publish(new Envelope <IEvent>(versionedEvent)
                     {
                         CorrelationId = correlationId
                     }));
                     eventsSet.Add(this.Serialize(versionedEvent, correlationId));
                 }
                 catch (Exception ex)
                 {
                     try
                     {
                         this.sqlIncrementalRetry.ExecuteAction(() => { context.SaveChanges(); }); //Save processed store events
                     }
                     catch (Exception exSql)
                     {
                         Exception exSqlOut = null;
                         EventSourcingExceptionProcessor.HandleException(exSql, out exSqlOut);
                         throw exSqlOut;
                     }
                     Exception exOut = null;
                     EventSourcingExceptionProcessor.HandleException(ex, out exOut);
                     throw exOut;
                 }
             }
             try
             {
                 this.sqlIncrementalRetry.ExecuteAction(() => { context.SaveChanges(); });
                 saveMemento(eventSourced);
             }
             catch (Exception ex)
             {
                 Exception exOut = null;
                 EventSourcingExceptionProcessor.HandleException(ex, out exOut);
                 throw exOut;
             }
         }
     });
 }
Esempio n. 10
0
 private void EnsureEventSourcingDatabase(EventSourcingContext eventSourcing = null)
 {
     if (eventSourcing == null)
     {
         using (eventSourcing = eventSourceContextFactory.Create())
         {
             eventSourcing.Database.EnsureCreated();
             eventSourcing.Database.Migrate();
         }
     }
     else
     {
         eventSourcing.Database.EnsureCreated();
         eventSourcing.Database.Migrate();
     }
 }
Esempio n. 11
0
        /// <summary>
        /// Load an entity applying all events, using <see cref="RaraAvis.nCubed.EventSourcing.Core.Mementos.IMemento"/> if available.
        /// </summary>
        /// <param name="entity">Entity to apply events.</param>
        public void Load(T entity)
        {
            CoreExceptionProcessor.ProcessInfrastructure(() =>
            {
                assignMemento(entity);

                using (var context = new EventSourcingContext())
                {
                    var deserialized = context.Set <EventData>()
                                       .Where(x => x.AggregateId == entity.Id && x.AggregateType == sourceType && x.Version > entity.Version)
                                       .OrderBy(x => x.Version)
                                       .AsEnumerable()
                                       .Select(this.Deserialize)
                                       .AsCachedAnyEnumerable();

                    if (deserialized.Any())
                    {
                        entity.ApplyEvents(deserialized);
                    }
                }
            });
        }
Esempio n. 12
0
 public CustomerQueryHandler(ArchDbContext architectureContext, EventSourcingContext eventSourcingContext)
 {
     _architectureContext  = architectureContext;
     _eventSourcingContext = eventSourcingContext;
 }
 public EventRespoitory(EventSourcingContext eventSourcingContext, IUser user)
 {
     _eventSourcingContext = eventSourcingContext;
     _user = user;
 }
 public EventRespoitory(EventSourcingContext eventSourcingContext)
 {
     _eventSourcingContext = eventSourcingContext;
 }
Esempio n. 15
0
 public EventStoreRepository(EventSourcingContext context)
 {
     _context = context;
 }
Esempio n. 16
0
 public EventSourcingQueryHandler(EventSourcingContext eventSourcingContext)
 {
     _eventSourcingContext = eventSourcingContext;
 }