Home .NET Dependencies implementation in the Apache Ignite.NETservice

Dependencies implementation in the Apache Ignite.NETservice

by admin

Developing various applications that use the popular library Castle Windsor For implementing dependencies and Apache Ignite.NET as the "key" that opens the door to cloud computing, I ran into a small inconvenience : I had no way to implement a dependency in the service running through the so-called Service Grid.
The reason why this happens is quite trivial. Apache Ignite.NET serializes the service, sends it to one of the available servers, where it is deserialized and started. Since this process has no concept of Castle Windsor in any way, we get what we get.
To solve this problem, we need to create a plugin for Apache Ignite.NET that gets the container responsible for implementing dependencies and allow the service to refer to it, to retrieve this or that object.
First, let’s introduce an additional abstraction layer for the container that provides dependency implementations, so that we can easily change its implementation to another one in the future :

public interface IContainer{T Resolve<T> ();}

public class DependencyInjectionContainer : IContainer{protected IKernel Kernel { get; set; }public DependencyInjectionContainer(IKernel kernel){Kernel = kernel;}public T Resolve<T> (){return Kernel.Resolve<T> ();}}

public class DependencyInjectionInstaller : IWindsorInstaller{public void Install(IWindsorContainer container, IConfigurationStore store){container.Register(Component.For<IContainer> ().ImplementedBy<DependencyInjectionContainer> ());}}

To create a plugin, we need to create 3 classes: the class responsible for configuring the plugin, the plugin provider and the plugin itself.

public class DependencyInjectionPlugin{public IContainer Container { get; set; }public T Resolve<T> (){return Container.Resolve<T> ();}}

[PluginProviderType(typeof(DependencyInjectionPluginProvider))]public class DependencyInjectionPluginConfiguration : IPluginConfiguration{public void WriteBinary(IBinaryRawWriter writer){// No-op}public int? PluginConfigurationClosureFactoryId { get; } = null; // No Java part}

public class DependencyInjectionPluginProvider : IPluginProvider<DependencyInjectionPluginConfiguration>{public string Name { get; } = "DependencyInjection";public string Copyright { get; } = "MIT";protected DependencyInjectionPlugin DependencyInjectionPlugin { get; set; }public T GetPlugin<T> () where T : class{return DependencyInjectionPlugin as T;}public void Start(IPluginContext<DependencyInjectionPluginConfiguration> context){DependencyInjectionPlugin = new DependencyInjectionPlugin();}public void Stop(bool cancel){}public void OnIgniteStart(){}public void OnIgniteStop(bool cancel){}}

Great, all that’s left is to load the plugin into Apache Ignite.NET.

public class IgniteInstaller : IWindsorInstaller{public void Install(IWindsorContainer container, IConfigurationStore store){container.Register(Component.For<IIgnite> ().UsingFactoryMethod(() => Ignition.Start(new IgniteConfiguration{PluginConfigurations = new[] {new DependencyInjectionPluginConfiguration()}})));}}

When we start the application, we initialize the dependency implementation container as usual, but now we are ready to hand it over to the plugin we just wrote :

var Done = new ManualResetEventSlim(false);// Build Windsor containerusing (var container = new WindsorContainer()){// Install DI abstraction layercontainer.Install(new DependencyInjectionInstaller());// Install cluster abstraction layercontainer.Install(new IgniteInstaller());// Attach DI container to cluster plugincontainer.Resolve<IIgnite> ().GetPlugin<DependencyInjectionPlugin> ("DependencyInjection").Container = container.Resolve<IContainer> ();// WaitDone.Wait();}

Congratulations, if you have read to the end, you can get any dependency from your container inside the service. It looks like this :

public class ClientConnectionService : IClientConnectionService, IService{private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();[InstanceResource] private IIgnite Ignite { get; set; }public void Init(IServiceContext context){Logger.Debug("Initialized");}public void Execute(IServiceContext context){var plugin = Ignite.GetPlugin<DependencyInjectionPlugin> ("DependencyInjection");var whatever = plugin.Resolve<IWhatever> ();whatever.DoSomething();}public void Cancel(IServiceContext context){Logger.Debug("Canceled");}}

You may also like