示例#1
0
        public DbContext()
        {
            var conn   = InjectContainer.GetInstance <IEventDBConnectionStringProvider>().ConnectionString;
            var client = new MongoClient(conn);

            _db = client.GetDatabase("LibraryEventStorage");
        }
示例#2
0
        public void Send <T>(T command) where T : ICommand
        {
            var handler = InjectContainer.GetInstance <ICommandHandler <T> >();

            if (handler != null)
            {
                try
                {
                    handler.Execute(command);
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    handler.Dispose();
                }
            }
            else
            {
                //Cannot find command handler for type
                throw new InvalidOperationException(string.Format("无法找到类型的命令处理程序 '{0}'", typeof(T).FullName));
            }
        }
示例#3
0
 public QueryService()
 {
     Reader       = InjectContainer.GetInstance <IConfigurationReader>();
     DataAccessor = InjectContainer.GetInstance <IQueryDataAccessor>(new Dictionary <string, object>()
     {
         { "connectionString", Reader.ConnectionString }
     });
 }
示例#4
0
        private static void LogError(Events.DomainEvent domainEvent, System.Exception exception)
        {
            var errorLogCommand = new AddErrorLogCommand();

            errorLogCommand.ErrorObject = domainEvent;
            errorLogCommand.Exception   = exception;

            InjectContainer.GetInstance <ICommandBus>().Send(errorLogCommand);
        }
        public CommandHandlerBase()
        {
            this.Reader       = InjectContainer.GetInstance <IConfigurationReader>();
            this.DataAccessor = InjectContainer.GetInstance <IDomainDataAccessor>(new Dictionary <string, object>()
            {
                { "connectionString", Reader.ConnectionString }
            });

            this.ExecuteDateTime = Utility.GetCurrentDateTime();
        }
示例#6
0
        public void RegisterAndStart(string libraryName)
        {
            var connectionString = InjectContainer.GetInstance <IEventDBConnectionStringProvider>().ConnectionString;

            Console.WriteLine($"Handler starting...");
            Console.WriteLine($"Event DB Connection String: {connectionString}");

            RegisterAndStartCommandHandlers(libraryName);
            RegisterAndStartEventHandlers(libraryName);

            Console.WriteLine($"Handler started.");
        }
示例#7
0
        public void SelfRegister()
        {
            var serviceDiscovery = InjectContainer.GetInstance <IServiceDiscovery>();

            serviceDiscovery.RegisterService(new Infrastructure.Operation.Core.Models.Service
            {
                Port        = 5000,
                ServiceName = "IdentityService",
                Tag         = "Microservice API"
            });

            Console.WriteLine("Register to consul successfully.");
        }
        public void Subscribe <T>(T domainEvent) where T : DomainEvent
        {
            this.channel.QueueDeclare(queue: domainEvent.EventKey,
                                      durable: true,
                                      exclusive: false,
                                      autoDelete: false,
                                      arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);

            IEventHandler <T> instance = InjectContainer.GetInstance <IEventHandler <T> >();

            if (instance == null)
            {
                return;
            }

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);

                var cmd = JsonConvert.DeserializeObject <T>(message);
                Console.WriteLine("[x] Receive New Event: {0}", domainEvent.EventKey);
                Console.WriteLine("[x] Event Parameters: {0}", message);

                try
                {
                    //执行命令操作
                    instance.Handle(cmd);
                    //tracker.Finish(cmd.CommandUniqueId, cmd.EventKey);
                }
                catch (Exception ex)
                {
                    //tracker.Error(cmd.CommandUniqueId, cmd.EventKey, "100001", ex.Message);
                }

                Console.WriteLine("[x] Event Handler Completed");


                channel.BasicAck(ea.DeliveryTag, false);
            };

            channel.BasicConsume(queue: domainEvent.EventKey, autoAck: false, consumer: consumer);
        }
示例#9
0
        public void RegisterAndStartCommandHandlers(string libraryName)
        {
            var assembly = Assembly.Load(libraryName);

            var allCommands = assembly.GetExportedTypes().Where(p => p.GetInterface("ICommand") != null);

            foreach (var command in allCommands)
            {
                var register       = new RabbitMQCommandSubscriber(InjectContainer.GetInstance <IRabbitMQUrlProvider>());
                var registerMethod = register.GetType().GetMethod("Subscribe");

                var cmd = Activator.CreateInstance(command);
                Console.WriteLine($"Found command {command.FullName}.");
                registerMethod.MakeGenericMethod(command).Invoke(register, new object[1] {
                    cmd
                });
            }
        }
示例#10
0
        public void RegisterAndStartEventHandlers(string libraryName)
        {
            var assembly = Assembly.Load(libraryName);

            var allEvents = assembly.GetExportedTypes().Where(p => p.GetInterface("IDomainEvent") != null);

            foreach (var @event in allEvents)
            {
                var register = new RabbitMQEventSubscriber(InjectContainer.GetInstance <IRabbitMQUrlProvider>(), InjectContainer.GetInstance <ICommandTracker>());

                if (register != null)
                {
                    var registerMethod = register.GetType().GetMethod("Subscribe");

                    var cmd = Activator.CreateInstance(@event);
                    Console.WriteLine($"Found event {@event.FullName}.");
                    registerMethod.MakeGenericMethod(@event).Invoke(register, new object[1] {
                        cmd
                    });
                }
            }
        }
        public void Subscribe <T>(T command) where T : ICommand
        {
            this.channel.QueueDeclare(queue: command.CommandKey,
                                      durable: true,
                                      exclusive: false,
                                      autoDelete: false,
                                      arguments: null);

            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

            var consumer = new EventingBasicConsumer(channel);

            ICommandHandler <T> instance = InjectContainer.GetInstance <ICommandHandler <T> >();

            if (instance == null)
            {
                Console.WriteLine($"The command handler for {typeof(T).FullName} is not prepared.");
            }

            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);

                var cmd = JsonConvert.DeserializeObject <T>(message);
                Console.WriteLine("[x] Receive New Task: {0}", cmd.CommandKey);
                Console.WriteLine("[x] Task Parameters: {0}", message);

                //执行命令操作
                instance.Execute(cmd);

                Console.WriteLine("[x] Task Completed");

                channel.BasicAck(ea.DeliveryTag, false);
            };

            channel.BasicConsume(queue: command.CommandKey, autoAck: false, consumer: consumer);
        }
 public RentalRecordsController()
 {
     _commandPublisher = InjectContainer.GetInstance <ICommandPublisher>();
     _reportDatabase   = InjectContainer.GetInstance <IRentalReportDataAccessor>();
 }
示例#13
0
 public IdentityController()
 {
     _dataAccessor   = InjectContainer.GetInstance <IIdentityReportDataAccessor>();
     _passwordHasher = InjectContainer.GetInstance <IPasswordHasher>();
 }
示例#14
0
        public void Save(AggregateRoot aggregate, Guid commandUniqueId)
        {
            using (var connection = new SqlConnection(InjectContainer.GetInstance <IEventDBConnectionStringProvider>().ConnectionString))
            {
                connection.Open();

                using (var tran = connection.BeginTransaction())
                {
                    var uncommittedChanges = aggregate.GetUncommittedChanges();
                    var currentIndex       = 0;

                    try
                    {
                        var version = aggregate.Version;

                        foreach (var @event in uncommittedChanges)
                        {
                            version++;
                            @event.Version         = version;
                            @event.CommandUniqueId = commandUniqueId;

                            var sql     = "insert into Events(AggregateRootId, Version, [EventName], [Content], OccurredOn,AssemblyName) values(@id, @version,@eventName,@content,@occurredOn,@assemblyName)";
                            var command = connection.CreateCommand();
                            command.CommandText = sql;
                            command.CommandType = CommandType.Text;
                            command.Transaction = tran;

                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@id", SqlDbType = SqlDbType.UniqueIdentifier, Value = @event.AggregateId
                            });
                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@version", SqlDbType = SqlDbType.Int, Value = @event.Version
                            });
                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@eventName", SqlDbType = SqlDbType.NVarChar, Value = @event.GetType().FullName
                            });
                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@content", SqlDbType = SqlDbType.NVarChar, Value = JsonConvert.SerializeObject(@event)
                            });
                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@occurredOn", SqlDbType = SqlDbType.DateTime2, Value = @event.OccurredOn
                            });
                            command.Parameters.Add(new SqlParameter {
                                ParameterName = "@assemblyName", SqlDbType = SqlDbType.NVarChar, Value = @event.GetType().Assembly.GetName().Name
                            });

                            command.ExecuteNonQuery();

                            currentIndex++;
                        }

                        foreach (var @event in uncommittedChanges)
                        {
                            var desEvent = Converter.ChangeTo(@event, @event.GetType());
                            _eventPublisher.Publish(desEvent);
                        }

                        tran.Commit();
                    }
                    catch
                    {
                    }
                }
            }
        }
示例#15
0
 public SQLServerEventStorage()
 {
     _eventPublisher = InjectContainer.GetInstance <IEventPublisher>();
 }
示例#16
0
 private static ILogHelper GetLogHelper()
 {
     return(InjectContainer.GetInstance <ILogHelper>());
 }
示例#17
0
 private static IStringEncrypter GetStringEncrypter()
 {
     return(InjectContainer.GetInstance <IStringEncrypter>());
 }
示例#18
0
 public MongoDBEventStorage()
 {
     _dbContext      = new DbContext();
     _eventPublisher = InjectContainer.GetInstance <IEventPublisher>();
 }
示例#19
0
 public BooksController()
 {
     _commandPublisher = InjectContainer.GetInstance <ICommandPublisher>();
     _reportDatabase   = InjectContainer.GetInstance <IInventoryReportDataAccessor>();
 }
示例#20
0
 public CommandLogsController()
 {
     _logger = InjectContainer.GetInstance <ILogger>();
 }