Posts

Showing posts from February, 2013

Coupling properties with friendly names

Hey, For some time I've been looking for a good way to couple between the property, and it's display name. Some may say it's a bad practice, but I think sometimes it has it's advantages. So I solved it using DescriptionAttribute. The class is in System.dll, so we don't need to add any reference. Let's take a look on the following class: public class UserChecklistEntity { public bool TakeTheTour { get; set; } public int VisitsCount { get; set; } } Let's decorate the properties with DescriptionAttributes: public class UserChecklistEntity { [Description("Take the tour")] public bool TakeTheTour { get; set; } [Description("Visits count")] public int VisitsCount { get; set; } } And now for the interesting part, add this extension method: public static class ObjectExtensions { public static string GetDescription<T>(this T obj, Expression<Func<T, object>> expression) where T : class {

Extending cshtml with functions and properties

Hey, Lately I've been building many infrastructures for my company's website and I wanted to expose those infrastructures in the most natural way that coders are used to. One of the problems I spent time on was exposing the infrastructures on *.cshtml files. I didn't want to use static classes - because it's not a natural way, and not very trivial. So I managed to extend the class that represents the view, and by that add more properties and functions to it. public abstract class ViewBase<TModel> : WebViewPage<TModel> where TModel : class { public LayoutConfiguration LayoutConfiguration { get; private set; } public ViewBase() { LayoutConfiguration = LayoutConfiguration.GetInstance(); } public void DoWork() { } } And now on your Web.config (in the Views folder) replace this line: <pages pageBaseType="System.Web.Mvc.WebViewPage"> With this one: <pages pageBaseType="namespace.ViewBase&