-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensorLifetimeStrategy.cs
More file actions
83 lines (72 loc) · 2.73 KB
/
SensorLifetimeStrategy.cs
File metadata and controls
83 lines (72 loc) · 2.73 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
using Ninject.Syntax;
namespace bootstrapper.sample
{
using System;
using System.Reflection;
using bbv.Common.Bootstrapper;
using bbv.Common.Bootstrapper.Behavior;
using bbv.Common.Bootstrapper.Configuration;
using bbv.Common.Bootstrapper.Syntax;
using bootstrapper.sample.Heartbeat;
using bootstrapper.sample.Sirius;
using Ninject;
public class SensorLifetimeStrategy : AbstractStrategy<ISensor>
{
private readonly Lazy<IKernel> standardKernel;
public SensorLifetimeStrategy()
{
this.standardKernel =
new Lazy<IKernel>(() =>
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IResolutionRoot>().ToConstant(kernel);
return kernel;
});
}
public override IExtensionResolver<ISensor> CreateExtensionResolver()
{
return this.standardKernel.Value.Get<IExtensionResolver<ISensor>>();
}
protected IKernel Kernel
{
get { return this.standardKernel.Value; }
}
protected override void DefineRunSyntax(ISyntaxBuilder<ISensor> builder)
{
builder
.Begin
.With(() => this.Kernel.Get<ExtensionConfigurationSectionBehavior>())
.Execute(() => GetVphtMessageBus(), (sensor, messagebus) => sensor.MessageBusInitialized(messagebus))
.Execute(() => GetVphtDataBus(), (sensor, databus) => sensor.DataBusInitialized(databus))
.Execute(sensor => sensor.Initialize())
.Execute(sensor => sensor.StartObservation())
.With(() => this.Kernel.Get<IMakeAwareOfHeartbeat>())
.With(() => this.Kernel.Get<IStartHeartbeat>());
}
protected override void DefineShutdownSyntax(ISyntaxBuilder<ISensor> builder)
{
builder
.Execute(sensor => sensor.StopObservation())
.With(() => this.standardKernel.Value.Get<IStopHeartbeat>())
.End
.With(new DisposeExtensionBehavior());
}
protected override void Dispose(bool disposing)
{
if (this.standardKernel.IsValueCreated && !this.Kernel.IsDisposed)
{
this.Kernel.Dispose();
}
base.Dispose(disposing);
}
private IVphtMessageBus GetVphtMessageBus()
{
return this.Kernel.Get<IVphtMessageBus>();
}
private IVhptDataBus GetVphtDataBus()
{
return this.Kernel.Get<IVhptDataBus>();
}
}
}