Command Handlers

The command pattern is one of the patterns defined in the gang of four's behavioural patterns but I rarely see it get much use. The idea behind the pattern is to encapsulate all of the information required to execute an action within a single object or class: public interface ICommand { void Execute(); } public class AdmitPatient : ICommand { string firstname; string surname; public AdmitPatient(string firstname, string surname) { this.firstname = firstname; this.surname = surname; } public void Execute() { // do something here } } Now the reason I want to highlight this pattern is because of something I've been seeing in a lot software projects. That's…

Read More

Have we forgotten what constructors are for?

Bit of a sensational title there, but I do think it's true. Believe it or not constructors are not just for injecting dependencies into a Controller or a Service. They actually serve another purpose as well. Making sure that we can't construct an object in an invalid state. I often see code like this in a project: public class Vehicle { public string Registration { get; set; } public string Colour { get; set; } } I'll probably find Entity framework in the same project as well, which is actually one of the libraries I blame for this epidemic :). Can you have a vehicle without a…

Read More

Time to cast away Visual Studio and use a text editor

I've been working on an OSS project for the past couple of weeks that I'm really excited about. It's a project that is going to allow you to effectively write .Net code outside of Visual Studio and it's called Omnisharp. To give a little bit of background It all started as an effort from a guy called @JasonImison and his frustrations with not being able to write C# code in his favourite editor, VIM. And with the help of NRefactory he was able to write a local http service that, when given a C# project would provide you with functionality…

Read More

Passing an object to a generic method

There's nothing complicated about this. But for some reason the solution escaped me. You know how it is. You have an idea in your mind and it just gets more and more complicated. Then somebody comes along and shows you something really simple. So you may know how to do this. You might not. But I found it interesting enough to blog about. Imagine you want to invoke a method defined as below: public void Execute<T> Generic(T foo) where T : class { var bar = new SomethingElse<T>(foo); .... } Except you don't know the object type…

Read More

Entities should look after their own events

I've been playing with CQRS and ES a lot recently and to be fair I should have started blogging about my experiences before now. But better late than never. This week I've been looking more carefully at how to manage the events that get applied to an entity within my aggregate root. When you first start your journery into CQRS/DDD/ES it won't be long before you come across the term aggregate. To quote Martin Fowler: A DDD aggregate is a cluster of domain objects that can be treated as a single unit. An example may be an order…

Read More

This Code is Bad and you should feel Bad

This Code is Bad and you should feel Bad

Want to mark some code as bad in your code base? Somebody written some poor code and you want to point it out? Need to mark some code to be reviewed? Well this is the attribute for you! [AttributeUsage(System.AttributeTargets.All, AllowMultiple = true, Inherited = true)] public class ಠ_ಠAttribute : Attribute { public ILog Log { get; set; } public ಠ_ಠAttribute() { Log.Info("This code is bad and you should feel bad"); } } [ಠ_ಠ] public class SomeClass { } This code was supplied to me by a JabbR friend called Ben Hyrmn (and I'd link him on twitter if he hadn't absconded!…

Read More

ASP.NET you're doing it wrong. Part Two: The Views

ASP.NET you're doing it wrong. Part Two: The Views

In my previous post I spoke briefly about how to setup routes and handle route arguments etc. In this post I'm going to run through the various options you have for returning and presenting your data in views. If you haven't already I suggest you read the first part of this series. Returning views and the Convention Just as a refresher, a simple get method that returns a string, is defined as follows: Get["/"] = _ => { return "Hello World!" }; To change this to return a view instead is easy. The NancyModule base class has a property of…

Read More

Why I'm no longer giving up on C#

For while now I've been getting bored and annoyed with C#. Its not that its a bad language, its just that when you look around the web there seems so many other languages that seem so fluent to write. NodeJs, Ruby, Javascript, Cloujure. These languages are all the rage at the moment and with good reason. Recently I've been playing around with all these languages trying to find a replacement for the C# that I've grown to hate. However, I realised something (and to be fair its pretty obvious). Its not C# the language that I've found frustrating but the…

Read More