Skip to content

Commit

Permalink
Merge pull request #42 from dstantotalsoft/master
Browse files Browse the repository at this point in the history
add cancellationtoken for each async method + some cleaning code
  • Loading branch information
fraliv13 authored Nov 19, 2019
2 parents 9ea5b3b + 1e725c4 commit 4c5efde
Show file tree
Hide file tree
Showing 124 changed files with 557 additions and 585 deletions.
4 changes: 2 additions & 2 deletions NBB.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.757
# Visual Studio Version 16
VisualStudioVersion = 16.0.29509.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7311E32F-C1B0-41C9-B5F1-DE9EBB6ABB55}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NBB.Contracts.Application.Commands;
using NBB.Contracts.ReadModel;
using NBB.Messaging.Abstractions;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Api.Controllers
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public async Task<IActionResult> Get(Guid id)
//var contract = await _contractReadModelRepository.GetFirstOrDefaultAsync(x=> x.ContractId == id, "ContractLines");
var contract = await _contractReadModelQuery
.Include(x=> x.ContractLines)
.SingleOrDefaultAsync(x => x.ContractId == id);
.SingleOrDefaultAsync(x => x.ContractId == id, CancellationToken.None);

if (contract != null)
return Ok(contract);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using NBB.Contracts.Application.Commands;
using NBB.Contracts.Domain.ContractAggregate;
using NBB.Data.Abstractions;
using NBB.Messaging.Abstractions;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Application.CommandHandlers
{
Expand All @@ -14,12 +13,10 @@ public class ContractCommandHandlers :
IRequestHandler<ValidateContract>
{
private readonly IEventSourcedRepository<Contract> _repository;
private readonly IMessageBusPublisher _messageBusPublisher;

public ContractCommandHandlers(IEventSourcedRepository<Contract> repository, IMessageBusPublisher messageBusPublisher)
public ContractCommandHandlers(IEventSourcedRepository<Contract> repository)
{
this._repository = repository;
this._messageBusPublisher = messageBusPublisher;
}

public async Task Handle(CreateContract command, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using NBB.Contracts.Domain.ContractAggregate.DomainEvents;
using NBB.Contracts.ReadModel;
using NBB.Data.Abstractions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Application.DomainEventHandlers
{
Expand All @@ -24,17 +24,17 @@ public ReadModelGenerator(ICrudRepository<ContractReadModel> contractReadModelRe
public async Task Handle(ContractCreated @event, CancellationToken cancellationToken)
{

var c = await _contractReadModelRepository.GetByIdAsync(@event.ContractId);
var c = await _contractReadModelRepository.GetByIdAsync(@event.ContractId, cancellationToken);
if (c == null)
{
await _contractReadModelRepository.AddAsync(new ContractReadModel(@event.ContractId, @event.ClientId, 0));
await _contractReadModelRepository.SaveChangesAsync();
await _contractReadModelRepository.AddAsync(new ContractReadModel(@event.ContractId, @event.ClientId, 0), cancellationToken);
await _contractReadModelRepository.SaveChangesAsync(cancellationToken);
}
}

public async Task Handle(ContractAmountUpdated @event, CancellationToken cancellationToken)
{
var e = await _contractReadModelRepository.GetByIdAsync(@event.ContractId);
var e = await _contractReadModelRepository.GetByIdAsync(@event.ContractId, cancellationToken);

//if(e == null)
// throw new Exception("Could not find entity in readModel");
Expand All @@ -44,14 +44,14 @@ public async Task Handle(ContractAmountUpdated @event, CancellationToken cancell
e.Amount = @event.NewAmount;
e.Version = e.Version + 1;

await _contractReadModelRepository.SaveChangesAsync();
await _contractReadModelRepository.SaveChangesAsync(cancellationToken);

}
}

public async Task Handle(ContractLineAdded @event, CancellationToken cancellationToken)
{
var e = await _contractReadModelRepository.GetByIdAsync(@event.ContractId, nameof(ContractReadModel.ContractLines));
var e = await _contractReadModelRepository.GetByIdAsync(@event.ContractId, cancellationToken,nameof(ContractReadModel.ContractLines));

if (e != null)
{
Expand All @@ -61,14 +61,14 @@ public async Task Handle(ContractLineAdded @event, CancellationToken cancellatio
e.ContractLines.Add(contractLine);
e.Version = e.Version + 1;

await _contractReadModelRepository.SaveChangesAsync();
await _contractReadModelRepository.SaveChangesAsync(cancellationToken);
}
}
}

public async Task Handle(ContractValidated @event, CancellationToken cancellationToken)
{
var contract = await _contractReadModelRepository.GetByIdAsync(@event.ContractId);
var contract = await _contractReadModelRepository.GetByIdAsync(@event.ContractId, cancellationToken);

//if(e == null)
// throw new Exception("Could not find entity in readModel");
Expand All @@ -77,7 +77,7 @@ public async Task Handle(ContractValidated @event, CancellationToken cancellatio
{
contract.IsValidated = true;
contract.Version = contract.Version + 1;
await _contractReadModelRepository.SaveChangesAsync();
await _contractReadModelRepository.SaveChangesAsync(cancellationToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Migrations
{
public class ContractsReadDatabaseMigrator
{
public async Task MigrateDatabaseToLatestVersion(string[] args)
public async Task MigrateDatabaseToLatestVersion(string[] args, CancellationToken cancellationToken = default)
{
var dbContext = new ContractsReadDbContextFactory().CreateDbContext(args);
await dbContext.Database.EnsureCreatedAsync();
await dbContext.Database.EnsureCreatedAsync(cancellationToken);
}

public async Task EnsureDatabaseDeleted(string[] args)
public async Task EnsureDatabaseDeleted(string[] args, CancellationToken cancellationToken = default)
{
var dbContext = new ContractsReadDbContextFactory().CreateDbContext(args);
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureDeletedAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using NBB.EventStore.AdoNet.Migrations;
using NBB.EventStore.AdoNet.Migrations;
using System;

namespace NBB.Contracts.Migrations
{
class Program
{
static void Main(string[] args)
private static void Main(string[] args)
{
var contractsMigrator = new ContractsReadDatabaseMigrator();
contractsMigrator.EnsureDatabaseDeleted(args).Wait();
Expand All @@ -17,4 +17,4 @@ static void Main(string[] args)
Console.WriteLine("EventStore objects re-created");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using NBB.Contracts.ReadModel.Data;
using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.IO;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand All @@ -20,6 +18,9 @@
using NBB.Resiliency;
using Serilog;
using Serilog.Events;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Contracts.Worker
{
Expand Down Expand Up @@ -93,7 +94,7 @@ public static async Task MainAsync(string[] args)
);
});

await builder.RunConsoleAsync();
await builder.RunConsoleAsync(default);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NBB.Invoices.Application.Commands;
using NBB.Invoices.Domain.InvoiceAggregate;
using NBB.Messaging.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Invoices.Api.Controllers
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using NBB.Data.Abstractions;
using NBB.Invoices.Application.Commands;
using NBB.Invoices.Domain.InvoiceAggregate;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Invoices.Application.CommandHandlers
{
Expand All @@ -18,8 +18,8 @@ public InvoiceCommandHandlers(ICrudRepository<Invoice> repository)
public async Task Handle(CreateInvoice command, CancellationToken cancellationToken)
{
var invoice = new Invoice(command.ClientId, command.ContractId, command.Amount);
await _repository.AddAsync(invoice);
await _repository.SaveChangesAsync();
await _repository.AddAsync(invoice, cancellationToken);
await _repository.SaveChangesAsync(cancellationToken);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using NBB.Contracts.PublishedLanguage.IntegrationEvents;
using NBB.Data.Abstractions;
using NBB.Invoices.Domain.InvoiceAggregate;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Invoices.Application.IntegrationEventHandlers
{
Expand All @@ -20,8 +20,8 @@ public ContractIntegrationEventHandlers(ICrudRepository<Invoice> invoiceReposito
public async Task Handle(ContractValidated e, CancellationToken cancellationToken)
{
var invoice = new Invoice(e.ClientId, e.ContractId, e.Amount);
await _invoiceRepository.AddAsync(invoice);
await _invoiceRepository.SaveChangesAsync();
await _invoiceRepository.AddAsync(invoice, cancellationToken);
await _invoiceRepository.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using NBB.Data.Abstractions;
using NBB.Domain.Abstractions;
using NBB.Invoices.Domain.InvoiceAggregate;
using NBB.Payments.PublishedLanguage.IntegrationEvents;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Invoices.Application.IntegrationEventHandlers
{
Expand All @@ -21,12 +20,12 @@ public async Task Handle(PaymentReceived e, CancellationToken cancellationToken)
if (e.InvoiceId == null)
return;

var invoice = await _invoiceRepository.GetByIdAsync(e.InvoiceId.Value);
var invoice = await _invoiceRepository.GetByIdAsync(e.InvoiceId.Value, cancellationToken);
if (invoice != null)
{
invoice.MarkAsPayed(e.PaymentId);

await _invoiceRepository.SaveChangesAsync();
await _invoiceRepository.SaveChangesAsync(cancellationToken);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace NBB.Invoices.Domain.InvoiceAggregate
{
public interface IInvoiceRepository
{
Task<Invoice> GetByIdAsync(Guid id);
Task AddAsync(Invoice invoice);
Task SaveChangesAsync();
Task<Invoice> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
Task AddAsync(Invoice invoice, CancellationToken cancellationToken = default);
Task SaveChangesAsync(CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace NBB.Invoices.Migrations
{
public class InvoicesDatabaseMigrator
{
public async Task MigrateDatabaseToLatestVersion(string[] args)
public async Task MigrateDatabaseToLatestVersion(string[] args, CancellationToken cancellationToken = default)
{
var dbContext = new InvoicesDbContextFactory().CreateDbContext(args);
await dbContext.Database.MigrateAsync();
await dbContext.Database.MigrateAsync(cancellationToken);
}

public async Task EnsureDatabaseDeleted(string[] args)
public async Task EnsureDatabaseDeleted(string[] args, CancellationToken cancellationToken = default)
{
var dbContext = new InvoicesDbContextFactory().CreateDbContext(args);
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureDeletedAsync(cancellationToken);
}
}
}
Loading

0 comments on commit 4c5efde

Please sign in to comment.