Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

just thought I'd post this for a code review. How can it be better?

The gist: 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.

On to the code:

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?

share|improve this question
    
I didn't realize there was a code review branch. Thank you. –  Jereme Oct 31 '12 at 17:29
add comment

migrated from stackoverflow.com Nov 27 '12 at 16:18

This question came from our site for professional and enthusiast programmers.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.