Пример #1
0
        public WorkerProcess(IEventConsumer consumer,
                             ISpotCharterCommandRepository source,
                             ISpotCharterUpdateViewRepository destination)
        {
            // Define mapping rule between Command and Query models
            Mapper.Initialize(cfg => {
                cfg.CreateMap <SpotCharterDomain.ValueObjects.FreightRate, string>().ConstructUsing((rate, ctx) => rate?.ToString() ?? null);
                cfg.CreateMap <SpotCharter, SpotCharterView>()
                .AfterMap((src, dest) => { dest.LastUpdate = DateTime.Now; });
            });

            this.source      = source;
            this.destination = destination;

            this.consumer = consumer;

            this.consumer.ReceivedEventHandler += (e) =>
            {
                try
                {
                    dynamic eventPayload = JsonConvert.DeserializeObject(e.Payload);
                    var     spotId       = new SpotCharterId(Guid.Parse(eventPayload.AggregateId.Value.ToString()));

                    // check if last version has been updated already
                    if (!lastVersionsDictionary.Keys.Contains(spotId) || lastVersionsDictionary[spotId] < e.Version)
                    {
                        switch (e.EventName)
                        {
                        case "SpotCharterDeleted":
                            destination.Remove(spotId);
                            Console.WriteLine("Spot {0} removed.", spotId);
                            break;

                        default:
                            var spot = source.Get(spotId);

                            var spotView = Mapper.Map <SpotCharterView>(spot);

                            destination.Save(spotView);
                            lastVersionsDictionary[spotId] = spot.Version;     // put the version from the entity, not from the processing event

                            Console.WriteLine("Spot {0} updated to version {1}", spotId, spot.Version);
                            break;
                        }

                        Console.WriteLine("[{3:HH:mm:ss}] Event {0}\tSpot Id {1} Version {2} processed.", e.EventName, spotId, e.Version, DateTime.Now);
                    }
                    else
                    {
                        Console.WriteLine("SpotId : {0} Version {1} already updated. Skipping.", spotId, e.Version);
                    }
                    consumer.AckReceived(e.Id);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }
            };
        }
        public PostgresSQLRepositoryTests()
        {
            SpotCharterEventSourceRepository concreteRepository = new SpotCharterEventSourceRepository
                                                                      ("SpotCharters", "spot_user", "spot_user", applicationName: "SpotRepositoryEventTest", host: dbConfig.host, port: dbConfig.port);

            repository = concreteRepository;
            eventsDispatchRepository = concreteRepository;
        }
Пример #3
0
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsEnvironment("Development"))
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(developerMode: true);
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();


            if (env.IsDevelopment())
            {
                var messageBroker = new SpotCharterInMemoryEventSourceRepository.InProcessEventDispatcher();
                messageDispatcher = messageBroker;

                commandRepository = new SpotCharterInMemoryEventSourceRepository.SpotCharterInMemoryCommandRepository(messageDispatcher);
            }
            else
            {
                // configuration settings for db and message broker
                var databaseConfig      = Configuration.GetSection("EventSourceDatabase");
                var messageBrokerConfig = Configuration.GetSection("MessageBroker");

                messageDispatcher = new RabbitMQEventDispatcher.RabbitMQEventDispatcher(
                    host: messageBrokerConfig["host"],
                    vhost: messageBrokerConfig["vhost"],
                    port: int.Parse(messageBrokerConfig["port"]),
                    username: messageBrokerConfig["username"],
                    password: messageBrokerConfig["password"],
                    exchangeName: messageBrokerConfig["exchangeName"]);

                commandRepository = new SpotCharterEventSourceRepository(
                    database: databaseConfig["database"],
                    login: databaseConfig["login"],
                    password: databaseConfig["password"],
                    applicationName: databaseConfig["applicationName"],
                    host: databaseConfig["host"],
                    port: int.Parse(databaseConfig["port"]),
                    messageBroker: messageDispatcher
                    );
            }
        }
Пример #4
0
        public void TestCommandQuery()
        {
            var spot          = GetNewSpotCharterInstance();
            var messageBroker = new SpotCharterInMemoryEventSourceRepository.InProcessEventDispatcher();

            var cmdRepository = new SpotCharterInMemoryCommandRepository(messageBroker);
            ISpotCharterCommandRepository commandRepository = cmdRepository;

            SpotCharterViewModel.ISpotCharterQueryRepository queryRepository = new SpotCharterInMemoryViewRepository(messageBroker, cmdRepository);

            commandRepository.Save(spot);

            var spotView = queryRepository.GetBySpotCharterId(spot.Id);

            Assert.NotNull(spotView);
            Assert.Equal(spot.VesselId, spotView.VesselId);
            Assert.Equal(spot.Laycan, spotView.Laycan);
        }
        public SpotCharterInMemoryViewRepository(IEventConsumer consumer, ISpotCharterCommandRepository commandRepository)
        {
            // Define mapping rule between Command and Query models
            Mapper.Initialize(cfg => {
                cfg.CreateMap <SpotCharterDomain.ValueObjects.FreightRate, string>().ConstructUsing((rate, ctx) => rate?.ToString() ?? null);
                cfg.CreateMap <SpotCharter, SpotCharterView>()
                .AfterMap((src, dest) => { dest.LastUpdate = DateTime.Now; });
            });
            this.consumer = consumer;

            if (this.consumer != null)
            {
                this.consumer.ReceivedEventHandler += Consumer_ReceivedEventHandler;
            }

            this.commandRepository = commandRepository;

            if (this.commandRepository == null) // run in a separate process, fill repository with some example data
            {
                this.FillRepositoryWithSampleCharters(10);
            }
        }
Пример #6
0
 public SpotCharterCommandHandler(ISpotCharterCommandRepository repository)
 {
     this._repository = repository;
 }