Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit e3f8ac6

Browse files
author
Ramón Tomás
committed
Refactoring IntegrationEventLog service
1 parent 24bed0a commit e3f8ac6

File tree

4 files changed

+17
-30
lines changed

4 files changed

+17
-30
lines changed

src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
66
using System.Linq;
77
using System.ComponentModel.DataAnnotations.Schema;
8+
using System.Reflection;
89

910
namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
1011
{
@@ -31,9 +32,10 @@ public IntegrationEventLogEntry(IntegrationEvent @event)
3132
public DateTime CreationTime { get; private set; }
3233
public string Content { get; private set; }
3334

34-
public void DeserializeJsonContent(Type type)
35+
public IntegrationEventLogEntry DeserializeJsonContent(Type type)
3536
{
3637
IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent;
38+
return this;
3739
}
3840
}
3941
}

src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,39 @@
88
using System.Collections.Generic;
99
using System.Data.Common;
1010
using System.Linq;
11+
using System.Reflection;
1112
using System.Threading.Tasks;
1213

1314
namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services
1415
{
1516
public class IntegrationEventLogService : IIntegrationEventLogService
1617
{
17-
private readonly IEventBusSubscriptionsManager _subsManager;
1818
private readonly IntegrationEventLogContext _integrationEventLogContext;
1919
private readonly DbConnection _dbConnection;
20+
private readonly List<Type> _eventTypes;
2021

21-
public IntegrationEventLogService(IEventBusSubscriptionsManager subsManager,
22-
DbConnection dbConnection)
22+
public IntegrationEventLogService(DbConnection dbConnection)
2323
{
2424
_dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
25-
_subsManager = subsManager ?? throw new ArgumentNullException(nameof(subsManager));
2625
_integrationEventLogContext = new IntegrationEventLogContext(
2726
new DbContextOptionsBuilder<IntegrationEventLogContext>()
2827
.UseSqlServer(_dbConnection)
2928
.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning))
3029
.Options);
30+
31+
_eventTypes = Assembly.Load(Assembly.GetEntryAssembly().FullName)
32+
.GetTypes()
33+
.Where(t => t.Name.EndsWith(nameof(IntegrationEvent)))
34+
.ToList();
3135
}
3236

3337
public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync()
3438
{
35-
var eventLogsPendingToPublish = await _integrationEventLogContext.IntegrationEventLogs
39+
return await _integrationEventLogContext.IntegrationEventLogs
3640
.Where(e => e.State == EventStateEnum.NotPublished)
3741
.OrderBy(o => o.CreationTime)
38-
.ToListAsync();
39-
40-
eventLogsPendingToPublish.ForEach(evtLog =>
41-
evtLog.DeserializeJsonContent(_subsManager.GetEventTypeByName(evtLog.EventTypeShortName)));
42-
43-
return eventLogsPendingToPublish;
42+
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName)))
43+
.ToListAsync();
4444
}
4545

4646
public Task SaveEventAsync(IntegrationEvent @event, DbTransaction transaction)

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,7 @@ public static IServiceCollection AddSwagger(this IServiceCollection services)
232232
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
233233
{
234234
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
235-
sp =>
236-
{
237-
var busMgr = sp.GetRequiredService<IEventBusSubscriptionsManager>();
238-
return (DbConnection c) => new IntegrationEventLogService(busMgr, c);
239-
});
235+
sp => (DbConnection c) => new IntegrationEventLogService(c));
240236

241237
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
242238

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,7 @@ private void ConfigureEventBus(IApplicationBuilder app)
113113
eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
114114
eventBus.Subscribe<OrderStockRejectedIntegrationEvent, IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>>();
115115
eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
116-
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
117-
eventBus.Subscribe<OrderStartedIntegrationEvent, IIntegrationEventHandler<OrderStartedIntegrationEvent>>();
118-
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>>();
119-
eventBus.Subscribe<OrderStatusChangedToCancelledIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToCancelledIntegrationEvent>>();
120-
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>>();
121-
eventBus.Subscribe<OrderStatusChangedToShippedIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>>();
122-
eventBus.Subscribe<OrderStatusChangedToSubmittedIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>>();
123-
eventBus.Subscribe<OrderStatusChangedToStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>>();
116+
eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
124117
}
125118

126119

@@ -258,11 +251,7 @@ public static IServiceCollection AddCustomIntegrations(this IServiceCollection s
258251
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
259252
services.AddTransient<IIdentityService, IdentityService>();
260253
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
261-
sp =>
262-
{
263-
var busMgr = sp.GetRequiredService<IEventBusSubscriptionsManager>();
264-
return (DbConnection c) => new IntegrationEventLogService(busMgr, c);
265-
});
254+
sp => (DbConnection c) => new IntegrationEventLogService(c));
266255

267256
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
268257

0 commit comments

Comments
 (0)