Entity Framework Week Part 1: Introduction, Configuration and Initialization

In February 2011 I found myself doing some contract development work in a team that was still doing data access using raw ADO.NET and stored procedures. Being the NHibernate fanboy that I am, I naturally attempted to persuade them of the benefits of moving over to NH, even going so far as to develop (in my own time) an NH-powered version of their application. My efforts were partially successful. The team were sold on the idea of using an ORM, but wanted me to develop a second proof of concept using Microsoft ADO.NET Entity Framework rather than NHibernate. This prompted much mirth amongst my FaceBook friends. ...

7 March 2011

Enterprise Integration Anti-Patterns #2 – Shared Assemblies

Having slain the beast that is Shared Database, the next dragon to appear on my Enterprise Integration horizon is Shared Assemblies. That is, the suggestion that Application A can leverage the functionality of Application B by simply adding references to B’s DLLs. After all, this potential for reuse is why we put our code in reusable assemblies in the first place, isn’t it..?! ...

1 December 2010

NHibernate and Mapping Aggregates

A few days ago a friend emailed me the following question regarding NHibernate mappings for a solution he’s currently developing: “I have an idea entity that has a collection of comment entities and I need to get the comment count for each idea. I made a massive mistake at the beginning by calling idea.Comments.Count (even worse, I did it in the view!), which due to the collection being lazy-loaded caused about 10 database calls so performance was sluggish even with second level cache. I was therefore wondering how you would do it – would you use HQL and use Comments.size or would you do something differently?” ...

25 November 2010

Enterprise Integration Anti-Patterns #1 – The Shared Database

So, I was in the office at a client site, walking back to my desk after grabbing a quick coffee, when a developer on a sister product to my current project grabbed me and asked (I paraphrase somewhat): “Hey, Ian – [my app] needs to retrieve [small piece of data] from [your app], so I’ve stuck a stored proc in [your database], OK?” I felt quite violated. My poor app, what had it done to deserve this despicable treatment? ...

8 November 2010

MVC – Where To Put Business Logic?

I just had an email from a friend asking where business logic should go when using MVC – the controller or the model. I thought I’d share my reply: I think asking whether the business logic should go in the Model or the Controller is a false dichotomy. There are different kinds of business logic, and there’s also the possibility of putting it somewhere else entirely. Much business logic does indeed belong in the model, particularly stuff like encapsulating calculations and projections, adding meaning to raw data properties, and ensuring domain objects are maintained in a valid state. I see this as the traditional side of OO design – encapsulating logic and restricting accessibility. ...

3 November 2010

Testing IoC Registrations

When I first started writing automated unit tests for my code, I remember getting carried away writing lots of tiny little tests, each with a single Assert. It felt good initially to see all those green ticks, but rapidly became a maintenance headache, and I am now happier having fewer less-brittle tests that do more. I think it’s great when tools like AutoMapper provide powerful methods like… …allowing me to test large swathes of the system configuration with a single, stable test. ...

9 April 2010

Testing LINQ Queries

I’ve been asked “How do you test LINQ queries?” a couple of times in the past few months, so I thought I’d blog my answer for the benefit of all you lovely people. Firstly I should explain how I’ve been doing most of my data access in recent months. I find that the Repository pattern, and particularly the .NET implementation described here by Fabio Maulo meets the vast majority of requirements I have in my applications, and I fall back on additionally using custom DAOs when required. ...

9 April 2010

WCF – Global Exception Handling

I’ve done a fair bit of WCF development recently (and amazingly I came out of it relatively unscathed, both psychologically and emotionally). Using Castle Windsor’s WCF Facility helped a great deal, but I still found myself writing a fair bit of infrastructure and plumbing code. I thought I’d blog some of these code snippets lest I forget, and on the offchance they might help others. One of the things I wanted to achieve was to ensure that any unhandled exceptions that propagated to the top of the stack were logged. The following custom EndpointBehavior achieves that. I’m using Castle Logging Facility here, but you could of course swap out the logging code for whatever error handling code you require. ...

9 April 2010

WCF – Logging Before and After Operation Invocation

Here’s another custom WCF EndpointBehavior I found useful recently. Not content with logging all unhandled errors, I wanted to output a DEBUG level log message before and after every operation invocation. As before, I’m using the Castle Windsor Logging Facility to handle my logging, but you can swap that out for your logger of choice if required. Firstly, here’s the EndPointBehaviour itself. This adds a custom CallContextInitializer to every service operation: ...

9 April 2010

WCF – NHibernate Unit Of Work Endpoint Behavior

OK, my last WCF-related code snippet of the day, I promise. This is quite similar to the last one. I required that the WCF service I was developing started a new NHibernate Session for each invocation, and closed it after invocation (i.e. session-per-request). I couldn’t rely on the ASP.NET session start and end events in global.asax as this particular WCF service was to respond to MSMQ messages rather than HTTP. ...

9 April 2010

An MVC Gotcha and the PRG Pattern

If you’ve recently moved across to ASP.NET MVC development following years of wrangling with the leaky abstraction that was WebForms, you may have encountered some seemingly curious behaviour when posting back to the same URL. Suppose we have the following simple, contrived and utterly imagination-free model: Here’s a view: And here’s the controller: Note that we have two separate Index actions – one for HTTP GET which instantiates and displays a new FooModel instance, and one for HTTP POST which modifies one of the properties on the posted FooModel instance before re-rendering the view. ...

9 April 2010

MoreLINQ – IEnumerable Extension From Jon Skeet

I’ve just become aware of this MoreLINQ project containing some neat IEnumerable extensions authored by the inimitable Jon Skeet. I particularly like the look of DistinctBy – that’s gotta be a lot easier than passing custom instances of IEqualityComparer into Distinct(). Fun fact – I used to work with someone who went to school with Jon Skeet! True Story!

30 March 2010

On the Inherent Negativity of Computer Programmers

A couple of weeks ago, early one morning, my friend and fellow coder Scott asked a curious question: “Do you think that programming computers for a living gives you a negative outlook on life?” Now, Scott is unfortunately responsible for administering occasional TLC to a codebase that I cut as long ago as 2004, so I assumed he was referring to this and was about to raise some complaints about my hand-rolled query object criteria framework or other coding horror. I steeled myself to defend my work as he continued to explain: ...

1 February 2010

Deduplication Fail

Oh, the irony!

2 December 2009

A Circuit Breaker Which Trips On Frequency Of Failures

@Jez tweeted last night: Funny, but not true. I am enamoured with the Castle Windsor project because its power makes it fairly simple for me to develop loosely-coupled systems which are easily maintained and tested. The wide range of Facilities and Contrib projects also integrate nicely with the other parts of my current development stack (NHibernate, WCF, WF, log4net). ...

28 October 2009