Exemplo n.º 1
0
        public void Publish <TCommand>(TCommand command) where TCommand : ICommand
        {
            using (SqlMessageDbContext context = _dbFactory())
            {
                var newCmd = new Command()
                {
                    GeneratedBy       = command.GeneratedBy,
                    GeneratedOn       = command.GeneratedOn,
                    Id                = command.Id,
                    SerializedCommand = JsonConvert.SerializeObject(command),
                    CommandType       = typeof(TCommand).AssemblyQualifiedName,
                    DispatchedOn      = DateTime.UtcNow
                };
                context.Commands.Add(newCmd);
                context.SaveChanges();
            }

            var handler = _serviceProvider.GetRequiredService <ICommandHandler <TCommand> >();

            handler.Handle(command);

            using (SqlMessageDbContext context = _dbFactory())
            {
                var cmd = context.Commands.Find(command.Id);
                cmd.HandledOn = DateTime.UtcNow;
                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public void Publish <TEvent>(TEvent evnt) where TEvent : IEvent
        {
            using (SqlMessageDbContext context = _dbFactory())
            {
                var newEvnt = new Event()
                {
                    GeneratedBy     = evnt.GeneratedBy,
                    GeneratedOn     = evnt.GeneratedOn,
                    Id              = evnt.Id,
                    SourceId        = evnt.SourceId,
                    EventType       = typeof(TEvent).AssemblyQualifiedName,
                    SerializedEvent = JsonConvert.SerializeObject(evnt),
                    DispatchedOn    = DateTime.UtcNow
                };
                context.Events.Add(newEvnt);
                context.SaveChanges();
            }

            var handlers = _serviceProvider.GetServices <IEventHandler <TEvent> >();

            foreach (IEventHandler <TEvent> handler in handlers)
            {
                handler.Handle(evnt);
            }

            using (SqlMessageDbContext context = _dbFactory())
            {
                var handled = context.Events.Find(evnt.Id);
                handled.HandledOn = DateTime.UtcNow;
                context.SaveChanges();
            }
        }