public async Task Create(DonorContext db, IIntegrationEventQueue queue, bool commitTransaction = true)
        {
            if (!IsValidated)
            {
                throw new Exception("Please validate model before creation.");
            }

            var donor = await db.Donors.FilterDeletedItems().SingleOrDefaultAsync(x => x.Id == DonorId);

            if (donor == null)
            {
                throw new ArgumentException($"Donor with ID '{DonorId}' does not exist.", nameof(DonorId));
            }

            var transactionSource = new TransactionSource();

            MapToEntity(transactionSource);
            db.TransactionSources.Add(transactionSource);
            MapFromEntity(transactionSource);

            if (commitTransaction)
            {
                await db.SaveChangesAsync();

                Id = transactionSource.Id;
                var integrationEvent = new IntegrationEvent <DonorTransactionSourceEvent>(ServiceNames.DonorService.ToString(), EventNames.AddDonorTransactionSource.ToString(), ToDonorTransactionSourceEvent());
                queue.Post(integrationEvent);
            }
        }
        // TODO: Unit test these methods

        public static IIntegrationEventQueue CopyToOutbox(
            this IIntegrationEventQueue eventQueue, IOutboxService outboxService, TimeSpan validitySpan)
        {
            foreach (var @event in eventQueue)
            {
                outboxService.AddObject(
                    @event.EventId, @event.GetType().ToString(), @event, DateTime.Now + validitySpan);
            }

            return(eventQueue);
        }
        public async Task CreateOrUpdate(CharityContext db, IIntegrationEventQueue queue, IRequestContext requestContext)
        {
            if (!Id.HasValue)
            {
                await Create(db, queue, requestContext);

                return;
            }

            await Update(db, queue, requestContext);
        }
Exemple #4
0
        public AppDbContext(
            DbContextOptions <AppDbContext> options,
            IDomainEventDispatcher dispatcher,
            IIntegrationEventQueue integrationEvents,
            IServiceBusSender <IIntegrationEvent> busSender,
            IOutboxService outboxService) : base(options)
        {
            _dispatcher        = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
            _integrationEvents = integrationEvents ?? throw new ArgumentNullException(nameof(integrationEvents));
            _busSender         = busSender ?? throw new ArgumentNullException(nameof(busSender));

            _outboxService = outboxService ?? throw new ArgumentNullException(nameof(outboxService));
            _outboxService.AddProvider(this);
        }
        public async Task Create(CharityContext db, IIntegrationEventQueue queue, IRequestContext requestContext)
        {
            if (!IsValidated)
            {
                throw new Exception("Please validate model before creation.");
            }

            var donor = new Charity();

            MapToEntity(donor, requestContext);
            db.Charities.Add(donor);
            await db.SaveChangesAsync();

            Id = donor.Id;

            var integrationEvent = new IntegrationEvent <CharityEvent>(ServiceNames.CharityService.ToString(), EventNames.AddCharity.ToString(), ToCharityEvent());

            queue.Post(integrationEvent);
        }
        public async Task Remove(CharityContext db, IIntegrationEventQueue queue)
        {
            if (!Id.HasValue)
            {
                throw new Exception("Please ensure that Id field has been populated before removing.");
            }
            var charity = await db.Charities.FilterDeletedItems().SingleOrDefaultAsync(x => x.Id == Id);

            if (charity == null)
            {
                throw new ArgumentException($"Donor with ID '{Id}' does not exist.", nameof(Id));
            }

            charity.IsDeleted = true;
            MapFromEntity(charity);
            await db.SaveChangesAsync();

            var integrationEvent = new IntegrationEvent <CharityEvent>(ServiceNames.CharityService.ToString(), EventNames.RemoveCharity.ToString(), ToCharityEvent());

            queue.Post(integrationEvent);
        }
        public async Task Remove(DonorContext db, IIntegrationEventQueue queue)
        {
            if (!Id.HasValue)
            {
                throw new Exception("Please ensure that Id field has been populated before removing.");
            }
            var transactionSource = await db.TransactionSources.FilterDeletedItems().SingleOrDefaultAsync(x => x.Id == Id);

            if (transactionSource == null)
            {
                throw new ArgumentException($"Transaction Source with ID '{Id}' does not exist.", nameof(Id));
            }

            transactionSource.IsDeleted = true;
            MapFromEntity(transactionSource);
            await db.SaveChangesAsync();

            var integrationEvent = new IntegrationEvent <DonorTransactionSourceEvent>(ServiceNames.DonorService.ToString(), EventNames.RemoveDonorTransactionSource.ToString(), ToDonorTransactionSourceEvent());

            queue.Post(integrationEvent);
        }
        public static async Task <List <IIntegrationEvent> > PublishToBus(
            this IIntegrationEventQueue eventQueue, IServiceBusSender <IIntegrationEvent> busSender)
        {
            List <IIntegrationEvent> publishedEvents = new List <IIntegrationEvent>();

            foreach (var @event in eventQueue)
            {
                // TODO: Batch these message sends in single awaiter to improve performance
                // TODO: Handle exceptions

                await busSender.SendAsync(@event);

                publishedEvents.Add(@event);
            }

            foreach (var @event in publishedEvents)
            {
                eventQueue.Remove(@event);
            }

            return(publishedEvents);
        }
        public TransactionGeneratorService(IServiceProvider serviceProvider)
        {
            _serviceScope = serviceProvider.CreateScope();
            var loggerFactory = _serviceScope.ServiceProvider.GetServiceOrThrow <ILoggerFactory>();

            _logger = loggerFactory.GetLogger <TransactionGeneratorService>();
            _transactionFeedService = _serviceScope.ServiceProvider.GetService(typeof(IIntegrationEventQueue)) as IIntegrationEventQueue;
            _db        = _serviceScope.ServiceProvider.GetService(typeof(FundContext)) as FundContext;
            _merchants = new Dictionary <Guid, string>()
            {
                { Guid.NewGuid(), "Merchant 1" },
                { Guid.NewGuid(), "Merchant 2" },
                { Guid.NewGuid(), "Merchant 3" },
                { Guid.NewGuid(), "Merchant 4" },
                { Guid.NewGuid(), "Merchant 5" },
                { Guid.NewGuid(), "Merchant 6" },
                { Guid.NewGuid(), "Merchant 7" },
                { Guid.NewGuid(), "Merchant 8" },
                { Guid.NewGuid(), "Merchant 9" },
                { Guid.NewGuid(), "Merchant 10" }
            };
        }
Exemple #10
0
        public async Task Update(CharityContext db, IIntegrationEventQueue queue, IRequestContext requestContext)
        {
            if (!IsValidated)
            {
                throw new Exception("Please validate model before creation.");
            }

            var charity = await db.Charities.FilterDeletedItems().SingleOrDefaultAsync(x => x.Id == Id);

            if (charity == null)
            {
                throw new ArgumentException($"Donor with ID '{Id}' does not exist.", nameof(Id));
            }

            MapToEntity(charity, requestContext);
            await db.SaveChangesAsync();

            MapFromEntity(charity);

            var integrationEvent = new IntegrationEvent <CharityEvent>(ServiceNames.CharityService.ToString(), EventNames.UpdateCharity.ToString(), ToCharityEvent());

            queue.Post(integrationEvent);
        }
 public CharityController(CharityContext db, IIntegrationEventQueue queue, IRequestContext requestContext)
 {
     _db             = db;
     _queue          = queue;
     _requestContext = requestContext;
 }
 public TransactionEventHandler(FundContext fundContext, IIntegrationEventQueue queue)
 {
     _fundContext = fundContext;
     _queue       = queue;
 }
 public ExamplePersonCreatedToIntegrationEventHandler(IIntegrationEventQueue integrationEventQueue)
 {
     _integrationEventQueue =
         integrationEventQueue ?? throw new ArgumentNullException(nameof(integrationEventQueue));
 }