Saturday, December 01, 2007

For everyone that was at my DevTeach presentation thank you for showing up and listening to me. I had my adrenaline flowing and ended up finishing a little to early, if only I could code like that day to day!

If you have svn installed, you can download my presentation from the command line with the following command:

svn checkout http://stevenrockartspresentations.googlecode.com/svn/trunk/ devteachgenericspresentation

I still have to do one more check-in to add my reduce implementation and to clean up the code. I am also going to upload a zipped version of the code to the downloads section located here: http://code.google.com/p/stevenrockartspresentations/downloads/list

 

Here is a list of resources from the presentation:

- Combining Generics, Specifications and Functional Programming Part 1

- Combining Generics, Specifications and Functional Programming Part 2

- Combining Generics, Specifications and Functional Programming Part 3

- Combining Generics, Specifications and Functional Programming Part 4

- Follow up to Generics Presentation Feedback

- Reduce in C#

Saturday, December 01, 2007 5:12:27 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

I got a couple of emails with great feedback from my last couple of posts. I think I could probably turn this presentation into 2 different presentations but it is curing my blogging writers block so i'll post the responses here for everyone's benefit.

Shane was very quick to point out that the method signature in Customer is kind of misleading:

public bool IsAnActiveCustomer(ISpecification<Customer> activeCustomerSpecification)

We could actually pass any specification that implements the ISpecification interface into this function. We can refactor a little to make it a little more clear to someone consuming the Customer class:

public bool IsAnActiveCustomer()
{
    return this.Matches(new ActiveCustomerSpecification());
}
 
public bool Matches(ISpecification<Customer> specification)
{
    return specification.IsSatisfiedBy(this);
}

Using this approach we supply consumers of our class with a default for an active customer and allow them to specify their own specification for what constitutes an active customer.

One thing that I should have mentioned in my post is that Filter and Map are included in the List class in .NET 2.0. Filter == FindAll and Map==ConvertAll, here is the code from Reflector:

public List<T> FindAll(Predicate<T> match)
{
    if (match == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    List<T> list = new List<T>();
    for (int i = 0; i < this._size; i++)
    {
        if (match(this._items[i]))
        {
           list.Add(this._items[i]);
        }
    }
    return list;
}
 
public List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
{
    if (converter == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.converter);
    }
    List<TOutput> list = new List<TOutput>(this._size);
    for (int i = 0; i < this._size; i++)
    {
        list._items[i] = converter(this._items[i]);
    }
    list._size = this._size;
    return list;
}
Saturday, December 01, 2007 3:39:40 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

At my DevTeach presentation I showed how you can do Map and Filter from Functional programming right now in C# 2.0. In mathematics Reduce is "the process of manipulating a series of equations or matrices into a desired 'simpler' format". In C# you can think of it as taking a list and reducing it down to one value. 

Here is my implementation of Reduce in C# 2.0:

[Test]
public void ShouldReduceListOfIntegersToTotal()
{
    IList<int> numbers = new List<int>();
    numbers.Add(1);
    numbers.Add(2);
    numbers.Add(3);
    numbers.Add(4);
 
    RecordBook<int> recordBook = new RecordBook<int>(numbers);
 
    Assert.AreEqual(10, recordBook.Reduce<int, int>(numbers, Add));
}
 
[Test]
public void ShouldPrintIntegersAsOneString()
{
    IList<int> numbers = new List<int>();
    numbers.Add(1);
    numbers.Add(2);
    numbers.Add(3);
    numbers.Add(4);
 
    RecordBook<int> recordBook = new RecordBook<int>(numbers);
 
    Assert.AreEqual("1234", recordBook.Reduce<int, string>(numbers, BuildStringFromIntegers));
}
 
private string BuildStringFromIntegers(int number, string test)
{
    return test + number;
}
 
private int Add(int number1, int number2)
{
    return number1 + number2;
}

and the implementation of the tests:

public delegate TResult Accumulator<TSource, TResult>(TSource x, TResult y);
 
public TResult Reduce<TSource, TResult>(IList<TSource> source, Accumulator<TSource, TResult> accumulator)
{
    TResult result = default(TResult);
 
    foreach (TSource item in source)
        result = accumulator(item, result);
 
    return result;
}

The first test calculates the sum of adding all the integers in the list together using the add function. The second test takes an integer and concatenates it to a string producing a string of all the integers in the list.

The Reduce function introduces the default keyword that you can leverage with generics. This will give you the default value for value types (0 for integers, 0.0 for decimals, null for string).

Saturday, December 01, 2007 1:21:02 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 
Thursday, November 22, 2007

What would you do if your client asked you to find all suppliers in a list that are located in Alberta? We could create a new Supplier record book but by now you should be seeing the pattern. Whenever the type differs, think about creating a generic class that can extract the essence of the algorithm without worrying about the type. Lets create a generic RecordBook class that can filter by any specification:

public class RecordBook<T>
{
    private IList<T> list;
 
    public RecordBook(IList<T> list)
    {
        this.list = list;
    }
 
    public IEnumerable<T> Filter(ISpecification<T> specification)
    {
        foreach (T item in list)
        {
            if (specification.IsSatisfiedBy(item))
                yield return item;
        }
    }
}

Our Filter method is actually a C# implementation of one of the big 3 in functional programming...Filter!

Our client is happy with our work and we as developers are satisfied that we can respond to change quickly and filter by and type and specification our client can dream up. Our client then throws us a curveball, they want to see all the customers in the list formatted with their last name before their first name. Lets drive out what this would look like with a test:

[Test]
public void ShouldFormatCustomerNameWithLastNameBeforeFirstName()
{
    IList<Customer> recordBook = new List<Customer>();
    recordBook.Add(new Customer(new DateTime(2007, 04, 29), "Steven", "Rockarts"));
 
    RecordBook<Customer> customerRecordBook = new RecordBook<Customer>(recordBook);
 
    IList<string> formattedResult =
        new List<string>(customerRecordBook.Map<Customer, string>(recordBook, ReverseFullName));
 
    Assert.AreEqual("Rockarts Steven", formattedResult[0]);       
}

private string ReverseFullName(Customer customer)
{
    return string.Format("{0} {1}", customer.LastName, customer.FirstName);
}

There is a lot going on in this test so let me try and explain it before showing the Map method. The Map method takes an generic input (in our case a Customer object) and an generic output (a string). It then takes the list that it is going to convert (recordBook) and a method that converts a Customer to a string (ReverseFullName). The result is a list of strings with the customer's name reversed. Here is the implementation:

public IEnumerable<TOutput> Map<TInput, TOutput>(IList<TInput> input, Converter<TInput, TOutput> converter)
{
    foreach (TInput item in input)
    {
        yield return converter(item);
    }
}

This is the C# implementation of the Map higher order function from functional programming. I will leave it up to you the reader to implement the last of the big three from functional programming (Reduce or Fold). Also try and think about how you would create a function to reverse all customers names who are active. Maybe once the dust settles after DevTeach I will post the solution.

Thursday, November 22, 2007 10:33:22 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

Our Customer comes to us and likes what we have done setting up a way for them to find if a customer is active, but now they want to keep track of all the customers. We come up with the concept of a Customer Record Book and our client seems happy with that:

[Test]
public void ShouldBeAbleToCreateACustomerRecordBook()
{
    IList<Customer> recordBook = new List<Customer>();
    recordBook.Add(new Customer(new DateTime(2007, 04, 29), "Steven", "Rockarts"));
    recordBook.Add(new Customer(new DateTime(1965, 04, 29), "John", "McCarthy"));
    recordBook.Add(new Customer(new DateTime(2006, 04, 29), "Doug", "Armstrong"));
 
    CustomerRecordBook customerRecordBook = new CustomerRecordBook(recordBook);
 
    Assert.IsNotNull(customerRecordBook);
}

public class CustomerRecordBook
{
    private IList<Customer> customerList;
 
    public CustomerRecordBook(IList<Customer> customerList)
    {
        this.customerList = customerList;
    }
}

Our record book does its job of storing customers well but our client wants us to add a way to find all the active customers in it. Our first attempt at creating a way to find all the active customers looks like this:

[Test]
public void ShouldFindAllActiveCustomers()
{
    IList<Customer> recordBook = new List<Customer>();
    recordBook.Add(new Customer(new DateTime(2007, 04, 29), "Steven", "Rockarts"));
    recordBook.Add(new Customer(new DateTime(1965, 04, 29), "John", "McCarthy"));
    recordBook.Add(new Customer(new DateTime(2006, 04, 29), "Doug", "Armstrong"));
 
    CustomerRecordBook customerRecordBook = new CustomerRecordBook(recordBook);
 
    Assert.AreEqual(1, customerRecordBook.Filter(new ActiveCustomerSpecification()).Count);
}

public class CustomerRecordBook
{
    private IList<Customer> customerList;
 
    public CustomerRecordBook(IList<Customer> customerList)
    {
        this.customerList = customerList;
    }
 
    public IList<Customer> Filter(ActiveCustomerSpecification specification)
    {
         IList<Customer> list = new List<Customer>();
 
         foreach (Customer customer in customerList)
         {
             if(customer.IsAnActiveCustomer(specification))
                list.Add(customer);
         }
 
         return list;
     }       
}

The Filter method that we just created takes in the ActiveCustomerSpecification, creates a temporary list and then iterates through the customer list adding the active customers to the temporary list, once it is done it returns the temporary list.  Our test passes but there are a couple of improvements we can make to this code. First of all, we don't need to create a temporary list or use an IList<Customer> we can leverage the yield keyword and IEnumerable<Customer> to make our code tighter:

public IEnumerable<Customer> Filter(ActiveCustomerSpecification specification)
{
    foreach (Customer customer in customerList)
    {
        if (customer.IsAnActiveCustomer(specification))
            yield return customer;
    }
}

This change will break our test since IEnumerable does not contain a definition for count. If we look at the available constructors for the List class we can see that List has a contructor for creating a list from IEnumerable. So we can easily fix our test:

[Test]
public void ShouldFindAllActiveCustomers()
{
    IList<Customer> recordBook = new List<Customer>();
    recordBook.Add(new Customer(new DateTime(2007, 04, 29), "Steven", "Rockarts"));
    recordBook.Add(new Customer(new DateTime(1965, 04, 29), "John", "McCarthy"));
    recordBook.Add(new Customer(new DateTime(2006, 04, 29), "Doug", "Armstrong"));
 
    CustomerRecordBook customerRecordBook = new CustomerRecordBook(recordBook);
 
    Assert.AreEqual(1, new List<Customer>(customerRecordBook.Filter(new ActiveCustomerSpecification())).Count);
}

Now we are able to find all active customers in a list.

Thursday, November 22, 2007 9:46:45 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

Encapsulating Criteria Using Specifications

"Specification provides a concise way of expressing certain kinds of rules, extricating them from conditional logic and making them explicit in the model."

Eric Evans : Domain Driven Design

In our domain model we have a normal Customer object that looks like this:

public class Customer
{
    private DateTime signUpDate;
    private string firstName;
    private string lastName;
 
    public Customer(DateTime signUpDate, string firstName, string lastName)
    {
        this.signUpDate = signUpDate;
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }
 
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
 
    public DateTime SignUpDate
    {
        get { return signUpDate; }
    }
 
    public bool IsAnActiveCustomer()
    {
        IRange<DateTime> activeRange = new Range<DateTime>(this.signUpDate, this.signUpDate.AddYears(1));
 
        return activeRange.Contains(DateTime.Now);
    }
}

With this code, we have defined a customer as something with a first name, last name, and a signup date. We have also specified (using the IsAnActiveCustomer method) that an active customer is a customer who has signed up with us within the current year.

This method that determines whether or not a customer is active as it stands isn't very flexible. To make the method more flexible we can extract the criteria that determines whether or not a customer is active or not and add it as a parameter to the IsAnActiveCustomer method so that the criteria can be passed in.

In order to encapsulate the active customer we can leverage the specification pattern. Our first attempt at making an active customer specification might look something like this:

public interface ISpecification
{
    bool IsSatisfiedBy(Customer customer);
}
 
public class ActiveCustomerSpecification : ISpecification
{
    public bool IsSatisfiedBy(Customer customer)
    {
        IRange<DateTime> activeRange = new Range<DateTime>(customer.SignUpDate, customer.SignUpDate.AddYears(1));
 
        return activeRange.Contains(DateTime.Now);
    }
}

and in the customer class:

public bool IsAnActiveCustomer(ISpecification activeCustomerSpecification)
{
    return activeCustomerSpecification.IsSatisfiedBy(this);
}

By adding the specification as a parameter to the IsAnActiveCustomer method we have inverted the control of the definition of an active customer to consumers of the customer class. There may be different definitions of what an active customer is in different situations.

We can make one more improvement to the specification class before moving on. What happens if another class wants to leverage the specification interface? They have to create a new specification for their new type...or they can use generics to save themselves the time of writing a new specification interface for every new type. Here is the result of our change:

public interface ISpecification<T>
{
    bool IsSatisfiedBy(T item);
}
 
public class ActiveCustomerSpecification : ISpecification<Customer>
{
    public bool IsSatisfiedBy(Customer customer)
    {
        IRange<DateTime> activeRange = new Range<DateTime>(customer.SignUpDate, customer.SignUpDate.AddYears(1));
 
        return activeRange.Contains(DateTime.Now);
    }
}

public bool IsAnActiveCustomer(ISpecification<Customer> activeCustomerSpecification)
{
    return activeCustomerSpecification.IsSatisfiedBy(this);
}
Thursday, November 22, 2007 8:17:48 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

Over the past month I have been preparing for my presentation at DevTeach in Vancouver. I am filling in for JP on the topic "Generics they're not just about collections". I am really nervous and one thing that I have been trying to work on is my ability to better communicate the concepts that I have learned and have trapped in my head.

I feel that my past 2 presentations on Windsor Container could have been a lot better if I were able to explain the underlying concepts behind a product like Windsor instead of just taking for granted the audience's knowledge.

After practicing my presentation multiple times in front of my dogs, I am not satisfied with how I am communicating these topics and I feel that writing out my presentation in front of my blog readership and asking for feedback is the next best thing.

If you have any suggestions or if there is anything that is unclear to you based on my explanation of things, please feel free to comment on this post or to send me a email at stevenrockarts@gmail.com.

Using Generics without Collections

Please not that this part of the presentation is based off of JP's original Generics article. How many times have you written a class like the following to check if something is within a range?

public interface IRange
{
    bool Contains(int valueToCheck);
}
 
public class Range : IRange
{
    private readonly int start;
    private readonly int end;