-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed switch-case from ProcessBatchJobConsumer
- Loading branch information
1 parent
d01be29
commit 2e692b1
Showing
18 changed files
with
219 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace SampleBatch.Contracts.Enums | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Converter; | ||
using Internal; | ||
using MassTransit; | ||
using MassTransit.Courier; | ||
using MassTransit.Courier.Contracts; | ||
using Newtonsoft.Json; | ||
|
||
|
||
[JsonConverter(typeof(BatchActionEnumConverter))] | ||
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) | ||
{ | ||
Value = value; | ||
Name = name; | ||
} | ||
|
||
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); | ||
|
||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/SampleBatch.Contracts/Enums/Converter/BatchActionEnumConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace SampleBatch.Contracts.Enums.Converter | ||
{ | ||
using System; | ||
using Internal; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
|
||
public class BaseSpecifiedConcreteClassConverter : DefaultContractResolver | ||
{ | ||
protected override JsonConverter ResolveContractConverter(Type objectType) | ||
{ | ||
if (typeof(BatchActionEnum).IsAssignableFrom(objectType) && !objectType.IsAbstract) | ||
return null; // pretend TableSortRuleConvert is not specified (thus avoiding a stack overflow) | ||
return base.ResolveContractConverter(objectType); | ||
} | ||
} | ||
|
||
public class BatchActionEnumConverter : JsonConverter | ||
{ | ||
static readonly JsonSerializerSettings SpecifiedSubclassConversion = new JsonSerializerSettings() { ContractResolver = new BaseSpecifiedConcreteClassConverter() }; | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return (objectType == typeof(BatchActionEnum)); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
JObject jo = JObject.Load(reader); | ||
switch (jo["value"].Value<int>()) | ||
{ | ||
case 1: | ||
return JsonConvert.DeserializeObject<CancelOrdersEnum>(jo.ToString(), SpecifiedSubclassConversion); | ||
case 2: | ||
return JsonConvert.DeserializeObject<SuspendOrdersEnum>(jo.ToString(), SpecifiedSubclassConversion); | ||
default: | ||
throw new Exception(); | ||
} | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override bool CanWrite { | ||
get { return false; } | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); // won't be called because CanWrite returns false | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/SampleBatch.Contracts/Enums/Internal/CancelOrdersEnum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace SampleBatch.Contracts.Enums.Internal | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
using MassTransit; | ||
using MassTransit.Courier; | ||
using MassTransit.Courier.Contracts; | ||
|
||
|
||
class CancelOrdersEnum : BatchActionEnum | ||
{ | ||
public CancelOrdersEnum() | ||
: base(1, "Cancel Orders") | ||
{ | ||
} | ||
|
||
protected override async Task SetupRoutingSlip( RoutingSlipBuilder builder, ConsumeContext<ProcessBatchJob> context) | ||
{ | ||
builder.AddActivity( | ||
"CancelOrder", | ||
new Uri("queue:cancel-order_execute"), | ||
new | ||
{ | ||
context.Message.OrderId, | ||
Reason = "Product discontinued" | ||
}); | ||
|
||
await builder.AddSubscription( | ||
context.SourceAddress, | ||
RoutingSlipEvents.ActivityFaulted, | ||
RoutingSlipEventContents.None, | ||
"CancelOrder", | ||
x => x.Send<BatchJobFailed>(new | ||
{ | ||
context.Message.BatchJobId, | ||
context.Message.BatchId, | ||
context.Message.OrderId | ||
})); | ||
} | ||
} | ||
} |
Oops, something went wrong.