How to compare ContentReference instances

February 25, 2015

I thought that I would mention a small improvement that was released with CMS 8 the other day. It’s a minor change in regards to comparisons of ContentReference instances, but as this is an area that is used a lot I thought that it would be worth mentioning.

First of all we tried to make the ContentReference class play more nicely with classes in the .NET framework. We therefore implemented both the IComparable<T> and IEquatable<T> interfaces which allows for nicer use with other framework methods. Here we also wanted to make sure that all comparison methods adhered to the framework comparison specification. This included changing the CompareTo method to include the same fields as the equality methods and not throwing exceptions when comparing with null.

We also added a ContentReferenceComparer class that can be passed in to methods requiring an IComparer<T> instance. This class has three static properties that gives you access to different types of comparisons, similar to how different StringComparer types are exposed.

  • Default – Compares ID, Version and Provider. This mirrors how methods for comparison on the ContentReference class works.
  • IgnoreVersion – Compares ID and Provider.
  • Complete – Compares all properties (ID, Version, Provider and GetPublishedOrLatest)

Finally we added a new overload to the Equals method on ContentReference for doing a version agnostic comparison. This new overload is meant to replace the CompareToIgnoreVersion method which developers new to EPiServer often find confusing, with it really being an “Equals” method rather than a “Compare” method. If you are a long time EPiServer developer you can relax, as we won’t remove or obsolete the old anytime soon since it’s used pretty much everywhere. We will however stop using this method in documentation and example code going forward.

Advertisements
%d bloggers like this: