Jul 16

I have been doing some Test-After Development unit testing on an ASP.NET MVC app and hit code that uses the cache through HttpContext.Current.Cache (I bet @serialseb would just love that! :P ) which as you may know is a royal pain in the arse for getting at or mocking without a web server.

As most sane people do I binged (that just does not sound right!) for an answer to my problem and came across a few interesting things but the closest I came initially was a post from Phil Haacked on his HttpSimulator cache where someone mentioned that using HttpRuntime got around the issue. I tired, it failed and I went back to Bing and came up with a post on Scott Hanselman’s blog where he details a conversation about simply including System.Web along with System.Web.Caching in non-web applications. I was skeptical but people have noted success with this method so I tried it and what did I find, it sodding well worked!

Granted the articles I found are old but this is the first time I have tried to unit test the Cache before so this was quite useful.

In other news I have ordered a copy of Working Effectively with Legacy Code by Michael Feathers from Amazon after @chriscanal recommended it, looking forward to having a read.

Nov 23

Well, I was going to be writing up a review of DDD7 but after seeking out a good 3 column layout that I actually liked for WordPress last night (and failing) I have decided to take a new course of action.

I have been wanting to write a blog with ASP.NET MVC for quite some time, just never really found the time. I am now however, faced with a 7+ hour drive home from Reading and I think this would be the perfect time to write one, if nothing else just to see if I can actually do that in 7 hours :)

The tools I have with me are:

  • Laptop
  • Mobile Broadband Dongle
  • Visual Studio 2008
  • SQL Server 2008 Express
  • ASP.NET MVC
  • ADO.NET Entity Framework
  • Endless antics from Gary Short
  • Time

Update:

I knew it was adventurous and it was far more fun to be sociable in the car and help Colin Mackay out on the Developer Day Scotland website than it was to work on the blog. Saying that however, I think I am going to continue on with the blog and turn it into an MVC tutorial (or perhaps “Yet Another MVC Tutorial” better suits) which would allow me more time to develop it and actually provide something useful on the actual blog as well.

So while I don’t actually have a new blog yet, this has given me the idea to develop my own blog system in MVC and publish a tutorial along the way. Neat huh? (Well I think so)

DDD7 post will follow in the next couple of days but in the meantime, a big thanks to everyone who helped organise and run Developer Day 7 and a huge shout out to everyone who attended and made it what it was!

Tagged with:
Nov 05

C# 3.0 added lambdas to the language syntax. I wont go into details as to what those are as there is pleanty of information on the web, what I will show is how you can assign a lambda to a variable for later use.

I do a lot of work with LINQ and the Entity Framework and as such lambdas come in very useful when using LINQ expressions (I am not a fan of the query syntax), however it is sometimes useful to reuse a lambda. A lamda is simply a delegate that has a number of different prototypes all under Func.

[source:C#]
Func<TResult>
Func<T,TResult>
Func< T1,T2,TResult>
Func< T1,T2,T3,TResult>
Func< T1,T2,T3,T4,TResult>ÂÂ
[/source]

All of the lambdas take a result type and each varient adds an additional input type. 

How does this help us save a lamda? Well lets take an example piece of code
[source:C#]
model.Users.Count(u => u.UserName == username);
[/source]
Here I am searching through the Users collection for any users which have UserName equal to the username variable. The input type for the lamda is User as Users is actually an ObjectQuery<User> object, the return type is bool as I am comparing 2 values and returning the result.

To store this lamda I do the following:

[source:C#]
Func<User,bool> userExpression = (u => u.UserName == username);
model.Users.Count(userExpression);
[/source]
If your lambda uses say 2 input types your code would look something like this:

[source:C#]
Func<User,Ticket,bool> userExpression =
     ((u,t) => u.UserName == username && t.UserID == u.UserID );ÂÂ
[/source]

The main difference here being that the list of input types must be wrapped in parenthesis (u,t) followed by the => operator.

For more information on lamdas have a look at this InformIT Article