Archive for .NET tag

Enums – Better syntax, improved performance and TryParse in NET 3.5

October 2010 .NET (, , ) • 5,864 views • 8 responses

Recently I needed to map external data into in-memory objects. In such scenarios the TryParse methods of Int and String are useful but where is Enum.TryParse? TryParse exists in .NET 4.0 but like a lot of people I’m on .NET 3.5.

A quick look at Enum left me scratching my head.

  • Why didn’t enums receive the generic love that collections received in .NET 2.0?
  • Why do I have to pass in typeof(MyEnum) everywhere?
  • Why do I have to the cast results back to MyEnum all the time?
  • Can I write TryParse and still make quick – i.e. without try/catch?

I found myself with a small class, Enum<T> that solved all these. I was surprised when I put it through some benchmarks that also showed the various methods were significantly faster when processing a lot of documents. Even my TryParse was quicker than that in .NET 4.0.

While there is some small memory overhead with the initial class (about 5KB for the first, a few KB per enum after) the performance benefits came as an additional bonus on top of the nicer syntax.

Before (System.Enum)

var getValues = Enum.GetValues(typeof(MyEnumbers)).OfType();
var parse = (MyEnumbers)Enum.Parse(typeof(MyEnumbers), "Seven");
var isDefined = Enum.IsDefined(typeof(MyEnumbers), 3);
var getName = Enum.GetName(typeof(MyEnumbers), MyEnumbers.Eight);
MyEnumbers tryParse;
Enum.TryParse<MyEnumbers>("Zero", out tryParse);

After (Enum<T>)

var getValues = Enum<MyEnumbers>.GetValues();
var parse = Enum<MyEnumbers>.Parse("Seven");
var isDefined = Enum<MyEnumbers>.IsDefined(MyEnumbers.Eight);
var getName = Enum<MyEnumbers>.GetName(MyEnumbers.Eight);
MyEnumbers tryParse;
Enum<MyEnumbers>.TryParse("Zero", out tryParse);

I also added a useful ParseOrNull method that lets you either return null or default using the coalesce so you don’t have to mess around with out parameters, e.g.

MyEnumbers myValue = Enum<MyEnumbers>.ParseOrNull("Nine-teen") ?? MyEnumbers.Zero;

The class

using System;
using System.Collections.Generic;
using System.Linq;

public static class Enum<T> where T : struct {
    private static readonly IEnumerable<T> All = Enum.GetValues(typeof(T)).Cast<T>();
    private static readonly Dictionary<string, T> InsensitiveNames = All.ToDictionary(k => Enum.GetName(typeof(T), k).ToLowerInvariant());
    private static readonly Dictionary<string, T> SensitiveNames = All.ToDictionary(k => Enum.GetName(typeof(T), k));
    private static readonly Dictionary<int, T> Values = All.ToDictionary(k => Convert.ToInt32(k));
    private static readonly Dictionary<T, string> Names = All.ToDictionary(k => k, v => v.ToString());

    public static bool IsDefined(T value) {
        return Names.Keys.Contains(value);
    }

    public static bool IsDefined(string value) {
        return SensitiveNames.Keys.Contains(value);
    }

    public static bool IsDefined(int value) {
        return Values.Keys.Contains(value);
    }

    public static IEnumerable<T> GetValues() {
        return All;
    }

    public static string[] GetNames() {
        return Names.Values.ToArray();
    }

    public static string GetName(T value) {
        string name;
        Names.TryGetValue(value, out name);
        return name;
    }

    public static T Parse(string value) {
        T parsed = default(T);
        if (!SensitiveNames.TryGetValue(value, out parsed))
            throw new ArgumentException("Value is not one of the named constants defined for the enumeration", "value");
        return parsed;
    }

    public static T Parse(string value, bool ignoreCase) {
        if (!ignoreCase)
            return Parse(value);

        T parsed = default(T);
        if (!InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out parsed))
            throw new ArgumentException("Value is not one of the named constants defined for the enumeration", "value");
        return parsed;
    }

    public static bool TryParse(string value, out T returnValue) {
        return SensitiveNames.TryGetValue(value, out returnValue);
    }

    public static bool TryParse(string value, bool ignoreCase, out T returnValue) {
        if (!ignoreCase)
            return TryParse(value, out returnValue);

        return InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out returnValue);
    }

    public static T? ParseOrNull(string value) {
        if (String.IsNullOrEmpty(value))
            return null;

        T foundValue;
        if (InsensitiveNames.TryGetValue(value.ToLowerInvariant(), out foundValue))
            return foundValue;

        return null;
    }

    public static T? CastOrNull(int value) {
        T foundValue;
        if (Values.TryGetValue(value, out foundValue))
            return foundValue;

        return null;
    }
}

Usage notes

  • This class as-is only works for Enum’s backed by an int (the default) although you could modify the class to use longs etc.
  • I doubt very much this class is of much use for flag enums
  • Casting from long can be done using the CastOrNull function instead of just putting (T)
  • GetName is actually much quicker than ToString on the Enum… (e.g. Enum<MyEnumbers>.GetName(a) over a.ToString())
  • IsDefined doesn’t take an object like Enum and instead has three overloads which map to the actual types Enum.IsDefined can deal with and saves runtime lookup
  • Some of the method may not behave exactly like their Enum counterparts in terms of exception messages, nulls etc.

[)amien

Include for LINQ to SQL (and maybe other providers)

May 2010 – February 2011 .NET (, , ) • 8,641 views • 12 responses

It’s quite common that when you issue a query you’re going to want to join some additional tables.

In LINQ this can be a big issue as associations are properties and it’s easy to end up issuing a query every time you hit one. This is referred to as the SELECT N+1 problem and tools like LINQ to SQL Profiler can help you find them.

An example

Consider the following section of C# code that displays a list of blog posts and also wants the author name.

foreach(Post post in db.Posts)
  Console.WriteLine("{0} {1}", post.Title, post.Author.Name);

This code looks innocent enough and will issue a query like “SELECT * FROM [Posts]” but iterating over the posts causes the lazy-loading of the Author property to trigger and each one may well issue a query similar to “SELECT * FROM [Authors] WHERE [AuthorID] = 1″.

In the case of LINQ to SQL it’s not always an extra load as it will check the posts AuthorID foreign key in its internal identity map (cache) to see if it’s already in-memory before issuing a query to the database.

LINQ to SQL’s LoadWith

Most object-relational mappers have a solution for this – Entity Framework’s ObjectQuery has an Include operator (that alas takes a string), and NHibernate has a fetch mechanism. LINQ to SQL has LoadWith which is used like this:

var db = new MyDataContext();
var dlo = new DataLoadOptions();
dlo.LoadWith<Posts>(p => p.Blog);
db.LoadOptions = dlo;

This is a one-time operation for the lifetime of this instance of the data context which can be inflexible and LoadWith has at least one big bug with inheritance issuing multiple joins.

A flexible alternative

This got me thinking and I came up with a useful extension method to provide Include-like facilities on-demand in LINQ to SQL (and potentially other LINQ providers depending on what they support) in .NET 4.0.

public static IEnumerable<T> Include<T, TInclude>(this IQueryable<T> query, Expression<Func<T, TInclude>> sidecar) {
   var elementParameter = sidecar.Parameters.Single();
   var tupleType = typeof(Tuple<T, TInclude>);
   var sidecarSelector =  Expression.Lambda<Func<T, Tuple<T, TInclude>>>(
      Expression.New(tupleType.GetConstructor(new[] { typeof(T), typeof(TInclude) }),
         new Expression[] { elementParameter, sidecar.Body  },
         tupleType.GetProperty("Item1"), tupleType.GetProperty("Item2")), elementParameter);
   return query.Select(sidecarSelector).AsEnumerable().Select(t => t.Item1);
}

To use simply place at the end of your query and specify the property you wish to eager-load, e.g.

var oneInclude = db.Posts.Where(p => p.Published).Include(p => p.Blog));
var multipleIncludes = db.Posts.Where(p => p.Published).Include(p => new { p.Blog, p.Template, p.Blog.Author }));
This code is very untested although it has worked quite well in the few tests I’ve thrown at it. Evaluate it properly before you decide to use it!

How it works

How it works is actually very simple – it projects into a Tuple that contains the original item and all additional loaded elements and then just returns the query back the original item. It is a dynamic version of:

var query = db.Posts.Where(p => p.Published)
   .Select(p => new Tuple(p, p.Blog))
   .AsEnumerable()
   .Select(t => t.Item1);

This is why it has to return IEnumerable<T> and belong at the end (and the use of Tuple is why it is .NET 4.0 only although that should be easy enough to change). Not all LINQ providers will necessarily register the elements with their identity map to prevent SELECT N+1 on lazy-loading but LINQ to SQL does :)

[)amien

Dictionary<T> look-up or create made simpler

August 2009 – August 2010 .NET (, ) • 5,490 views • 9 responses

The design of a Dictionary<T> lends itself well to a caching or identification mechanism and as a result you often see code that looks like this:

private static Dictionary<string,Employee> employees;
…
public static Employee GetByName(string name) {
    Employee employee;
    if (!employees.TryGetValue(name, out employee)) {
        employee = new Employee(whatever);
        employees.Add(name, employee);
    }
    return employee;
}

It’s not that it is particularly difficult but it can be a bit error prone and when you’re doing it over and over. What would be nicer is something that let you do:

public static Employee GetByName(string name) {
    return employees.GetOrAdd(name, () => new Employee(whatever));
}

Here’s an extension method to drop-in to a static class of your choosing that achieves just that.

public static TDictionaryValue GetOrAdd<TKey, TDictionaryValue>(this IDictionary<TKey, TDictionaryValue> dictionary, TKey key, Func<TDictionaryValue> newValue)
{
    TDictionaryValue value;
    if (!dictionary.TryGetValue(key, out value)) {
        value = newValue.Invoke();
        dictionary.Add(key, value);
    }
    return value;
}

[)amien

Changing type, the state pattern and LINQ to SQL

January 2009 – March 2011 .NET (, , , , ) • 3,079 views • 8 responses

A question I see from time-to-time on LINQ to SQL relates to changing an entity’s class.

C# and VB.NET don’t allow a class to change its type at runtime and LINQ to SQL specifically doesn’t provide a mechanism for changing the underlying discriminator for this reason.

Discarding the current object and creating a new one is fraught with issues. What do we do about existing references, unsaved data, established associations and caches?

Start with an example

Consider an abstract Account class with SavingsAccount and CurrentAccount sub-classes. Bank accounts don’t change type once created (in my experience) so that’s good so far.

When we get into processing and validation logic its tempting to create ClosedAccount and OpenAccount classes but what happens during execution when closing an account?

A further consideration is how exactly ClosedAccount and OpenAccount fit into the hierarchy given the single-inheritance limitation of C# and VB.NET.

Enter the State Pattern

The ever-resourceful Gang of Four describe the State Pattern as:

Allow an object to alter its behavior when its internal state changes.
The object will appear to change its class.

Taking the bank accounts example and applying the state pattern gives:

State Pattern class diagram

Account no longer needs to change type at run-time yet is still able to have clients call Validate and process Methods that end up in discrete methods as if inheritance had been used.

To achieve this we have:

  1. Created a State hierarchy that contains the logic we need to change at run-time
  2. Introduced a private member in Account that points to the current state instance
  3. Ensured the Account methods call the current state class via the private member

Because the state member is private and only accessed by Account itself we can happily create and dispose it as the conditions that affect the state change as much as we like without worrying about references to it.

This is best illustrated with code. Here rather than just calling the state’s validation logic there is a combination of core Account validation (balance), state validation (closed) and CheckingAccount validation (transaction limits):

public abstract class Account {
    private AccountState state;

    public virtual Status Validate(ITransaction tx) {
        Status result = state.Validate(tx);
        if (tx.Amount > Balance)
            result.Add(TransactionFailures.InsufficientFunds);
        return result;
    }
}
public class SavingsAccount : Account {
    public override Status Validate(ITransaction tx) {
        Status result = base.Validate(tx);
        if (Transactions.Count > TransactionLimit)
            result.Add(TransactionFailures.TransactionLimitReached);
        return result;
    }
}
public class ClosedAccountState : AccountState {
    public override Status Validate(ITransaction tx) {
        return new Status(TransactionFailures.InvalidSourceAccount);
    }
}

This is less complex than selectively replacing objects within our application at run-time and can bring additional benefits:

Like all guidance, patterns and principles do not blindly follow these guidelines or patterns but consider how it affects and fits with your application. For this particular example it not only solves the problem but helps maintainability – at least at this simple stage. Once Validation becomes sufficiently complex it would likely move out entirely into a new set of orchestrated classes just for that.

With LINQ to SQL (and other mappers)

Moving this example into an object-relational mapper requires two – not unexpected – database-mapped properties.

  1. The inheritance discriminator (Type)
  2. A state indicator (Active)

Sample class diagrams for accounts using state pattern

The only thing we need to ensure is the Account’s state member always refers to either a ClosedAccountState or OpenedAccountState depending upon the Active flag.

Given that LINQ to SQL code-generates the property for Active we could:

  1. Make Active private, wrap it in another property and set the state member when it changes and at initialization
  2. Make the state member a read-only property instead of an instance variable

The second works well here and given that AccountState is itself stateless (perhaps not the best choice of name) we can use a singleton to avoid multiple instances. The state instance variable in the Account class is replaced with:

private AccountState State {
    get {
        if (Active)
            return OpenAccountState.Instance;
        else
            return ClosedAccountState.Instance;
    }
}

The code continues to work and now changing the Active flag results in different behavior.

Best of all we still have the code in separate classes, no switch/case/if statements relating to validation or account types, a clean inheritance hierarchy and no running around trying to invalidate existing references.

Hitting the discriminator directly

There may be times when claims are made that a type has to change – perhaps data was entered incorrectly.

Before delving into the database or providing a tool to flip the underlying discriminator value consider:

  1. Does the new class interpret the same data in a different manner?
    Has a $1,000 credit limit just become a 1,000 transactions per-month limit?
  2. Would the object be valid in the new class?
    Did a ProposedCustomer just become ApprovedCustomer without a policy-enforced credit check?
  3. Are associations still honored?
    Are 300 unshipped orders for a GameProduct still honored for a BookProduct?

If in doubt don’t do it.

An inconsistent database bleeding through your application isn’t good for anyone and will take a lot longer to sort out than setting up a new entity.

[)amien