From 1d377f7808b83a0c8f314de8f27e979eb4d92300 Mon Sep 17 00:00:00 2001 From: Rafsanul Hasan Date: Sun, 9 Sep 2018 05:18:08 +0600 Subject: [PATCH] Code re-factorings and formatting in IntegrationEventLogEF project --- .../IntegrationEventLogEF/EventStateEnum.cs | 18 ++-- .../IntegrationEventLogContext.cs | 59 ++++++------ .../IntegrationEventLogEntry.cs | 48 +++++----- .../Services/IIntegrationEventLogService.cs | 20 ++-- .../Services/IntegrationEventLogService.cs | 96 ++++++++++--------- .../Utilities/ResilientTransaction.cs | 60 ++++++------ 6 files changed, 146 insertions(+), 155 deletions(-) diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/EventStateEnum.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/EventStateEnum.cs index 3efb78e74..3763724d3 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/EventStateEnum.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/EventStateEnum.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF +namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF { - public enum EventStateEnum - { - NotPublished = 0, - Published = 1, - PublishedFailed = 2 - } + public enum EventStateEnum + { + NotPublished = 0, + Published = 1, + PublishedFailed = 2 + } } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogContext.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogContext.cs index de8754e03..ed55c4b20 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogContext.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogContext.cs @@ -1,48 +1,45 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; -using System; -using System.Collections.Generic; -using System.Text; namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF { - public class IntegrationEventLogContext : DbContext - { - public IntegrationEventLogContext(DbContextOptions options) : base(options) - { - } + public class IntegrationEventLogContext : DbContext + { + public IntegrationEventLogContext(DbContextOptions options) : base(options) + { + } - public DbSet IntegrationEventLogs { get; set; } + public DbSet IntegrationEventLogs { get; set; } - protected override void OnModelCreating(ModelBuilder builder) - { - builder.Entity(ConfigureIntegrationEventLogEntry); - } + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity(ConfigureIntegrationEventLogEntry); + } - void ConfigureIntegrationEventLogEntry(EntityTypeBuilder builder) - { - builder.ToTable("IntegrationEventLog"); + void ConfigureIntegrationEventLogEntry(EntityTypeBuilder builder) + { + builder.ToTable("IntegrationEventLog"); - builder.HasKey(e => e.EventId); + builder.HasKey(e => e.EventId); - builder.Property(e => e.EventId) - .IsRequired(); + builder.Property(e => e.EventId) + .IsRequired(); - builder.Property(e => e.Content) - .IsRequired(); + builder.Property(e => e.Content) + .IsRequired(); - builder.Property(e => e.CreationTime) - .IsRequired(); + builder.Property(e => e.CreationTime) + .IsRequired(); - builder.Property(e => e.State) - .IsRequired(); + builder.Property(e => e.State) + .IsRequired(); - builder.Property(e => e.TimesSent) - .IsRequired(); + builder.Property(e => e.TimesSent) + .IsRequired(); - builder.Property(e => e.EventTypeName) - .IsRequired(); + builder.Property(e => e.EventTypeName) + .IsRequired(); - } - } + } + } } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs index 3cab9e500..9af23149e 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs @@ -1,28 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Newtonsoft.Json; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; +using DateTime = System.DateTime; +using Guid = System.Guid; +using JsonConvert = Newtonsoft.Json.JsonConvert; namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF { - public class IntegrationEventLogEntry - { - private IntegrationEventLogEntry() { } - public IntegrationEventLogEntry(IntegrationEvent @event) - { - EventId = @event.Id; - CreationTime = @event.CreationDate; - EventTypeName = @event.GetType().FullName; - Content = JsonConvert.SerializeObject(@event); - State = EventStateEnum.NotPublished; - TimesSent = 0; - } - public Guid EventId { get; private set; } - public string EventTypeName { get; private set; } - public EventStateEnum State { get; set; } - public int TimesSent { get; set; } - public DateTime CreationTime { get; private set; } - public string Content { get; private set; } - } + using IntegrationEvent = EventBus.Events.IntegrationEvent; + + public class IntegrationEventLogEntry + { + private IntegrationEventLogEntry() { } + public IntegrationEventLogEntry(IntegrationEvent @event) + { + EventId = @event.Id; + CreationTime = @event.CreationDate; + EventTypeName = @event.GetType().FullName; + Content = JsonConvert.SerializeObject(@event); + State = EventStateEnum.NotPublished; + TimesSent = 0; + } + public Guid EventId { get; private set; } + public string EventTypeName { get; private set; } + public EventStateEnum State { get; set; } + public int TimesSent { get; set; } + public DateTime CreationTime { get; private set; } + public string Content { get; private set; } + } } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IIntegrationEventLogService.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IIntegrationEventLogService.cs index ed1f74616..c7b001d83 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IIntegrationEventLogService.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IIntegrationEventLogService.cs @@ -1,15 +1,13 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Linq; -using System.Threading.Tasks; +using DbTransaction = System.Data.Common.DbTransaction; +using Task = System.Threading.Tasks.Task; namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services { - public interface IIntegrationEventLogService - { - Task SaveEventAsync(IntegrationEvent @event, DbTransaction transaction); - Task MarkEventAsPublishedAsync(IntegrationEvent @event); - } + using IntegrationEvent = EventBus.Events.IntegrationEvent; + + public interface IIntegrationEventLogService + { + Task SaveEventAsync(IntegrationEvent @event, DbTransaction transaction); + Task MarkEventAsPublishedAsync(IntegrationEvent @event); + } } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs index a12309482..f94fcb238 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs @@ -1,52 +1,54 @@ using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Diagnostics; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using System; -using System.Data.Common; -using System.Linq; -using System.Threading.Tasks; +using static System.Linq.Enumerable; +using ArgumentNullException = System.ArgumentNullException; +using DbConnection = System.Data.Common.DbConnection; +using DbTransaction = System.Data.Common.DbTransaction; +using RelationalEventId = Microsoft.EntityFrameworkCore.Diagnostics.RelationalEventId; +using Task = System.Threading.Tasks.Task; namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services { - public class IntegrationEventLogService : IIntegrationEventLogService - { - private readonly IntegrationEventLogContext _integrationEventLogContext; - private readonly DbConnection _dbConnection; - - public IntegrationEventLogService(DbConnection dbConnection) - { - _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); - _integrationEventLogContext = new IntegrationEventLogContext( - new DbContextOptionsBuilder() - .UseSqlServer(_dbConnection) - .ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)) - .Options); - } - - public Task SaveEventAsync(IntegrationEvent @event, DbTransaction transaction) - { - if (transaction == null) - { - throw new ArgumentNullException(nameof(transaction), $"A {typeof(DbTransaction).FullName} is required as a pre-requisite to save the event."); - } - - var eventLogEntry = new IntegrationEventLogEntry(@event); - - _integrationEventLogContext.Database.UseTransaction(transaction); - _integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry); - - return _integrationEventLogContext.SaveChangesAsync(); - } - - public Task MarkEventAsPublishedAsync(IntegrationEvent @event) - { - var eventLogEntry = _integrationEventLogContext.IntegrationEventLogs.Single(ie => ie.EventId == @event.Id); - eventLogEntry.TimesSent++; - eventLogEntry.State = EventStateEnum.Published; - - _integrationEventLogContext.IntegrationEventLogs.Update(eventLogEntry); - - return _integrationEventLogContext.SaveChangesAsync(); - } - } + using IntegrationEvent = EventBus.Events.IntegrationEvent; + + public class IntegrationEventLogService : IIntegrationEventLogService + { + private readonly IntegrationEventLogContext _integrationEventLogContext; + private readonly DbConnection _dbConnection; + + public IntegrationEventLogService(DbConnection dbConnection) + { + _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); + _integrationEventLogContext = new IntegrationEventLogContext( + new DbContextOptionsBuilder() + .UseSqlServer(_dbConnection) + .ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)) + .Options); + } + + public Task SaveEventAsync(IntegrationEvent @event, DbTransaction transaction) + { + if (transaction == null) + { + throw new ArgumentNullException(nameof(transaction), $"A {typeof(DbTransaction).FullName} is required as a pre-requisite to save the event."); + } + + var eventLogEntry = new IntegrationEventLogEntry(@event); + + _integrationEventLogContext.Database.UseTransaction(transaction); + _integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry); + + return _integrationEventLogContext.SaveChangesAsync(); + } + + public Task MarkEventAsPublishedAsync(IntegrationEvent @event) + { + var eventLogEntry = _integrationEventLogContext.IntegrationEventLogs.Single(ie => ie.EventId == @event.Id); + eventLogEntry.TimesSent++; + eventLogEntry.State = EventStateEnum.Published; + + _integrationEventLogContext.IntegrationEventLogs.Update(eventLogEntry); + + return _integrationEventLogContext.SaveChangesAsync(); + } + } } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Utilities/ResilientTransaction.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Utilities/ResilientTransaction.cs index f8227882b..0170770ee 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Utilities/ResilientTransaction.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Utilities/ResilientTransaction.cs @@ -1,37 +1,35 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; -using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Text; -using System.Threading.Tasks; +using static Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions; +using ArgumentNullException = System.ArgumentNullException; +using DbContext = Microsoft.EntityFrameworkCore.DbContext; +using Task = System.Threading.Tasks.Task; + namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities { - public class ResilientTransaction - { - private DbContext _context; - private ResilientTransaction(DbContext context) => - _context = context ?? throw new ArgumentNullException(nameof(context)); + using FuncOfTask = System.Func; + + public class ResilientTransaction + { + private DbContext _context; + private ResilientTransaction(DbContext context) => + _context = context ?? throw new ArgumentNullException(nameof(context)); - public static ResilientTransaction New (DbContext context) => - new ResilientTransaction(context); + public static ResilientTransaction New(DbContext context) => + new ResilientTransaction(context); - public async Task ExecuteAsync(Func action) - { - //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction(): - //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency - var strategy = _context.Database.CreateExecutionStrategy(); - await strategy.ExecuteAsync(async () => - { - using (var transaction = _context.Database.BeginTransaction()) - { - await action(); - transaction.Commit(); - } - }); - } - } + public async Task ExecuteAsync(FuncOfTask action) + { + //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction(): + //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency + var strategy = _context.Database.CreateExecutionStrategy(); + await strategy.ExecuteAsync(async () => + { + using (var transaction = _context.Database.BeginTransaction()) + { + await action(); + transaction.Commit(); + } + }); + } + } }