Skip to content

Commit

Permalink
Removed switch-case from ProcessBatchJobConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
shahabganji committed Jul 20, 2020
1 parent f4c8e59 commit 48c6aa3
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
namespace SampleBatch.Components.Consumers
{
using System;
using System.Threading.Tasks;
using Contracts;
using Contracts.Enums;
using MassTransit;
using MassTransit.Courier;
using MassTransit.Courier.Contracts;
Expand Down
4 changes: 1 addition & 3 deletions src/SampleBatch.Components/SampleBatchDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.EntityFrameworkCore;
using SampleBatch.Components.StateMachines;
using System;
using System.Collections.Generic;
using System.Text;


namespace SampleBatch.Components
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
{
using System;
using System.Collections.Generic;
using Ardalis.SmartEnum;
using System.Linq;
using Common;
using Contracts.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;


class BatchStateEntityConfiguration :
Expand All @@ -24,7 +23,7 @@ public void Configure(EntityTypeBuilder<BatchState> builder)
builder.Property(c => c.CurrentState).IsRequired();

builder.Property(p => p.Action)
.HasConversion(v => v.Value, i => BatchActionEnum.FromValue(i));
.HasConversion(v => v.Value, i => BatchActionEnum.List().FirstOrDefault(e => e.Value == i));


builder.Property(c => c.UnprocessedOrderIds)
Expand All @@ -36,4 +35,4 @@ public void Configure(EntityTypeBuilder<BatchState> builder)
.Metadata.SetValueComparer(new JsonValueComparer<Dictionary<Guid, Guid>>());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace SampleBatch.Components.StateMachines
{
using System.Linq;
using Contracts.Enums;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;


class JobStateEntityConfiguration :
Expand All @@ -20,7 +20,7 @@ public void Configure(EntityTypeBuilder<JobState> builder)
builder.Property(c => c.CurrentState).IsRequired();

builder.Property(p => p.Action)
.HasConversion(v => v.Value, i => BatchActionEnum.FromValue(i));
.HasConversion(v => v.Value, i => BatchActionEnum.List().FirstOrDefault(e => e.Value == i));
}
}
}
}
31 changes: 27 additions & 4 deletions src/SampleBatch.Contracts/Enums/BatchActionEnum.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace SampleBatch.Contracts.Enums
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Ardalis.SmartEnum;
using Converter;
using Internal;
using MassTransit;
Expand All @@ -12,16 +12,39 @@


[JsonConverter(typeof(BatchActionEnumConverter))]
public abstract class BatchActionEnum : SmartEnum<BatchActionEnum>
public abstract class BatchActionEnum
{
public static readonly BatchActionEnum CancelOrders = new CancelOrdersEnum();
public static readonly BatchActionEnum SuspendOrders = new SuspendOrdersEnum();

public int Value { get; private set; }
public string Name { get; private set; }

public static IEnumerable<BatchActionEnum> List()
{
yield return CancelOrders;
yield return SuspendOrders;
}

protected BatchActionEnum(int value, string name)
: base(name, value)
{
Value = value;
Name = name;
}

public abstract Task<RoutingSlip> SetupRoutingSlip(ConsumeContext<ProcessBatchJob> context, Func<RoutingSlipBuilder,Task> commonAction);
public async Task<RoutingSlip> SetupRoutingSlip(ConsumeContext<ProcessBatchJob> context, Func<RoutingSlipBuilder, Task> commonAction)
{
var builder = new RoutingSlipBuilder(NewId.NextGuid());

await SetupRoutingSlip(builder, context);

await commonAction?.Invoke(builder);

return builder.Build();

}

protected abstract Task SetupRoutingSlip(RoutingSlipBuilder builder, ConsumeContext<ProcessBatchJob> context);

}
}
9 changes: 1 addition & 8 deletions src/SampleBatch.Contracts/Enums/Internal/CancelOrdersEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ public CancelOrdersEnum()
{
}

public override async Task<RoutingSlip> SetupRoutingSlip( ConsumeContext<ProcessBatchJob> context ,
Func<RoutingSlipBuilder,Task> commonAction)
protected override async Task SetupRoutingSlip( RoutingSlipBuilder builder, ConsumeContext<ProcessBatchJob> context)
{
var builder = new RoutingSlipBuilder(NewId.NextGuid());

builder.AddActivity(
"CancelOrder",
new Uri("queue:cancel-order_execute"),
Expand All @@ -39,10 +36,6 @@ await builder.AddSubscription(
context.Message.BatchId,
context.Message.OrderId
}));

await commonAction?.Invoke(builder);

return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ public SuspendOrdersEnum()
{
}

public override async Task<RoutingSlip> SetupRoutingSlip( ConsumeContext<ProcessBatchJob> context ,
Func<RoutingSlipBuilder,Task> commonAction)
protected override async Task SetupRoutingSlip(RoutingSlipBuilder builder, ConsumeContext<ProcessBatchJob> context)
{
var builder = new RoutingSlipBuilder(NewId.NextGuid());

builder.AddActivity(
"SuspendOrder",
new Uri("queue:suspend-order_execute"),
Expand All @@ -35,10 +32,6 @@ await builder.AddSubscription(
context.Message.BatchId,
context.Message.OrderId
}));

await commonAction?.Invoke(builder);

return builder.Build();
}
}
}
1 change: 0 additions & 1 deletion src/SampleBatch.Contracts/SampleBatch.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.SmartEnum" Version="1.0.11" />
<PackageReference Include="MassTransit" Version="7.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 48c6aa3

Please sign in to comment.