A suggestion for conditional evaluation

Aug 19, 2010 at 3:26 PM
Edited Aug 19, 2010 at 3:28 PM

Hi,

first of all thanks for this great library.

Maybe an "If" would be helpfull.

Old style:

if (_discardRequestsWithoutHandler == IntraAppBusBehaviour.ThrowExceptionIfNoHandlerExists)
{
     Condition.Requires(HasSubscriptionFor<TRequest>());
}
 

New (suggested) style:

Condition.If(_discardRequestsWithoutHandler == IntraAppBusBehaviour.ThrowExceptionIfNoHandlerExists)
              .Then()
                  .Requires(HasSubscriptionFor<TRequest>());

 or more abstract

 Condition.If( evaluation_condition ).Then().Requires(....);

Best regards

Christian 

Coordinator
Aug 19, 2010 at 8:33 PM
Edited Aug 19, 2010 at 8:35 PM

Hi Christian,

Thank you for your interest in CuttingEdge.Conditions and thank you for your suggestion. Conditions is specially developed to make it easy to write pre condition validations in a readable way. I'm not sure however, that that API you suggest is an improvement to this goal. I am very conservative about these types of changes, because I want the API to be simple and understandable as possible. I personally think your 'old style' example is much more readable than the 'suggested style'. In this scenario I probably even wouldn't use Conditions. I would simply write it as follows:

    if (_discardRequestsWithoutHandler == IntraAppBusBehaviour.ThrowExceptionIfNoHandlerExists &&
    !HasSubscriptionFor<TRequest>())
    {
        throw new InvalidOperationException(
            "An expressive message about the error and how to solve it.");
    }

When you want to use Conditions, you can currently write that sample as follows:

Condition.Requires(_discardRequestsWithoutHandler) .Evaluate(_discardRequestsWithoutHandler != IntraAppBusBehaviour.ThrowExceptionIfNoHandlerExists || HasSubscriptionFor<TRequest>());

As I said, I'm conservative about things that make the library more difficult to use. Your examples perhaps even shown that the library might already be unintuitive, because the code you wrote doesn't actually do a thing. The Requires(x) method returns an object that allows validation. The Requires() method itself however, does never throw an exception. It are the methods you can call on this object that allow you to validate. This code for instance, doesn't do a thing:

    Condition.Requires(value);

This code however, throws an exception when value is false:

    Condition.Requires(value).IsTrue();

I hope this helps.