ASP.NET MVC3 EditorFor that looks for inheritance

Today I ran into issue with MVC 3 where a view with a model type TModel won’t check to see if the model’s instance is a subclass of TModel, resulting in any attributes on overridden properties being ignored. This is because the metadata is read from the type specified in the view, and not the actual instance. For example, with the following class structure and view will never render “SomeProperty” as “required”, even if the model is an instance of InheritedClass. Classes: public class BaseClass { public virtual string SomeProperty { get; set; } } public class...
read more

OrderBy a string in Entity Framework

I’ve just started using Entity Framework, and I’ve found that it really helps speed up development time when dealing with queries where performance isn’t a huge issue. I added to an existing project which needed to keep the EF classes isolated in the data layer. This made it difficult to pass in a sort order without having a massive switch statement to deal with each of the options. I solved this by building an OrderBy extension method that takes a string, and builds the sort expression using reflection. This means you can sort without having to know the actual type of what...
read more

Simple Events in JavaScript

A couple of weeks ago I posted about C# like Properties in JavaScript. I wanted to explore how properties could possibly be added into JavaScript, and hopefully offer some insight into how some frameworks might work. Another feature that JavaScript doesn’t explicitly support is events. Though using some of the same techniques I used for events also work very well for building events. Most implementations of events in JavaScript use optional arguments in order to provide two overrides to an “event” function, one for adding a listener and the other for raising the event, like this: //...
read more

Detecting Encoding in C#

On a project that I’ve been working on recently, I was having some trouble combining SQL scripts that where in a couple of different formats. While there’s no easy way to detect all of the possible encodings, by checking the byte order mark (BOM) there is a pretty straight forward way to detect the following encodings: UTF-16 UTF-16BE UTF-32 UTF-32BE UTF-8 public static Encoding GetFileEncoding(string path){ if (path == null) throw new ArgumentNullException("path"); var encodings = Encoding.GetEncodings() .Select(e => e.GetEncoding()) .Se
read more

Properties in JavaScript

JavaScript doesn’t inherently support properties in the same way that .net languages do, which is a shame since the syntax really helps simplify access internalized data in a structured way. There’s a common solution to this in JavaScript, that exploits the fact that JavaScript function arguments are optional. A property function will have a single argument. If the argument is undefined then the current call is a ‘get’, otherwise it’s a ‘set’. jQuery has many good examples of this, for example the val() method: var value = $('input').val(); //...
read more

« Previous Entries