Archive for November, 2009
Validating DataAnnotations
public static IList<String> GetClassLevelErrors(object instance){ return TypeDescriptor.GetAttributes(instance).OfType<ValidationAttribute>() .Where(attribute => !attribute.IsValid(instance)) .Select(attribute => attribute.FormatErrorMessage(string.Empty)) .ToList();}
[TestMethod]public void TestMethod1(){ var prop = typeof (DataAnnotationsModelBinderSpike.Models.Contact).GetProperty("First"); var attrib = prop.GetCustomAttributes(true).Cast<RequiredAttribute>().FirstOrDefault(); Assert.IsNotNull(attrib);}
Base Controller Class and TempData
Need:
To pass information to every view based on an environmental variable. The specific case I ran across this need is when you want to use the minified version of your JavaScript libraries in production and the human readable in dev/QA.
Solution:
- Create your own base controller class.
- Override either onactionexecuting or onactionexecuted.
- Populate TempData with the environment specific information.
- Change your controllers to inherit from your custom base controller
- Use that information in the view.
using System.Configuration;using System.Web.Mvc; namespace BaseController.Controllers{ public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.Controller.TempData["OnActionExecuting"] = "OnActionExecuting"; filterContext.Controller.TempData["js_dev"] = ConfigurationManager.AppSettings["js_dev"]; base.OnActionExecuting(filterContext); } protected override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.Controller.TempData["OnActionExecuted"] = "OnActionExecuted"; filterContext.Controller.TempData["js_prod"] = ConfigurationManager.AppSettings["js_prod"]; base.OnActionExecuted(filterContext); } }}
Pros:
- I’ve found that I often have to create my own base controller class. Doing so for something this simple is a god starting point.
Cons:
Alternate Solutions:
- Use T4 templates to generate a const file based on environment.
- Put the information of each server in the machine.config
Final:
The solution is sound and a sample is attached but I am going to have to come back and flesh out this post a little more with details about the *.config/caching and other options.
Help! Looking for a CSS DSL!
Update: Today Less CSS for .NET, or just .Less, was released. Its a port of the ruby LESS library and exactly what I was/am looking for. I updated my question on SO already.
—– old post ——
I posted this on stackoverflow.com already…..
I’ve been working on a really large project for almost 2 years and the client requirements keep changing. These changes, of course, effect everything and I would like to find a way to work with the CSS in a more dynamic fashion.
I assume I could get one of the ruby or python CSS DSLs running under ironRuby/Python but this client is very very particular about what software/frameworks are installed.
I have not found a CSS DSL where the base programming language is vb or c#.
reference: http://sandbox.pocoo.org/clevercss/ and http://nubyonrails.com/articles/dynamic-css
I guess another question is has anyone got either of these frameworks working under IronPython or IronRuby?
Unrecognized attribute ‘targetFramework’
Change the app pool to target the .NET 4.0 Framework…it defaults to 2.