Skip to content

Commit 8423c8b

Browse files
Larger Refactoring of IntegrationEvents
1 parent e41ce96 commit 8423c8b

20 files changed

Lines changed: 262 additions & 68 deletions

src/Services/Basket/Basket.API/Basket.API.csproj

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
1313
</PropertyGroup>
1414

15+
<ItemGroup>
16+
<Compile Include="IntegrationEvents\Events\ProductPriceChangedEvent.cs.txt" />
17+
</ItemGroup>
18+
1519
<ItemGroup>
1620
<Content Update="web.config">
1721
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
@@ -23,16 +27,16 @@
2327

2428
<ItemGroup>
2529
<PackageReference Include="System.Threading" Version="4.3.0" />
26-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
27-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.0" />
28-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.0" />
29-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.0" />
30-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.0" />
31-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.0" />
32-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
33-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.0" />
34-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
35-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.0" />
30+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
31+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
32+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
33+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
34+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
35+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
36+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
37+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
38+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
39+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
3640
<PackageReference Include="StackExchange.Redis" Version="1.1.608" />
3741
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
3842
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.0.1-rc3" />

src/Services/Basket/Basket.API/Events/CatalogPriceChangedHandler.cs renamed to src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedEventHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88

9-
namespace Basket.API.Events
9+
namespace Basket.API.IntegrationEvents.EventHandling
1010
{
11-
public class ProductPriceChangedHandler : IIntegrationEventHandler<ProductPriceChanged>
11+
public class ProductPriceChangedEventHandler : IIntegrationEventHandler<ProductPriceChangedEvent>
1212
{
1313
private readonly IBasketRepository _repository;
14-
public ProductPriceChangedHandler(IBasketRepository repository)
14+
public ProductPriceChangedEventHandler(IBasketRepository repository)
1515
{
1616
_repository = repository;
1717
}
1818

19-
public async Task Handle(ProductPriceChanged @event)
19+
public async Task Handle(ProductPriceChangedEvent @event)
2020
{
2121
var userIds = await _repository.GetUsers();
2222
foreach (var id in userIds)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+

2+
3+
// To implement ProductPriceChangedEvent.cs here

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server;
1616
using Microsoft.eShopOnContainers.Services.Common.Infrastructure;
1717
using Microsoft.eShopOnContainers.Services.Common.Infrastructure.Catalog;
18-
using Basket.API.Events;
18+
using Basket.API.IntegrationEvents.EventHandling;
1919

2020
namespace Microsoft.eShopOnContainers.Services.Basket.API
2121
{
@@ -79,16 +79,16 @@ public void ConfigureServices(IServiceCollection services)
7979
});
8080

8181
services.AddTransient<IBasketRepository, RedisBasketRepository>();
82-
services.AddTransient<IIntegrationEventHandler<ProductPriceChanged>, ProductPriceChangedHandler>();
82+
services.AddTransient<IIntegrationEventHandler<ProductPriceChangedEvent>, ProductPriceChangedEventHandler>();
8383

8484
var serviceProvider = services.BuildServiceProvider();
8585
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
8686
var eventBus = new EventBusRabbitMQ(configuration.EventBusConnection);
8787
services.AddSingleton<IEventBus>(eventBus);
8888

8989

90-
var catalogPriceHandler = serviceProvider.GetService<IIntegrationEventHandler<ProductPriceChanged>>();
91-
eventBus.Subscribe<ProductPriceChanged>(catalogPriceHandler);
90+
var catalogPriceHandler = serviceProvider.GetService<IIntegrationEventHandler<ProductPriceChangedEvent>>();
91+
eventBus.Subscribe<ProductPriceChangedEvent>(catalogPriceHandler);
9292

9393
}
9494

src/Services/Catalog/Catalog.API/Catalog.API.csproj

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,33 @@
2121
<Content Include="Pics\**\*;">
2222
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2323
</Content>
24+
<Compile Include="IntegrationEvents\EventHandling\AnyFutureIntegrationEventHandler.cs.txt" />
25+
<Compile Include="IntegrationEvents\Events\ProductPriceChangedEvent.cs.txt" />
2426
<Content Update="web.config;">
2527
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
2628
</Content>
2729
</ItemGroup>
2830

2931

3032
<ItemGroup>
31-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0" />
32-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.0" />
33-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="1.1.0" />
34-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.0" />
35-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.0" />
36-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.0" />
37-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.0" />
38-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.0" />
39-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.0" />
40-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
41-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.0" />
42-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
43-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.0" />
44-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.0" />
45-
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.0" />
46-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
47-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" />
33+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
34+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
35+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="1.1.1" />
36+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
37+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
38+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
39+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
40+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
41+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
42+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
43+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
44+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
45+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
46+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
47+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
48+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.1" />
49+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
50+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
4851
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
4952
<PackageReference Include="Swashbuckle" Version="6.0.0-beta902" />
5053
</ItemGroup>

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public async Task<IActionResult> Post([FromBody]CatalogItem value)
149149
_context.CatalogItems.Update(item);
150150
await _context.SaveChangesAsync();
151151

152-
var @event = new ProductPriceChanged(item.Id, item.Price, oldPrice);
152+
var @event = new ProductPriceChangedEvent(item.Id, item.Price, oldPrice);
153153
await ProcessEventAsync(@event);
154154
}
155155

@@ -166,14 +166,14 @@ private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
166166
return items;
167167
}
168168

169-
private async Task ProcessEventAsync(IntegrationEventBase @event)
169+
private async Task ProcessEventAsync(IntegrationEvent @event)
170170
{
171171
_eventBus.Publish(@event);
172-
var eventData = new IntegrationEvent(@event);
173-
eventData.TimesSent++;
174-
eventData.State = EventStateEnum.Sent;
172+
var eventLogEntry = new IntegrationEventLogEntry(@event);
173+
eventLogEntry.TimesSent++;
174+
eventLogEntry.State = EventStateEnum.Published;
175175

176-
_context.IntegrationEvents.Add(eventData);
176+
_context.IntegrationEventLog.Add(eventLogEntry);
177177
await _context.SaveChangesAsync();
178178

179179
}

src/Services/Catalog/Catalog.API/Infrastructure/CatalogContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public CatalogContext(DbContextOptions options) : base(options)
1313
public DbSet<CatalogItem> CatalogItems { get; set; }
1414
public DbSet<CatalogBrand> CatalogBrands { get; set; }
1515
public DbSet<CatalogType> CatalogTypes { get; set; }
16-
public DbSet<IntegrationEvent> IntegrationEvents { get; set; }
16+
public DbSet<IntegrationEventLogEntry> IntegrationEventLog { get; set; }
1717

1818
protected override void OnModelCreating(ModelBuilder builder)
1919
{
2020
builder.Entity<CatalogBrand>(ConfigureCatalogBrand);
2121
builder.Entity<CatalogType>(ConfigureCatalogType);
2222
builder.Entity<CatalogItem>(ConfigureCatalogItem);
23-
builder.Entity<IntegrationEvent>(ConfigureIntegrationEvent);
23+
builder.Entity<IntegrationEventLogEntry>(ConfigureIntegrationEventLogEntry);
2424
}
2525

2626
void ConfigureCatalogItem(EntityTypeBuilder<CatalogItem> builder)
@@ -80,9 +80,9 @@ void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
8080
.HasMaxLength(100);
8181
}
8282

83-
void ConfigureIntegrationEvent(EntityTypeBuilder<IntegrationEvent> builder)
83+
void ConfigureIntegrationEventLogEntry(EntityTypeBuilder<IntegrationEventLogEntry> builder)
8484
{
85-
builder.ToTable("IntegrationEvent");
85+
builder.ToTable("IntegrationEventLog");
8686

8787
builder.HasKey(e => e.EventId);
8888

src/Services/Catalog/Catalog.API/Infrastructure/Migrations/20170316012921_RefactoringToIntegrationEventLog.Designer.cs

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace Catalog.API.Infrastructure.Migrations
6+
{
7+
public partial class RefactoringToIntegrationEventLog : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.DropTable(
12+
name: "IntegrationEvent");
13+
14+
migrationBuilder.CreateTable(
15+
name: "IntegrationEventLog",
16+
columns: table => new
17+
{
18+
EventId = table.Column<Guid>(nullable: false),
19+
Content = table.Column<string>(nullable: false),
20+
CreationTime = table.Column<DateTime>(nullable: false),
21+
EventTypeName = table.Column<string>(nullable: false),
22+
State = table.Column<int>(nullable: false),
23+
TimesSent = table.Column<int>(nullable: false)
24+
},
25+
constraints: table =>
26+
{
27+
table.PrimaryKey("PK_IntegrationEventLog", x => x.EventId);
28+
});
29+
}
30+
31+
protected override void Down(MigrationBuilder migrationBuilder)
32+
{
33+
migrationBuilder.DropTable(
34+
name: "IntegrationEventLog");
35+
36+
migrationBuilder.CreateTable(
37+
name: "IntegrationEvent",
38+
columns: table => new
39+
{
40+
EventId = table.Column<Guid>(nullable: false),
41+
Content = table.Column<string>(nullable: false),
42+
CreationTime = table.Column<DateTime>(nullable: false),
43+
EventTypeName = table.Column<string>(maxLength: 200, nullable: false),
44+
State = table.Column<int>(nullable: false),
45+
TimesSent = table.Column<int>(nullable: false)
46+
},
47+
constraints: table =>
48+
{
49+
table.PrimaryKey("PK_IntegrationEvent", x => x.EventId);
50+
});
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)