Dependancy Injection With RemObjects SDK

RemObjects SDK is a comprehensive remoting framework which I’ve used in a number of projects over the years. It has support for a number of platforms, including .Net and Delphi among others, and provides a comprehensive feature set from encryption to load balancing.

Injection Points

There are a few points we could use to do dependancy injection when using RemObjects; I have highlighted a couple below

Overriding Activate

Each service implemented in RemObjects inherits from RemObjects.SDK.Server.Service which has a method called Activate. This is called before a call is pushed through a service in order to carry out any initialization required.

using System.Collections.Generic;

namespace TestService
{
  using System;
  using RemObjects.SDK;
  using RemObjects.SDK.Types;
  using RemObjects.SDK.Server;
  using RemObjects.SDK.Server.ClassFactories;

  [RemObjects.SDK.Server.ClassFactories.StandardClassFactory()]
  [RemObjects.SDK.Server.Service(Name = "TestService", InvokerClass = typeof(TestService_Invoker), ActivatorClass = typeof(TestService_Activator))]
  public class TestService : RemObjects.SDK.Server.Service, ITestService
  {
    private System.ComponentModel.Container components = null;
    private ILogger logger;
    public PingService() :
      base()
    {
      this.InitializeComponent();
    }
    private void InitializeComponent()
    {
    }
    protected override void Dispose(bool aDisposing)
    {
      if (aDisposing)
      {
        if ((this.components != null))
        {
          this.components.Dispose();
        }
      }
      base.Dispose(aDisposing);
    }
    public override void Activate(Guid aClientID)
    {
      base.Activate(aClientID);
      //inject here...
      logger = App.Get<ILogger>();
    }
    public override void Deactivate(Guid aClientID)
    {
      base.Deactivate(aClientID);
      //deinit here...
    }
    public virtual bool DoSomething(string msg)
    {
      try
      {
        //service implementation
      }
      catch (Exception ex)
      {
        logger.Log(LogLevel.ERROR, "Error in DoSomething: " + ex.Message);
        return false;
      }
    }
  }
}

Service Activator

In order to instantiate a service, RemObjects uses generated activator classes. This is the perfect point at which to inject whatever you need to as this is where the instance of the service is first created.

[RemObjects.SDK.Activator()]
  [System.Reflection.ObfuscationAttribute(Exclude = true, ApplyToMembers = false)]
  public class TestService_Activator : RemObjects.SDK.Server.ServiceActivator
  {
    public TestService_Activator() :
      base()
    {
    }
    public override RemObjects.SDK.IROService CreateInstance()
    {
      //call to our service locator
      return App.Get<PingService>();
    }
  }

The big downside to this is that this code gets generated every time you edit the service in the Service Builder application.

The App object above simply provides us with access to our chosen method of handling dependency resolution. In this case it would be wrapping access to the Ninject kernel configured for the application.

Decisions, Decisions

My preference is to do the injection in the Service Activator as this allows you to clearly define what dependencies a service has by listing them as part of its constructor.

If instead you were to override the Activation method, it becomes less clear what objects the service depends on as the details are lost in a method call, rather than being part of the class declaration.