Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

Comparers

RSS
Modified on 2010/01/02 13:55 by olmo Categorized as SignumUtilities

LambdaComparer

In many .Net and SF API a IComparer or IEqualityComparer is taken as a parameter of the constructor of a data structure or a method.

Implementing a IComparer nowadays looks like too ceremonial, too Javaish... lambda comparer makes it simpler.

public class LambdaComparer<T, S> : IComparer<T>, IEqualityComparer<T>, IComparer, IEqualityComparer
{
   public LambdaComparer(Func<T, S> func){..}
   
   (...)
{

Example:

List<Person> people = (...);
people.Sort(new LambdaComparer<Person, string>(p=>p.Name)); //Not as short as OrderBy, but better than writing an IComparer by hand 

LambdaComparer now also implements IComparer and IEqualityComparer

ReferenceEqualityComparer

By default the default implementation of Equals and GetHashCode for all .Net uses the object referenceb, but when you override both of the methods you get the default implementation lost.

i.e: IdentifiableEntity for example overrides it to use Type and Id for convenience when writing business logic.

ReferenceEqualityComparer allows you to change back this behavior in whatever data structure or algorithm that takes an IEqualityComparer.

public class ReferenceEqualityComparer<T> : IEqualityComparer<T>, IEqualityComparer where T : class
{
   public static ReferenceEqualityComparer<T> Default {get; }
}

Example:

var memoryGraph = new DirectedGraph<Modifiable>(ReferenceEqualityComparer<Modifiable>.Default);
Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.