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

A Circuit Breaker Which Trips On Frequency Of Failures

@Jez tweeted last night: @ianfnelson admit it: you use Castle Windsor primarily to highlight and lampoon Google’s poor selection of adwords?! — 𝚂𝚑𝚘𝚘𝚝𝚒𝚗𝚐 𝚂𝚌𝚒𝚕𝚕𝚢 (@shootingscilly) October 26, 2009 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

Castle Windsor Array Resolution Gotcha

The shiny new system which I’ve recently been developing makes heavy use of the Chain of Responsibility pattern, and as such a number of service classes take an array of objects in the constructor: I’m using Castle Windsor for dependency management, so I’ve been fluently registering all instances of ILeadAllocator: Easy, right? And yet at runtime Windsor surprised me by throwing this exception in my face: C M K – a a e s r y l t s s e l h a e a ( d . l c A M l o l i s m l c . p o r L o c o e n a K a e t e d n o r s t r n . s s e A l p w w . p i h H l t i a i h c n c h d a s l t p w e i e a r o c s s n i . S f n H e i o a r c t n v d i k r l c e e e e y g r . s i E L ) s x e t c a e e d r p A e t l d i l . o o n c : a t C i a o n n ’ S t e r c v r i e c a e t e i s c o w m a p i o t n i e n n g t f & o # r 8 2 t 1 h 6 e ; M f a o r l s l h o a w l i l n s g . L d e e a p d e s n . d A e p n p c l i i e c s a : t i o n S e r v i c e . L e a d A l l o c a t i o n S e r v i c e ’ a s i t h a s d e p e n d e n c i e s t o b e s a t i s f i e d . Huh?! What gives? Well, a little Googling revealed this post from Castle founder Hamilton Verissimo explaining that by default the Castle MicroKernel expects me to define what should be included in the array. But he goes on to explain that the behaviour I desire can be achieved by registering a custom subresolver with the microkernel. That subresolver has since been included in the Castle Windsor distro, so in actual fact all I needed to do was add the following line of code when configuring my container: ...

26 October 2009

Castle Windsor Lifestyle Gotcha

I always knew my lifestyle choices would come back to bite me some day… I’ve been making heavy use of the Castle Windsor container to handle dependency and configuration management in the shiny new system that I’m currently developing. For the most part it’s been an absolute blast, allowing me to easily create a loosely-coupled system architecture and focus my efforts on what the system should be achieving for the client, rather than worrying about how it all hangs together. ...

19 October 2009

Alt.Net UK In The North

I spent much of this weekend over in Bradford, talking shop with the good folks at the Alt.Net UK ‘in the North’ conference. I had a great time, learned much, and am grateful to Richard Fennell of Black Marble and the other organisers and sponsors for making the event possible. This was the first Open Spaces event I’ve attended, and I was pleasantly surprised at how orderly and productive the sessions turned out to be. Unlike conventional sales-pitch conferences of the MSDN roadshow variety, this was more of an open forum, with an expectation that all of the relatively small number (25ish?) of participants would, well, participate! ...

19 April 2009

ASP.NET Just Became a Legacy Platform

One of the least surprising but more significant announcements to come out of Mix ‘09 last week was the general availability of ASP.NET MVC 1.0. The development process for this framework has been very transparent, with no fewer than eight previews, betas and release candidates made available over the past year or so. Microsoft are being careful to tout ASP.NET MVC as being “an alternative, not a replacement, for ASP.NET Web Forms”, but just look at their list of some of its benefits: ...

22 March 2009

NDepend

OK, dear readers, today I’d like to talk about tools. Software development isn’t really so different from traditional skilled crafts in that it is important to have an understanding of the tools available to help you create a quality products in the least amount of time possible. For .NET developers, the Visual Studio IDE is usually just the starting point, and most of us will come to rely on some of the tools listed in Scott Hanselman’s famous tools list. Personally, I can’t imagine coding without first installing Refactor Pro, editing text files in anything other than TextPad, or working on a machine that doesn’t have Snag It installed. ...

4 February 2009

.NET Coding Standards

Sometimes, when you join a new team as a .NET developer, the team lead proudly points you in the direction of a Word document or wiki page detailing the team’s house coding standards. In my experience, these documents are invariably: Incomplete. Subjective. Not enforced. Largely copied from the interweb. Obsessed with the trivial (e.g. whitespace) while failing to mention the genuinely useful (e.g. boxing/unboxing, avoiding use of deprecated classes, Dispose pattern, etc). I know this because I’ve been involved in creating such documents in the past 🙂 ...

23 January 2009

MSB3247 – Dependent Assembly Conflicts

Earlier today a dev came over to ask me about a compiler warning he was getting when building a .NET solution: MSB3247: Found conflicts between different versions of the same dependent assembly. This is basically telling you that one project or dependency in your solution is referencing one version of an assembly, whilst another project or dependency is trying to reference a different version of the same assembly. Unhelpfully, the message and build output don’t tell you which dependent assembly is causing the problem! ...

4 December 2008

Performance of Loops on Collections

Interesting article from Patrick Smacchia (the NDepend guy) in which he runs some benchmarks on the cost of looping in different ways over different constructs and comes to the conclusion that: “looping on array using for is 5 times cheaper than looping on List<T> using foreach (which I believe, is what we all do)” Well worth a read, although I suspect that in my solutions the bottlenecks will remain the out-of-process calls, i.e. database access and web service calls. Still, a 5x gain is not to be sniffed at. ...

28 November 2008

A LINQ Gotcha – First Operator

So, I was using the First operator to return the first item element in a sequence matching a predicate. I expected a null to be returned if the sequence did not contain any matching elements, but found (well, Scott found) that this actually throws an InvalidOperationException. Turns out I should have been using the FirstOrDefault operator instead…

14 December 2007

MSDN Technical Roadshow 2007

I’m off to the MSDN Technical roadshow in Harrogate tomorrow, and am actually really looking forward to it this time. These events are free, so always well worth attending when they’re local, but some years are certainly better than others – it mostly depends on the material being covered. I remember one year circa 2003-4, when .NET was well established, but before the Whidbey release, so the majority of the day was effectively just a sales pitch covering elementary .NET concepts, presumably for those laggards who hadn’t yet made the great leap from VB6. Another data-binding demo, anybody? Yawn. ...

12 March 2007

A Serializable KeyValuePair Class

Having accepted that Returning DataSets from WebServices is the Spawn of Satan and Represents All That Is Truly Evil in the World (or at least, not exactly best practice), I’ve been trying to make a conscious effort to instead use lightweight custom objects in my middle tier, to be exposed via web services for use by other applications. I felt sure that some of the new Generic classes in the .NET 2.0 FCL would help me in my quest. Specifically, as I often need to expose simple arrays of Key/Value Pair metadata, I planned to create an instance of the System.Collections.ObjectModel.Collection class, containing a bunch of System.Collections.Generic.KeyValuePair objects. ...

17 September 2006

Big Integer

As I mentioned last summer, I do enjoy whiling away a few hours attempting to solve the maths / progamming challenges set on Project Euler. My language of choice for most puzzles is C#, but this hampered me on some of the questions as there is no in-built support for really big integers. However, a little bit of Googling and I came across this excellent BigInteger class on Code Project which does everything I need and more – and it seems bloody fast, too. Thanks to this class, I’ve been able to knock off several more problems this weekend, and now have a rating of “11% Genius” – still some way to go, then… ...

17 July 2006