Coding out loud

Expressing the strange rhythms in my head

HtmlTag, it's the new TagBuilder

Hello again! Long time no see. Today’s post will be nothing but advertising for my latest and greatest open source library called HtmlBuilders. It’s even conveniently available as a Nuget package, so what are you waiting for? (All the cool kids are using it, so why not you?) Okay maybe I should elaborate a bit and tell you why you would want to use this. To do that, we must pay a small visit to the past: do you remember my earlier post about the TagBuilder in C#? You can go check it out, I’ll wait. ...

August 31, 2013 · 4 min · Alexander Moerman

Replacing method calls with expressions at runtime with Linqkit and the Entity Framework

LINQ to Entities does not recognize the method ‘…’ method, and this method cannot be translated into a store expression. Does this sound familiar to you? It’s a common issue that many developers face, as evidenced by the astronomical amount of questions about it on stackoverflow.com The problem To understand why this happens, you must first realize that Linq To Entities - the thing that produces the SQL statements for you - never really executes your C# code. So when you write: ...

March 9, 2013 · 11 min · Alexander Moerman

A generic approach to data tables with server-side processing

A little library that provides a generic way to use jQuery Datatables with ASP.Net MVC 4 Update to future readers: this code is no longer maintained! Use at your own risk, the Github repository should be seen as a pointer on how this can be done, not as a drop-in library to consume. This is going to be a long post. Don’t say I didn’t warn you! If you’re not one for long texts (but why are you reading blogs then?), today’s post in the ‘who-needs-hobbies-anyway’ series is all about https://github.com/amoerie/generic-datatables ...

March 8, 2013 · 6 min · Alexander Moerman

Applying the builder pattern to the .Net MVC TagBuilder

Sometimes I’m a nitpicker. Actually I’m a nitpicker 24/7, but I can’t really say that on a blog, can I? So today’s nitpicking is about C#’s TagBuilder. This class allows you to build html without writing a single ‘<’ or ‘>’. Using this class usually boils down to something like this: // this will create <button> var input = new TagBuilder("button"); // Set some attributes input.Attributes.Add("name", "property.name"); input.Attributes.Add("id", "property_id"); // some classes for markup input.AddCssClass("btn btn-primary"); // equivalent to jQuery's $("property_id").text("Click me!"); input.InnerHtml = "Click me!"; However, wouldn’t it be nicer if we could say: ...

February 25, 2013 · 5 min · Alexander Moerman

Multilanguage support in C# with code first and the Entity Framework

One of the downsides of living in Belgium is that everything needs to be available in multiple languages, given that we’re sporting no less than 3 official languages. This has consequences for developers too, and today I would like to expand a little on how to deal with these consequences. There is already decent language support in the most popular programming languages, such as resource bundles in Java and resources in .Net. However, a question that often goes unanswered is how to to translate dynamic resources. What if you want to maintain a list of Products where one product can have separate names in English, French, Dutch, etc. Furthermore, what if you want do the same thing for the product description? What if you want to dynamically add other languages later on? ...

January 11, 2013 · 10 min · Alexander Moerman

Automatic injection of models to nested Data Transfer Objects (DTOs) and vice versa with ValueInjecter

The DTO pattern is often used in multi-layered applications where data is transfered from one layer to another, where -preferably- we keep the data being transfered to a minimum. One of the issues I’ve ran into while applying this pattern is the use of nested DTO’s and automatic mapping from DTOs to models and vice versa. Allow me to clarify with an example: Suppose I have a .NET MVC application where I want do perform some simple CRUD operations on the following domain classes, Person and Address: ...

December 22, 2012 · 7 min · Alexander Moerman