forked from hibernating-rhinos/rhino-esb
/
DhtClientSagaFacility.cs
70 lines (63 loc) · 2.11 KB
/
DhtClientSagaFacility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
namespace Rhino.ServiceBus.DistributedHashTableIntegration
{
using System.Linq;
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.Facilities;
using DistributedHashTable;
using Exceptions;
using Internal;
using Messages;
using Sagas;
public class DhtClientSagaFacility : AbstractFacility
{
private void Kernel_OnComponentRegistered(string key, IHandler handler)
{
var model = handler.ComponentModel;
var list = model.Implementation.GetInterfaces()
.Where(x => x.IsGenericType &&
x.IsGenericTypeDefinition == false &&
x.GetGenericTypeDefinition() == typeof(ISaga<>))
.ToList();
if (list.Count == 0)
return;
if (list.Count > 1)
{
throw new InvalidUsageException(model.Implementation +
" implements more than one ISaga<T>, this is not permitted");
}
var sagaType = list[0];
var sagaStateType = sagaType.GetGenericArguments()[0];
if (typeof(Orchestrates<MergeSagaState>).IsAssignableFrom(model.Implementation))
{
Kernel.AddComponent(
"SagaPersister<" + model.Implementation + ">",
typeof(ISagaPersister<>)
.MakeGenericType(model.Implementation),
typeof(DistributedHashTableSagaPersister<,>)
.MakeGenericType(model.Implementation, sagaStateType)
);
}
else if (typeof(SupportsOptimisticConcurrency).IsAssignableFrom(model.Implementation))
{
Kernel.AddComponent(
"SagaPersister<" + model.Implementation + ">",
typeof(ISagaPersister<>)
.MakeGenericType(model.Implementation),
typeof(OptimisticDistributedHashTableSagaPersister<,>)
.MakeGenericType(model.Implementation, sagaStateType)
);
}
else
{
throw new InvalidUsageException(
"When using DHT for saga state, you must implement either SupportsOptimisticConcurrency or Orchestrates<MergeSagaState>");
}
}
protected override void Init()
{
Kernel.ComponentRegistered += Kernel_OnComponentRegistered;
Kernel.AddComponent("dht", typeof(IDistributedHashTableClient), typeof(DistributedHashTableClient));
}
}
}