Sending Messages

Sending messages to trigger message handlers is simple.

First you have to create a custom instance inheriting from IMessage. Next call the Add(IMessage) method of IExecutionModule using the message as parameter. Adding messages to the module is thread safe. You can do this from anywhere in your application.

There a several ways to obtain an instance of IExecutionModule. A fresh new instance is returned at the end of the bootstrapping process. You an use the instance right away.

public static void UseExecutionModule()
{
  // Initialize execution module.
  IExecutionModule execModule = Module.Configure()
                           .UseStructureMap()
                           .Build();

  // create a custom message and fill it with data.
  var msg = new CreateReportMessage { Title = "Report Title" };

  // add the message into the execution module.
  execModule.Add(msg);
}

However, probably the more common way is to have the DI container automatically inject the instance into your custom classes. During initialization the library registers the execution module into the target container. This means all you have to do is to configure your own classes and acquire instances like you normally would. Here is an example using the default StructureMap container.

public static void Main()
{
  Module.Configure()
             .UseStructureMap()
             .Build();
 
  // configure custom manager in DI container.
  ObjectFactory.Container.Configure(x => x.For<ICustomManager>().Use<CustomManager>());

  // request a new instance of ICustomManager and call target method.
  var customManager = ObjectFactory.Container.GetInstance<ICustomManager>();
  customManager.DoSomething();
}

public class CustomManager : ICustomManager
{
  private readonly IExecutionModule _execModule;

  public CustomManager(IExecutionModule execModule)
  {
    // notice: the execution module dependency is automatically
    // injected in the constructor
    // store it as private field for later use.
    _execModule = execModule;
  }

  public void DoSomething()
  {
    // use the injected interface
    _execModule.Add(new SomeMessage());
  }
}

Last edited Jul 15, 2012 at 10:29 AM by domo42, version 6

Comments

No comments yet.