Tuesday, March 20, 2012

The text in TextEdit - John Appleseed

I came to this post grumpy and ready to moan. TextEdit is slow in Lion. I use it a lot - many times every day. Since the Mac OS Lion, when Apple made it auto-save in a similar way to apps on iOS, it has a real problem. It spins its 'I'm busy doing something' ball, preventing me writing my really important and interesting gems. It does get a bit annoying after a while.
But, I now know that I should stop moaning. Moaning is pointless AND I have just learnt that the text that I write will probably never be as good as the text hidden in the application's icon:



Dear Kate,
Here's to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They're not fond of rules. And they have no respect for the status quo. You can praise them, disagree with them, quote them, disbelieve them, glorify or vilify them. About the only thing you can't do is ignore them. Because they change things.

Take Care,
John Appleseed



Saturday, March 17, 2012

ASP.NET page lifecycle

Init
1. PreInit
Can manipulate Master pages and Themes
2. Init
All the controls have been constructed in the control tree. Themes and master pages completed.
3. InitComplete

Load
4. PreLoad
5. Load
Server-side events have all fired
6. LoadComplete

Render
7. PreRender
8. PreRenderComplete
9. SaveStateComplete
10. Render
Response sent back to the client.
11. Unload

LINQ Lambda and Extension methods

This post is a gentle ramble through the application of Extensions and delegates and Funcs that go to make LINQ. I found it quite a lot to get my head around at first and I hope this explains it well. Because if it is explained well it is interesting. If I fail, then I would recommend you do some google/Bing searching. There must be stuff out there.

I have a post that describes Extension methods. It might be worth reading that first.

Example 1 - Creating a rather pointless Extension method to IEnumerable.
This example gives a method to the IEnumerable interface that will search for the first letter of a string and return the list. If you do not know what an extension method is, do read the other post. Unfortunately this post assumes knowledge of that.
Note the use of the Yield keyword. I have a post on that if you need it.

using System;
using System.Collections.Generic;
using MyExtensions;

namespace MyProgram
{
class Program
{
public static void Main (string[] args)
{
IEnumerable<string> names = new[] {"Will", "Bill", "Jill", "Bob" };
IEnumerable<string> nameThatStartwithB = names.NameBeginsWithLetter("B");
foreach (var name in nameThatStartwithB)
{
Console.WriteLine(name);
}
}
}
}

namespace MyExtensions
{
public static class NameFilterExtensions
{
//This is an Extension method for IEnumerable of string - note the "this"
// in front ot the IEnumerable.
public static IEnumerable<string> NameBeginsWithLetter(this IEnumerable<string> names, 
                                                  string startChar)
{
foreach(var s in names)
{
if (s.StartsWith(startChar))
{
yield return s;
}
}
}                                     
}
}


  • Note that the NameBeginsWithLetter(..) method is a static method and has the "this" keyword. This is an Extension method for the IEnumerable interface. Exactly in the same way that LINQ operators are built. In the same why that our extension method is in a namespace, the LINQ extension methods are defined in System.Linq
  • Note in the Main() function, how our method is a "member" of the names object - and IEnumerable object.
  • Also note that the namespace MyExtensions, which contains my extension method, is included in the Usings.

This example is a bit rubbish. It works but it's not very generic. The next example is more generic as we work towards a nice and elegant solution.

Making the Method Generic with Lambdas
The generic methods used by LINQ, the Select, Orderby etc. are inside the System.Linq namespace. There is an Enumerable class. In the following screen shot I have used dotPeek to decomplile the System.Core dll and in the System.Linq namespace we can get an idea of what is coming. Notice on the highlighted portion there is a bunch of generic looking <T>'s and the Func keyword. That's where we're heading though I didn't realise it till I just saw it!



If we decomplie the Where function we can see what Microsoft have done, and it's very close to our previous example. We can see the main parts that go to making this an extension method.



public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
    {
      if (source == null)
        throw Error.ArgumentNull("source");
      if (predicate == null)
        throw Error.ArgumentNull("predicate");
      if (source is Enumerable.Iterator<TSource>)
        return ((Enumerable.Iterator<TSource>) source).Where(predicate);
      if (source is TSource[])
        return (IEnumerable<TSource>) new Enumerable.WhereArrayIterator<TSource>((TSource[]) source, predicate);
      if (source is List<TSource>)
        return (IEnumerable<TSource>) new Enumerable.WhereListIterator<TSource>((List<TSource>) source, predicate);
      else
        return (IEnumerable<TSource>) new Enumerable.WhereEnumerableIterator<TSource>(source, predicate);
    }


Example 2 - Using a Delegate
In this example I have created a delegate that is passed into the Extension method. As I see it, this is an improvement in two ways. (1), if we want to filter in a different way, we can write a new delegate method  and pass it in, rather than write a new Extension method on the IEnumerable object. (2), this separates the WHAT from the HOW. I mean that the Extension method says it is going to filter, but it doesn't know How it is going to filter. That, is defined by the delegate function.

namespace MyProgram
{
class Program
{
public static void Main (string[] args)
{
IEnumerable<string> names = new[] {"Will", "Bill", "Jill", "Bob" };
//IEnumerable<string> nameThatStartwithB = names.NameBeginsWithLetter("B");
IEnumerable<string> nameThatStartwithB = names.Filter(NameBeginsWithLetterB);
foreach (var name in nameThatStartwithB)
{
Console.WriteLine(name);
}
}
// This can be passed to the extension method. It is a bit more flexible 
// because we can write another method like this rather than anther 
// extension method.
static bool NameBeginsWithLetterB(string name)
{
return name.StartsWith("B");
}
}
}

namespace MyExtensions
{
public static class NameFilterExtensions
{
  // Notice this delegate function                                  
public delegate bool MyFilterDelegate<T> (T item);

  // Notice that the delegate is passed into the extension method
public static IEnumerable<T> Filter<T>(this IEnumerable<T> src, MyFilterDelegate<T> predicate)
{
foreach(var i in src)
{
if (predicate(i))  yield return i;
}
}  
}
}



Example 3 - Anonymous Delegates
I think that Anonymous functions, or anonymous delegates are so called because they do not appear in the call stack. They are "in-line" a-la functional programming. In this example we can use the in-line Delegate. This is now looking more like the LINQ syntax.

namespace MyProgram
{
class Program
{
public static void Main (string[] args)
{
IEnumerable<string> names = new[] {"Will", "Bill", "Jill", "Bob" };
//IEnumerable<string> nameThatStartwithB = names.NameBeginsWithLetter("B");
//IEnumerable<string> nameThatStartwithB = names.Filter(NameBeginsWithLetterB);
IEnumerable<string> nameThatStartwithB = names.Filter(delegate (string name)
                                                    {
return name.StartsWith("B");
                                        });
foreach (var name in nameThatStartwithB)
{
Console.WriteLine(name);
}
}
}
}


Example 4 - Remove the Delegate and use Lambdas
The designers of the language/complier decided that they could simplify the syntax for the above code and they decided that they could:
  • Remove the delegate keyword because the compiler knows the user will be passing in a delegate.
  • Remove the type of the parameter because we know that we are dealing with an IEnumerable of strings.
  • Remove the Return keyword.
  • Remove the  "{ }" around the return statement.
  • Add the "Goes to" operator, =>
  • Remove a superflous semi colon (you'll spot that one when you write this example)

And we end up with:


IEnumerable<string> nameThatStartwithB = names.Filter( (name) => name.StartsWith("B"));

This is the lambda expression and is so much easier to type, and read.


The class in full is as follows:

namespace MyProgram
{
class Program
{
public static void Main (string[] args)
{
IEnumerable<string> names = new[] {"Will", "Bill", "Jill", "Bob" };
IEnumerable<string> nameThatStartwithB = names.Filter( (name) => name.StartsWith("B"));
foreach (var name in nameThatStartwithB)
{
Console.WriteLine(name);
}
}
}
}



Example 5 - Using Func 
I have a post on Func. (Basically, Func is a type that encapsulates callable code)

We can use the Func type to simplify out Extension method. We do this by using Func to remove the delegate declaration.


Before:
namespace MyExtensions
{
public static class NameFilterExtensions
{                                  
public delegate bool MyFilterDelegate<T> (T item);
public static IEnumerable<T> Filter<T>(this IEnumerable<T> src, MyFilterDelegate<T> predicate)
{
foreach(var i in src)
{
if (predicate(i))  yield return i;
}
}  
}
}


After:
namespace MyExtensions
{
public static class NameFilterExtensions
{                                  
public static IEnumerable<T> Filter<T>(this IEnumerable<T> src, Func<T, bool> predicate)
{
foreach(var i in src)
{
if (predicate(i))  yield return i;
}
}  
}
}


And that is about it.

Friday, March 16, 2012

How to use the Func type in C#

To make using delegates easier, Microsoft introduced a new generic type, Func.

Main features:

  • The Func encapsulates callable code. 
  • It can take 1 to 16 input parameters.
  • The last prameter is the return type.
There, easy.

As an example, Func<int, int, bool> takes two integers as input and will return a bool.


private static void PlayWithFunc()
{
// First two params are input, last param is output
Func<int, int, int> addemup = (x,y) => x+y;
Console.WriteLine( addemup(2, 3));
// Note that with the single input param, x is in brackets: (x)
Func<int, bool> notZero = (x) => x!=0;
Console.WriteLine( notZero(1) );
// Note also,that with the single input param, x is NOT brackets: x
Func<int, int> sqr = x => x*x;
Console.WriteLine( sqr(2) );
Console.ReadLine();
}

Note that if you have 2 or more parameters in the lambda expression you must have parenthesis. If you have one parameter the parameters are optional.


Action
Ah, but what happens if you don't want to return a value. 

The Action type is the same as Func, except it returns void.


Action <string> printToConsole = s => Console.WriteLine("The string is: " + s);

printToConsole ("Hi there");
Console.ReadLine();

Extension methods in C#

I think these are clever. A neat compiler trick but where the idea came from I don't know.
What they provide.
They allow us to add methods to an object without changing the code for that object. Their main use is in LINQ.  How is it that LINQ can have a method that works on so many types. How did they do that? Well the solution was really quite clever - and simple.

At the bottom of LINQ is the IEnumerable interface and that is very basic and simple. It is something like this:

using System.Collections;

namespace System.Collections.Generic
{
public interface IEnumerable<out T> : IEnumerable
{
new IEnumerator<T> GetEnumerator();
}
}

GetEnumerator lets us enumerate, or loop through collections:

IEnumerable<string> names = new IEnumerable<string>[] {"Bill", "Will", "Jill" };
foreach (var name in names)
{
Console.WriteLine(name);
}

Really handy and used all the time.

As I said, IEnumerable is at the bottom of LINQ and Microsoft didn't want to change this every time a whizzo idea arrived so they created the Extension method. Using these, it appears as if IEnumerator has more and more methods, but actually it doesn't. It's smoke and mirrors. We can add methods to it, but not change it. What fun.

Using DateTime as an example
In C#, DateTime is a struct, and is sealed, so we can not inherit from it. If say, you wanted to add fancy functions to DateTime we would have a couple of options. We can write a utility class that you pass a DateTime object into and the date maths, the workings, can be hidden in there. We would therefore call this Utility class method throughout our code, passing the date object into it. We could, if we wanted to, make the class static with a static method. That's not really news.

In the example below there are two methods that show how we can implement a method on a DateTime. The first, GetPreviousYear(DateTime date) is the usual way to do this.
The second, GetPreviousYearExtensionVersion(this DateTime date), is the extension method way of doing things.
There are three important aspects to note about the extension menthol version:
  1. The class is static,
  2. The method is static,
  3. The parameter type declaration has "this" prefixing it.
namespace Stuff
{
class MyProg
{
public void DoMoreStuff()
{
DateTime date = new DateTime(2012, 3, 15);
int previousYear = MyDateHelper.GetPreviousYear(date);
DateTime date2 = new DateTime(2012, 3, 15);
int previousYear2 = date2.GetPreviousYearExtensionVersion();
}
}
public static class MyDateHelper
{
public static int GetPreviousYear(DateTime date)
{
return date.Year -1;
}
}
public static class MyDateHelperExtension
{
public static int GetPreviousYearExtensionVersion(this DateTime date)
{
return date.Year -1;
}
}
}

You can see that the first method is called using MyDateHelper.GetPreviousYear(date); 
That is ok, it seems reasonable because that's how we've done it for years. But the syntax of the extension method is much easier to understand I think. The previous call is, in this example, date.GetPreviousYearExtensionVersion(). That is a silly name and example. It would become, date.GetPreviousYear();
  • MyDateHelper.GetPreviousYear(date);
  • date.GetPreviousYear();
Now that is good.

Thursday, March 15, 2012

The Strategy pattern

This is one of the more simple patterns and is really useful. It is ideal for encapsulating rules and simplifying those if-else or switch statements.

If you have some code that looks at an object and makes a decision based on a property of the object, then you may need to consider the Strategy. You should also consider this if you think that you may, at some point, get asked to add another clause into that switch statement. Maybe the code calculates the exchange rate for US$, and Euro. But what happens when the Business decides they want the British £ also.
For that you may need to add an else clause or extend the Switch.

That's not good really as it breaks the Open-Closed principle. I have a post on this which contains a code example.

This diagram gives a general idea of the pattern.

As an example we could have an object that needs to calculate the exchange rate. So, at construction time we could inject the required calculator and the object calls the relevant method. If we want to add another currency calculator, say UKPound, then we create the new class UKPoundCalculator, it implements the interface IExchangeRateStrategy, and we pass it to the contact class. The beauty is, there are no changes made to the CalculateTheCost context class.



See:
Open-Closed Principle





Running NUnit from within Visual Studio

It looks like NUnit will run from within Visual Studio now.
http://www.nunit.org/index.php?p=vsTestAdapter&r=2.6

The current release version is 0.9 and only runs in Visual Studio 11, which is in preview release at the moment. That's good news for NUnit as new developers to Visual Studio will probably use NUnit over MSTest now the integration and not needing to use a separate application hurdle has gone.

Friday, March 2, 2012

Strategic Design - Responsibility Traps lecture by Eric Evans

This is an Excellent talk by Eric. 
Full of common sense and good observation.
The talk is essentially about using the Anti-corruption layer as a way to concentrate on the Core Domain.
Anti-corruption layer? A way of embracing the reality that often times it's better to leave the legacy system, the ball of mud, and concentrate on the new core features.

The example of what can be core domain and sub domain is excellent and simple.
Eric chose the rating feature used by Amazon and by Ebay.
In Amazon the star ratings are very handy but their business model does not depend on the feature. It is unlikely that the visitor to the site will purchase based on this feature. Maybe sometimes, but not significantly.
However in Ebay, the star rating is absolutely crucial in developing the trust between the seller and buyer. This therefore is key and is core domain.


Summary
Eric discusses the need for strategic thinking and how early design decisions have major impact on the organization and the entire development process. He uses the lens of DDD Strategic Design principles (emphasizing "Context Mapping" and "Distilling the Core Domain") to show how to avoid strategic failures and achieve strategic successes. Winning strategy starts with the domain

http://www.infoq.com/presentations/design-strategic-eric-evans

Work in the Core Domain - Part 4 in the book!