private static void Main(string[] args) { ObjectContainer.SetContainer(new AutofacContainerObject()); var applictionInitializer = new EventHandleProvider(); applictionInitializer.Initialize(Assembly.GetExecutingAssembly()); var commandInitializer = new CommandHandleProvider(); commandInitializer.Initialize(Assembly.GetExecutingAssembly()); ObjectContainer.RegisterInstance(applictionInitializer); ObjectContainer.RegisterInstance(commandInitializer); var dbConnection = new MySqlConnection(_mysqlConnection); var persistence = new SnapshotPersistence(dbConnection); var binarySerializer = new DefaultBinarySerializer(); var snapshotRepository = new SnapshotRepository(persistence, new SnapshotFactory(binarySerializer)); var eventPersistence = new EventStorePersistence(dbConnection); var eventFactory = new EventStreamFactory(binarySerializer); var eventStore = new EventStore(eventPersistence, eventFactory); IRepository repository = new EventSouringRepository(eventStore, snapshotRepository); var comamndHandler = ObjectContainer.Resolve<CommandHandleProvider>(); var changePasswordCommand = new ChangePasswordCommand("90ca0d59-65e6-403b-82c5-8df967cc8e22", "2222222", "11111"); var commandContext = new CommandContext(repository); var commandHanldeAction = comamndHandler.GetInternalCommandHandle(typeof(ChangePasswordCommand)); commandHanldeAction(commandContext, changePasswordCommand); var aggregateRoots = commandContext.AggregateRoots; IList<IEvent> unCommitEvents = null; foreach (var item in aggregateRoots) { unCommitEvents = item.Value.Commit(); } var aggregateRoot = aggregateRoots.FirstOrDefault().Value; eventStore.AppendToStream(aggregateRoot.AggregateRootId, new EventStream(aggregateRoot.Version, unCommitEvents)); snapshotRepository.Create(aggregateRoot); Console.WriteLine("改方法执行完毕..."); }
public DefaultCommandProssor( IEventStore eventStore, IRepository repository, CommandHandleProvider commandHandleProvider, IEventPublisher eventPublisher, ISnapshotStorage snapshotStorage, IBinarySerializer binarySerializer) { _eventStore = eventStore; _repository = repository; _commandHandleProvider = commandHandleProvider; _eventPublisher = eventPublisher; _binarySerializer = binarySerializer; _snapshotStorage = snapshotStorage; }
private static void Main(string[] args) { var containerBuilder = new AutofacContainerBuilder(); var binarySerializer = new DefaultBinarySerializer(); var eventHandleInitializer = new EventHandleProvider(); eventHandleInitializer.Initialize(Assembly.GetExecutingAssembly(), Assembly.Load("Seven.Tests")); var commandInitializer = new CommandHandleProvider(); commandInitializer.Initialize(Assembly.Load("Seven.Tests")); var messageTypeProvider = new MessageTypeProvider(); messageTypeProvider.Initialize(Assembly.GetExecutingAssembly(), Assembly.Load("Seven.Tests")); containerBuilder.RegisterInstance(eventHandleInitializer); containerBuilder.RegisterInstance(commandInitializer); containerBuilder.RegisterInstance(messageTypeProvider); var mysqlEventStore = new MySqlEventStore(_mysqlConnection); var snapshotStorage = new MysqlSnapshotStorage(_mysqlConnection); var aggregateRootStorage = new MysqlAggregateRootStorage(_mysqlConnection); var aggregateRootMemory = new AggregateRootMemoryCache(); var nonEventSouringRepository = new NonEventSouringRepository(aggregateRootStorage, binarySerializer); var eventSouringRepository = new EventSouringRepository(mysqlEventStore, snapshotStorage, binarySerializer, aggregateRootMemory); var endPoint = new RemoteEndpoint("127.0.0.1", "/", "guest", "guest", 5672); var exChangeName = typeof(CreateUserCommand).Assembly.GetName().Name; ; var responseRoutingKey = MessageUtils.CurrentResponseRoutingKey; var channelPools = new CommunicateChannelFactoryPool(endPoint); containerBuilder.RegisterInstance(channelPools); containerBuilder.RegisterInterface<IBinarySerializer, DefaultBinarySerializer>(); DependencyResolver.SetResolver(new DefaultDependencyResolver(containerBuilder.Build())); var requestChannelPools = new RequestChannelPools(); var commandTopicProvider = new UserTopicProvider(); var eventTopicProvider = new UserEEventTopicProvider(); var eventPublisher = new EventPublisher(requestChannelPools, eventTopicProvider); var commandProssor = new DefaultCommandProssor(mysqlEventStore, eventSouringRepository, commandInitializer, eventPublisher, snapshotStorage, binarySerializer); var messageHandler = new MessageRequestHandler(commandProssor); for (var i = 0; i < 5; i++) { var routingKey = string.Format("{0}_{1}_{2}", exChangeName, "command", i); var consumerContext = new ConsumerContext(exChangeName, routingKey, routingKey, responseRoutingKey, false, true); var consumer = new PushMessageConsumer(channelPools, binarySerializer, consumerContext, messageHandler); consumer.Start(); Console.WriteLine("Started."); } for (var i = 0; i < 5; i++) { var routingKey = string.Format("{0}_{1}_{2}", exChangeName, "event", i); var consumerContext = new ConsumerContext(exChangeName, routingKey, routingKey, responseRoutingKey, false, true); var consumer = new PushMessageConsumer(channelPools, binarySerializer, consumerContext, messageHandler); consumer.Start(); Console.WriteLine("Started."); } Console.WriteLine("begin to consumer the message."); Console.ReadLine(); }