Archive for the 'Uncategorized' Category

Why share knowledge?

I’ve been watching the Continuous Integration Workshop video that Eric Hexter and Jeffrey Palermo presented and that Headspring Systems kindly made available on their website.  Quite early into this video, Jeffery gives a refreshing perspective providing some insight into why Headspring are happy to share knowledge through mechanisms like OSS, free workshops and blogs.

We want to be working on problems that the industry hasn’t solved.

Jeffery mentions that at Headspring, they make use of many open source projects and that this allows them to focus on solving new problems, and move forward efficiently. This allows them to focus on delivering value to their customers rather than having to spend time re-inventing the wheel.

Having looked into this further, I found the following quote on Headsprings website:

Because so much of our efficiency comes from open-source sharing and re-use of successful code modules, we believe in the power of open source. So much so that we frequently share our own code with other developers; yes, even our competitors

They embrace giving back to the community – presenting the problems that they have solved, such that others may benefit in the same way.

 

I find this attitude very refreshing indeed.

Upgrading a webforms project to an ASP.NET MVC application

In a newly created ASP.NET MVC application, Visual Studio is quite friendly to us, and provides us nice little menus for creating our controllers/views:

The New Project dialog box

This menu does is not provided however, for any existing apps that we may want to migrate to using ASP.NET MVC.

How can we tell Visual Studio to treat this web app as an ASP.NET MVC app? I couldn’t find an immediately obvious way to do this, and it took me more than a simple google search to figure this one out, so here’s what to do:

  1. Unload the web project (right clicking on the project within Visual Studio –> Unload Project)
  2. Find the tag <ProjectTypeGuids>
  3. Add the guid {603c0e0b-db56-11dc-be95-000d561079b0};
  4. Reload the project
  5. The new menus should now be available! (you should probably now go ahead and add the Controllers and Views folders)

 

Hopefully this helps someone else too!

 

*disclaimer* This worked for me, but I place no guarantees that it will work for you too!! If your computer explodes, your cat passes away, or you end up getting a divorce, please don’t blame me!  *disclaimer*

Why not Make Every Method Virtual?

In an effort to make my blog suck less, I figured I’d post my response to Ward Bells Do not Make Every Method Virtual. He carries on the discussion of whether or not .NET methods should be made virtual by default, as they are in Java. You should read his post before mine, as this will establish the context of everything that follows (and if you don’t already subscribe to his blog,  be sure to add it – you’ve been missing out on a lot of good stuff).

All done? Ok, I’ll continue.

Reviewing case against default virtual methods

Unwanted Extension Points

Wards "elevator" example demonstrates that we should not make all methods virtual, since it exposes extension points where we do not want them. Let’s have a look at the reason this particular extension point is not wanted.

The elevator.Up() method has responsibilities for moving the elevator up and  closing doors etc. These responsibilities need to be chained in the right order to function safely; we do not wish client code to break the desired behaviour by inadvertently calling Up() at the wrong time.

This seems to highlight a smell that the single responsibility principle is not being adhered to in the elevator class. Since this issue is cause by incorrect ordering of multiple responsibilities. By adhering to SRP, separating out the different behaviours from the responsibility of coordinating them, we can avoid this conflict. We can then keep this method as virtual, and benefit from an additional extension hook safely.

Explicit Contracts

Paraphrasing another point made (I hope Ward will correct me if this is a misrepresentation) is that we may want to be very explicit about the points in which an application can be extended. Marking a method as virtual is a way of explicitly showing your "approved" extension hook.

Arguably, I prefer to see my "extension hooks" more explicitly than a virtual method. As an application developer looking for a "seam" to extend, I would favour seeing an interface that I can implement, over inheriting from a class and overriding it’s virtual methods. This is mainly because to interfaces tend to be more discoverable than virtual methods.

Open/Closed Principle

Lastly, I have a different perspective on the OCP. I believe "closed for modification" to be in reference to the actual LOC contained in the class rather than "closing off" modifications to behaviour. I agree however, that making all methods virtual does open up a greater susceptibility to the violation of LSP.

Summary

The arguments made against methods being virtual as default mostly seem to fit under the umbrella of protecting yourself against unwitting developers. I think there are safer ways to do this whilst still obtaining the flexibility provided when all methods are virtual by default.

Perfect is the enemy of the good

I’ve spent the last few evenings catching up on some of the posts on JP Boodhoo’s blog  (I’m still gutted that I can’t get the go-ahead from work to attend his Nothin’ but .NET Developer Boot Camp). In one of his posts, JP touches upon an issue that really plagues me; the trap of perfectionism.

I’ve often caught myself spending longer than appropriate trying to get something done “right” first time, when it would be far more pragmatic to try the simplest thing that works then refactor. If you catch me doing this (which won’t be hard, I think I do it a lot), you have my permission to slap me!

I think the trick is to making this work is doing “the simplest thing” rather than “the first thing that springs to mind that might work”. It’s also important to make small steps and refactor for this approach to work, keeping technical debt to a minimum.

It’s definitely something I need to work on.

MS Stubs framework

A colleague of mine recently came across Stubs – a Lightweight Stub Framework for .NET and suggested I take a look. Having used both Moq and Rhino Mocks in the past, I was intrigued, and thought I should take a closer look.

I may have gotten the wrong end-of-the-stick, but it seems that Stubs uses code-generation to generate stubs, rather than the more typical dynamic code approach used by other frameworks (reflection and expression trees).

My initial thoughts are:

  • I wonder how well the code-gen approach works with TDD? I don’t fancy having to re-run a tool manually every time I create a new interface; the less friction the better!
  • I generally would tend to favour dynamic code (reflection, expression trees etc) over code-gen unless performance is a key-factor, since this results in less code to maintain. It may be the case that code-gen here can make a significant reduction in execution time of tests in larger code-bases, although it would be interesting to see some stats for this. It would also be interesting to see whether this approach works out favourably on a long-running project (where interfaces are likely to change) as I can see maintenance of the stubs being a potential issue.
  • It’s good to see Microsoft favouring type-safe code rather than using magic strings as they had in other projects (like asp.net MVC)

 

I seems like this could be an interesting project to keep an eye on, but for the mean-while at least, I think I’ll stick to Rhino Mocks!

Interesting post from Oren…‏

http://ayende.com/Blog/archive/2009/06/09/avoid-externalizing-decisions-from-your-domain-model.aspx

Extract:

Boolean methods worry me because of their implications. If you have a Boolean method here, it means that somewhere else you have an if statement that works based on this method.

The “legacy” application I took ownership of when starting at my current employer is teeming with boolean methods – UserAccount as an example of this; IsSuspended, IsActive, IsAuthenticated etc. Funnily enough if I “Find Usages” they tend to have two types of usage; making decisions as to what to display in the UI, and validating whether an action can take place.

Oren hasn’t suggested his preferred approach to tackling this kind of problem but I would favour:

a) having these boolean methods moved out of my entity, and onto a view model for when I need this data when binding a view (my repositories can retrieve “suspended” or “not-suspended” users based on specifications).

b) when boolean properties (like IsActive) are used to determine whether or not to display a link to an action, based on the state of the domain (like  re-activate User for inactive accounts) then I’d prefer this responsibility to be delegated to to my application layer in a class responsible for application co-ordination, rather than having this logic my domain.

What do you think?

I’ve learnt so much from…

…reading blogs. This originally started with me stumbling along Los Techies (I believe it was a post by Jimmy Bogard), and adding this to my favourites. After doing the same for several other bloggers, this became unmanageable and a colleague of mine pointed me in the direction of the popular feed-reading service Google Reader.

I imagine that most people reading this right now probably will have their own RSS-feed aggregator, but if they haven’t, I would definitely recommend Google Reader as it makes it very easy to keep up with new posts. I currently use the Reader application on my iPhone while on the train to/from work, so I can us this “dead-time” productively.

According to Google Readers “trends” feature, on average I read 12 posts a day from my 53 subscriptions. A large amount of these seem to be posted by Oren aka Ayende Rahien – I have no idea how he manages to post so often, especially considering his posts are usually very thought provoking.

Anyway, the reason I’m writing all this was to share links to a few of the blog-authors I feel I’ve learnt the most from (you can see my Google Reader shared items here). These guys are super-smart (I mean clever rather than well dressed), and almost every post I read from them is a gem.

In no particular order:

  • Rob Conery – A fantastic, usually very entertaining writing style, with great content, especially on ASP.NET MVC. I’m also very jealous that he lives in Hawaii.
  • Jimmy Bogard – Every post is very clearly written and well thought out. A definite must-have on your feed-reader. Content generally covers things like SOLID principles, Agile development, and Doman-Driven Design. Author of to OSS tool AutoMapper.
  • Grey Young – A very inspiring blog, which I will read over-and over again. Content usually covers: DDD (specifically distributed domain-driven design), design patterns and agile development.
  • Jeremy Miller – I’ve got a lot of respect for this guy. He’s clearly a very intelligent chap. His blog often includes writing about: Software Architecture, Design Patterns, Test Driven Development, and Alt.Net. Author of the StructureMap IoC tool and FubuMVC.
  • Chad Myers – Chad’s posts are always very cohesive and well written, and along with Jimmy Bogard was one of the first blog-authors to inspire me with their work. His blog often covers topics like TDD, SOLID principles, and ALT.NET. Author of FubuMVC
  • Jan Van Ryswyck – Having only found Jan’s blogs on Elegant Code quite recently, it says quite a lot that he’s made it into my top list of blog-authors. His recent posts on fluent interfaces are typical of the inspirational pots he writes.
  • Ian Cooper – Ian’s blog contains some great material on persistence ignorance, nhibernate, TDD and BDD, and Linq. Less active than the bloggers above, but certainly worth adding to your feed-reader.

Other very good blog-authors to check out:

 

Whose blogs would you recommend?

Active Blogger?!

Hmm. Perhaps not.

Over the last four months (has it really been that long?!) I’ve been very quiet in terms of blogging. Silent in fact. Over this time a lot has happened for me – I changed jobs (I now work for a company in London), I relocated from Bournemouth into “the city”, and then went on holiday…for a month and a half!

I have however been itching write a few posts and have a couple in mind that have yet to make it into digital form. Before I do though, I wanted to make some sample code available from my previous post. Although this code was really just a spike for me to try out the Entity Framework POCO Adapter, hopefully it may be of some use to someone.

The “Implementing a Customer Search Service with DDD, BDD and the Entity Framework” sample can be found on my skydrive here: http://cid-811a86b9c84017e5.skydrive.live.com/browse.aspx/Public

 

*DISCLAIMER* Please don’t consider this sample as anything more than just a spike – the code is quite far from best practice (for a start, I attempted to use an IOC container in my tests, YUCK!). *DISCLAIMER*

Pluggable Dependencies Intermission

Wow, its been over a month since I posted last on this, and what a month it’s been. Over the last month I uncovered something pretty special: Domain-Driven Design by Eric Evans. Seriously, buy this book. Every developer should own a copy of this. Fact.

Right, now I’ve gotten that out of my system, I’ll continue with what I was planning on writing about. Something was up about my implementation. Something didn’t seem to smell right. I couldn’t put my finger on in, but I found myself coding myself into corners. I found myself coding unit tests that didn’t seem to be testing the right “layer” of code, and found myself getting mixed up in issues like “how to lazy-load collections” when really I knew that this persistence related code didn’t sit right amongst my business logic.

So, I did some research, and some reading, and here’s what I found…

Code Smells

When writing my first unit test for the problem in hand I made a few assumptions; first that the repository would be programmed against an abstraction (IPerson rather that Person) and second that the repository would return an IQueryable of Person objects. On reflection, perhaps I did this because I had an implementation in mind, or perhaps this was for another reason entirely, but I believe this lead me to a few issues and code smells.

First of all, I breached the YAGNI principle – I wasn’t trying to filter this list of people (or Persons), or at least, not at this stage of the game anyway, so I shouldn’t have been trying to return an IQueryable when perhaps an IEnumerable collection would have better represented the task in hand.

Secondly, I breached the purpose of the repository, to obtain a reference to the aggregate root, which in this case, should have been a collection of Person objects. What I actually returned was a IQueryable object; a query nothing else. Rather than try to explain why I believe this to be an issue, I think this is much better summed by Fredrik Normén in his post here. I really think Frederik hit the nail on the head in that a Repository should behave as a collection of entities, and to call a mechanism that serves up a query a “repository” goes against the original definition of what Martin Fowler and Evans intended.

I now think a better option to meet the goal of filtering a list of entities, or rather specifying the criteria a list of entities must adhere to in order to be returned by the repository (should I require this), would be to use the specification pattern. We can then compose our query using well defined domain concepts (like MandatoryPersonDetailsSpecification), chain specifications together if required (And/Or/Not) and then pass them into our repository. An example of this that I particularly like can be found on Scott Walkers blog here.

Code Responsibilities

I paused for thought when I realised I was writing unit tests that seemed to be testing the wrong “layer” of code. This was the code that sounded the alarm bells in my head:

[TestMethod]
public void Person_ShouldHave_Forename_Surname_Address_Fields_NotNull()
{
    var p = (from persons in rep.Repository<IPerson>()
                select persons).SingleOrDefault();

    Assert.IsNotNull(p.Forename);
    Assert.IsNotNull(p.Surname);
    Assert.IsNotNull(p.Address);
}

The code in question is testing that the repository is returning values for Forename, Surname and Address…but surely it’s not the repositories responsibility to enforce our business rule that a person must have these attributes?  I wanted to ensure that “draft” person objects could be captured in our domain, but full person details must be provided in order for a person to be “published”. So where was I going wrong?

Lets consider the facts – the concept of a “draft” person is a domain concept; the rules enforcing this should therefore be contained in the domain. I had previously considered this as a persistence only concern, however I had overlooked the fact that the rules defining whether a person was “complete” or “draft” have to exist in the domain.

I no longer necessarily require a “draft repository” for persisting temporary data; a draft person object (or a person deemed to be in a draft state) is more suitable in this context.

Lastly, in regard to lazy loading, my hunch is that by carefully considering where my aggregate boundaries are, I shouldn’t need to worry about lazy loading, especially considering this is such a basic example.

Where do I go next from here?

What this really drove home to me is that the first few hours of TDD really are the most painful. I’m going to stick with it though, and using that in conjunction with my new favourite book, I should be a few steps closer to writing more elegant code.

:)

How I Got Started In Software Development

This post is another development meme that I’ve seen crop up on a few blogs I subscribe to – the basic gist being that you answer a bunch of questions about the origins of your interest/career in software development, and then pass on the meme to others who you’d be interested in finding out more about! I was passed this meme by Scott Lovegrove, so thanks to him for this!

1. How old were you when you started programming?
My first experience with “programming” was when I was in Sixth Form where I wrote a basic Fantasy Football application using Microsoft Excel macro language (if you can really call that programming!!) , so I must’ve been about 16.

2. How did you get started in programming?
Subsequent to my brush with very basic programming in Sixth Form, I didn’t really get started until I started my degree at University (Computing/Software Engineering Management).

3. What was your first language?
Hmm…excluding Microsoft Excel macro language, the first language I coded with was C.

4. What was the first real program that you wrote?
Wasn’t everybody’s first application Hello World?! If I remember rightly, my first university program was a nightclub admission application – tracking the movement of clubber’s from entry, to the dancefloor, to the VIP area…something like that anyway!

5. What languages have you used since you started programming?
In no particular order: HTML, javascript, XSLT, XPath, UML (if modelling languages count?!), C, Java, C#, ASP, ASP.NET, .Net 2, 3.0 and 3.5, Perl, PHP, SQL (for both SQL Server and Oracle), and both the BNF and VDM specification languages.

6. What was your first programming gig?
My first programming job was my placement year at IQSystems Services Ltd, working as a Junior Software Developer on the IQUtopia product – a patient management solution, designed to support care provided outside the acute hospital environment..

7. If you knew then what you know now, would you have started programming?
Sure thing, I love it!

8. If there is one thing you learned along the way that you would tell new developers, what would it be?
Don’t try to re-invent the wheel. 9 times out of 10, the solution that you are looking for has already been considered in depth, and will be out there should you need it. As Scott said, Google is your friend. This applies to software design, as much as it applies to implementation so consider reading up on design patterns too!

9. What’s the most fun you’ve ever had… programming?
I generally get a kick out of solving complex business problems with software (rather than solving technical problems with software), so I think I’d say any of the examples where clients have turned around and thanked me for my efforts an input. I guess apart from that, I’ve generally had a lot of fun “playing with” the .NET 3.5 extensions over the period where it was in beta…particularly Dynamic Data, ADO.NET Data Services (Astoria).

 

So to continue this meme, I need to pass this on to other developers…

I’m not sure if any of my colleagues have a blog, but if not, they could start their blog with this! ;)

I would nominate: Rowan Dennigan, Tom Dudfield and Dave Masters.

Next Page »