Jake Ginnivan's blog

No More R# Warnings for nHibernate Aliases

I dislike having R# warnings in code bases I work on, and try to leave every class I work on having a green status.

Here is a scenario

var entryAlias = default(Entry);
var entries = session.QueryOver<Entry>(()=>entryAlias);

In the above scenario R# will warn that entryAlias is always null, which is is, but I don’t care, nHibernate is simply getting the type from the expression.

So I have created a simple static class:

/// <summary>
/// Simple helper class to remove r# warnings when using nHibernate aliases
/// </summary>
public static class Alias
{
    public static T For<T>()
    {
        return default(T);
    }
}

And we turn the above code into:

var entryAlias = Alias.For<Entry>();
var entries = session.QueryOver<Entry>(()=>entryAlias);

Which does not warn.

Happy Resharpering

Comments