Tom 07:01, 16 November 2009

Just a quick update on my previous posting following one of the comments made by den Ben

 

just out of curiosity... why wouldn't you
dispatcher.Add<SaveDocumentRequest>(s =>
{
s.Document = document;
s.UserID = userID;
});

 

He's absolutely right. Using a Func forced me to (mis)use the params keyword as you are not allowed  to use the above syntax with the old code:

public virtual IDispatcher Add<TRequestType>(params Func<TRequestType, object>[] funcs)
    where TRequestType : Request, new()
{
    TRequestType request = new TRequestType();
    foreach (var func in funcs)
    {
        func.Invoke(request);
    }
    AddRequest(request, false);
    return this;
}

I actually had noticed the code not using the return value by the func but disregarded it as I was a bit too happy with the "elegance" of my inital solution.

But I think (and I think you'll agree) that using an Action is much cleaner here:

public virtual IDispatcher Add<TRequestType>(Action<TRequestType> action)
    where TRequestType : Request, new()
{
    TRequestType request = new TRequestType();
    action(request);
    AddRequest(request, false);
    return this;
}

 

Thanks den Ben!

Tuesday, November 17, 2009 12:53:32 PM (W. Europe Standard Time, UTC+01:00)
glad to have pointed it out

it does indeed look even better using an Action ;)

and btw... Davy is coworker of mine :)
Comments are closed.