Expressive String Validation with Fluent Validation

The standard approach to write validators with Fluent Validation (FV) is to extend AbstractValidator<T> and set T to the type you want to run validation on. For very simple cases, one could try to create a validator for strings, as I did:

public class StringNotEmptyValidator : AbstractValidator<string>
{
    public StringNotEmptyValidator()
    {
        RuleFor(str => str).NotEmpty();
    }
}

Unfortunately, this does not work, because RuleFor() is designed to work on properties. Thus, FV tries to extract the property name from the RuleFor() expression, which fails.

However, the property name is only required for validation error reporting. By specifying the property name explicitly, FV does not try to extract it from the expression:

public class StringNotEmptyValidator : AbstractValidator<string>
{
    public StringNotEmptyValidator(string name)
    {
        RuleFor(str => str).NotEmpty().OverridePropertyName(name);
    }
}

There need not be any property with the specified name, since all FV does, is put that string in the thrown exceptions. If you don’t care for the string in the exception, you could even hard-code it.

Continue reading

C# Event Relays

Events are a powerful mechanism to propagate changes within an application. Using custom add and remove handlers, you can even decide what happens when clients subscribe and unsubscribe, a feature I used extensively to “forward” subscriptions to other classes.

class Intermediate
{
    private readonly ISomeEventSource _source;

    public event EventHandler IntermediateEvent
    {
        add { _source.SourceEvent += value; }
        remove { _source.SourceEvent -= value; }
    }
}

If you’re working with events extensively, this is something you come across sooner or later. It is a simple mechanism to directly connect the subscriber of IntermediateEvent to the SourceEvent.

As soon as you have slightly more complicated setups, however, this approach becomes arduous. For example, there is no possibility to intercept the event propagation from the source to the client, because the client is connected to the source directly. Thus, you have no chance of modifying event arguments, dropping certain events or performing some custom handling in the Intermediate class.

Continue reading

Mederger Flue Northwest Couloir

On Sunday, February 5th I went on a backcountry snowboarding trip to Mederger Flue near Langwies, Switzerland. It was very cold that day. In the morning, the thermometer showed -27°C outside our accommodation, a small hut above Langwies.

Perfect powder below the Mederger Flue NW couloir. The smile on my face says it all.

Continue reading

Logical Layering Concepts

When we were designing the architectural cornerstones in the project I’m currently working on, we had eventually arrived at the logical view ‌– the developers’ perspective on the software. This lead to some very interesting thoughts and discussions about different layering concepts that I’d like to share in this post.

Note that this article is not about which specific layers you should define in your application. Actually, the three layers in the examples below may be not appropriate for your case in any way. The discussion here is about dependencies between layers and the components in them, so the names of the layers do not matter for the concepts I’d like to talk about.

Continue reading

View-Only Actions in WPF

About a year ago, I answered a question about WPF bindings on stackoverflow.com. The goal was to bind a button to the “close window” action. All answers involved writing some C# code to actually close the window, which is a rather cumbersome approach.

Recently, I was asked about a very similar problem at the project I’m currently working on, so I thought it may be worth to share an elegant solution to this, using only XAML code.

Continue reading

Clean Climbing in Valle Calanca

I like difficult climbs. But I also like adventures. Since combining the two usually leads to dangerous undertakings that’d be better left to the pros, I found that easy climbs with a higher adventure-factor is something I should do more.

Then, on a nice weekend in June 2011, I met some friends for a clean climbing trip to the Valle Calanca, a narrow valley in the italian-speaking part of Grisons, Switzerland. Our goal was to climb the west ridge of Pizzo Della Molera, using only the protection gear we brought along.

Continue reading