3
\$\begingroup\$

I needed to publish a message across the ESB (MassTransit) that will return a different type of object depending on how the message is processed. I wanted to implement this in a generic way so I could wire up other services with similar requirements in the same way.

Publish a request

public IGenericResponse<TPayload> PublishRequest<TMessage, TPayload>(TMessage message, Type[] potentialResponseTypes)
 where TMessage : class
{
 IGenericResponse<TPayload> commandResult = null;
 _bus.PublishRequest(message, c =>
 {
 foreach (Type responseType in potentialResponseTypes)
 {
 Type genericHandler = typeof(GenericHandler<,>).MakeGenericType(new[]{ responseType, typeof(TMessage)});
 var handler = new Action<object>(result =>
 {
 commandResult = (IGenericResponse<TPayload>) result;
 });
 Activator.CreateInstance(genericHandler, new object[] {c, handler});
 }
 c.SetTimeout(10.Seconds());
 });
 return commandResult;
}

Generic Handler to wire it up

internal class GenericHandler<TPayload, TMessage>
 where TMessage : class
 where TPayload : class
{
 public GenericHandler(InlineRequestConfigurator<TMessage> configurator, Action<object> handler)
 {
 configurator.Handle<GenericResponse<TPayload>>(handler);
 }
}

Thoughts?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 31, 2012 at 17:04
\$\endgroup\$
1
  • \$\begingroup\$ What type is the c variable you get when you publish a request? Is this your response? If so there's a much simpler solution that needs way less reflection. \$\endgroup\$ Commented Oct 2, 2014 at 20:38

1 Answer 1

2
\$\begingroup\$

It's possible, providing c is your response from the bus, to do a lot of this without needing Reflection. Instead we can use a separate method and the joys of implicit typing to work out the type of TResponse for us.

Proposed solution

public IGenericResponse<TPayload> PublishRequest<TMessage, TPayload>(TMessage message, Type[] potentialResponseTypes)
 where TMessage : class
{
 IGenericResponse<TPayload> commandResult = null;
 _bus.PublishRequest(message, response =>
 {
 foreach (Type responseType in potentialResponseTypes)
 {
 var genericHandler = CreateHandler(response, message);
 }
 c.SetTimeout(10.Seconds());
 });
 return commandResult;
}
private GenericHandler<TResponse, TMessage> CreateHandler<TResponse,TMessage>(TResponse response, TMessage message)
{
 var handler = new Action<object>(result =>
 {
 commandResult = (IGenericResponse<TPayload>) result;
 });
 return new GenericHandler<TResponse, TMessage>(response, handler);
}
answered Oct 2, 2014 at 20:46
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.