Tags: , , | Categories: .net, asp.net mvc Posted by balazs.zoltan on 7/10/2010 7:38 PM | Comments (0)

Some of the MVC mantras:

  • Keep models fat, controllers skinnyimages
  • Keep as much business logic in the models as possible
  • Models should not talk to view

Controller stinks if:

  • The controller services too many requests (i.e. has too many actions)
  • The controller has domain logic (i.e. model code has crept into the controller)

What a controller should not do:

  • Bind post data to view models – use model binders for this
  • Calculate something (domain logic) – view models or entities should do this
  • Validation – best if view model validation is handled automatically (using binders)
Tags: | Categories: .net Posted by balazs.zoltan on 2/13/2010 4:00 PM | Comments (0)

In .NET 2.0 we had something like:

delegate int Sum(int x, int y);
public static void Main()
{
	Sum sum = delegate(int x, int y) { return x + y; };
	Console.WriteLine(sum(1, 2));
}

The delegate type must be defined outside the method body, because you can’t create a type within a method. Along came .NET 3.5 with the new generic delegate types: Action and Func. The signatures of the Action<T>, Func<T> and Predicate<T> delegate types:

// Action is a delegate type with void return parameter
public delegate void Action<T>(T obj);
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);

// Func is a delegate type with a generic return parameter
public delegate TResult Func<T, TResult>(    T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

// For completeness sake:
public delegate bool Predicate<T>(T obj);

Both the Action and Func types have signatures declared to support up to 4 arguments. So now we are able to write (using anonymous delegates, observe that no named delegate was declared):

// the long form would be 
// sum  = new Func<int, int, int>(delegate(int x, int y) { return x + y; });
Func<int, int, int> sum = delegate(int x, int y) { return x + y; };
Console.WriteLine(sum(1, 2));

Transforming into lambda expressions

The format of the lambda expressions is: (parameter  list) => { statements; }. So the example above becomes:

Func<int, int, int> sum = (x, y) => x + y;
// an extra parenthesis may help readability
Func<int, int, int> sum = ((x, y) => x + y);

Reading lambda expressions is easier if you think in the terms of a method call and return value (some say that the => sign should be read “goes to”).

One more generic delegate

The Predicate<T> generic delegate represents any method that returns a bool and accepts a single typed parameter (a method that evaluates some criteria). Now let’s see an example first using the Predicate<T> delegate, an anonymous delegate and finally a lambda expression:

        Item[] items = 
        { 
            new Item {Name = "a"}, new Item {Name = "b"}, 
            new Item {Name = "c"}
        };
 
        // using Predicate generic delegate
        Item findUsingPredicate = Array.Find(
            items, 
            FindUsingPredicate);
    
        // using anonymous delegate
        Item findUsingDelegate = Array.Find(
            items, 
            delegate(Item item) { return item.Name == "b"; });

        // using lambda expression
        Item findUsingLambda = Array.Find(
            items, 
            item => item.Name == "b");

where FindUsingPredicate method is defined as follows:

    static bool FindUsingPredicate(Item item)
    {
        return item.Name == "b";
    }

In a nutshell this would be it.