ASP.NET MVC

ASP.NET MVC Spark Support

ASP.NET MVC 2 supports Spark!

What? hear me out. The release of ASP.NET MVC 2 includes an amazing hidden gem. You see the “Add View/Controller” dialog now takes into account a T4 directive “output extension”.

Getting started with Custom CodeTemplates

To get started with Custom CodeTemplates check out this excellent article by K. Scott Allen

Getting started with Custom CodeTemplates and Spark

  1. At the top of your templates add the output extension as shown below.
  2. Modify the template to use spark.

image

Tags: ,

Thursday, March 11th, 2010 ASP.NET MVC, Spark No Comments

Spark View Engine 1.1

ASP.NET MVC 2 was RTM’ed today and with the help of Jay Harris@jayharris we upgraded our rather large codebase to not only ASP.NET MVC 2 RTM but also a new drop of the Spark View Engine built against the RTM.

Since the release of Spark View Engine 1.1 RC a few weeks ago there have been some minor bugs/enhancements identified but no blockers. The update to RTM was nothing more then changing the assembly reference, build number and running the build script.

These changes are not yet reflected in on the GitHub source. I’m sure there will be a pull request or Lou@loudej will update master pretty quickly.

Head over to CodePlex to download the new release and of course thanks to Lou for building an amazing tool.

Tags: ,

Thursday, March 11th, 2010 ASP.NET MVC, Spark No Comments

Spark Binaries For ASP.NET MVC RC2

THIS IS NOT AN OFFICAL RELEASE…YET

I spent the evening updating the spark source to support ASP.NET MVC RC/RC2. Well, actually I simply applied the changes that were posted to either the mailing list or codeplex.

All the tests pass:

image

Download it here:

Binaries: Spark-1.0.1.0-release.zip

Source: Spark-1.0.1.0-source.zip

This post, hopefully, won’t be up long.

Tags: ,

Tuesday, February 9th, 2010 ASP.NET MVC, Spark 4 Comments

ASP.NET MVC2 To Provide Real Spark Support

Ok, that might be going a little too far but it sounds like the barrier I wrote about a few weeks ago might be going away.

Earlier this evening Phil Haack posted this tweet:

image

Excited to hear this I asked a leading implementation question about honoring the output extension directive in T4 templates.

image

Us old-timer remember the almost year before ASP.NET MVC 1.0, when we had not tooling support inside visual studio. Right before 1.0 was released we got right-click view/controller support and it made a world of difference. I honestly believe that without the GUI integration, that pushed us to use strong-typed views, MVC would have taken longer to get into less progressive shops.

I’m really excited about MVC2 …but it still needs a logo.

image

image

Tags: ,

Thursday, January 21st, 2010 ASP.NET MVC, Spark 2 Comments

SOA+MVC

Disclaimer: This post is my personal reflection of a current situation. It’s not intended for any other use. Don’t use this information in anyway.

Backgroundimage

The previous enterprise-grade SOA architected systems I’ve worked on, last decade, were Classic ASP, Java or Webforms. These systems had their presentation layer wired directly into the services. The largest, .NET or other, MVC application I worked with was huge MVC but it had a “traditional” connection to the database.

 

First thoughts on SOA+MVC

Tags: ,

Tuesday, January 19th, 2010 ASP.NET MVC No Comments

ASP.NET MVC: Not That Open Source

ASP.NET MVC has one small hook into Visual Studio, the ability to right click and add views and controllers.

image

This feature is small, hell since I’ve been using spark I almost forgot about it, that is until I started working on a simple administration section. The application has enough entities that “Right Click –> Crud” would make just the right tool.

image

Idea

ASP.NET MVC provides customizable code templates for the view/controller content. There are a few really good articles on how to get started.

Plan

  1. Include the CodeTemplates in my project
  2. Change the output extention to .spark
    1. image
  3. Modify the markup to sparkup
  4. Phase 3: Profit.

Problem

ASP.NET MVC uses a custom tool to process the T4 templates.

MvcTextTemplateHost

It ignores the output extension directive in T4 templates.

(Click play)

The Price is Right Losing Horns sound bite

Sadness

MvcTextTemplateHost is included in Microsoft.VisualStudio.Web.Extensions.dll and this DLL that is not “part” of MVC code that was open sourced.

Conclusion

I’d love to write a Visual Studio addin to create spark views but I just don’t have the time. However; I do wonder if I can just have spark parse the .aspx and .ascx files.

Tags: , ,

Monday, January 11th, 2010 ASP.NET MVC, Open Source, Spark, mvc 2 Comments

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);}

Tags: , , ,

Thursday, November 26th, 2009 ASP.NET MVC, DataAnnotations, ModelBinders, TDD No Comments

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.

Tags: ,

Monday, November 23rd, 2009 ASP.NET MVC, mvc No Comments

ASP.NET MVC Routing domain.com/*

Scenario; you have to pass wildcard requests to a controller for processing. Think myspace.com style vanity names.

Solution: Not as nice as .htaccess but it works; and I think I’m doing it write.

Step 1: Add Routes to Global.asa

   1:  routes.MapRoute(
   2:      "Default", 
   3:      "{controller}/{action}/{id}", 
   4:      new { controller = "Home", action = "Index", id = "" },
   5:      new { controller = new Constraints("Account", "User", "Home") }
   6:  );
   7:   
   8:  routes.MapRoute(
   9:      "User", 
  10:      "{id}", 
  11:      new { controller = "User", action = "Index", id = "" }
  12:  );

Step 2: Create Class that implements IRouteConstraint

 

   1:  public class Constraints : IRouteConstraint
   2:      {
   3:          private string[] _matches = null;
   4:   
   5:          public Constraints(params string[] matches)
   6:          {
   7:            _matches = matches;
   8:          }
   9:   
  10:          public bool Match(HttpContextBase httpContext, 
  11:                              Route route, 
  12:                              string parameterName, 
  13:                              RouteValueDictionary values, 
  14:                              RouteDirection routeDirection)
  15:          {
  16:            bool foundMatch = false;
  17:            foreach (string match in _matches)
  18:            {
  19:                if (String.Compare(values[parameterName].ToString(), match, true)
  20:                        == 0)
  21:                {
  22:                    foundMatch = true;
  23:                }
  24:                 
  25:            }
  26:            return foundMatch;//not matching!
  27:          }
  28:      }

Tags:

Thursday, July 30th, 2009 ASP.NET MVC 1 Comment

Tools I want to use more

As a follow up to my tools post here is a list of the tools I’d like to spend more time with. The % next to the title is an estimation as the likelihood I’ll do any serious work with the tool/software/etc.

ReSharper – 20%

http://www.jetbrains.com/resharper/

Simply put, ReSharper is a must-have productivity tool for .NET developers. It fully integrates with Visual Studio to intelligently and powerfully extend the functionality that is native to Visual Studio. ReSharper provides solution-wide error highlighting on the fly, instant solutions for found errors, over 30 advanced code refactorings, superior unit testing tools, handy navigation and search features, single-click code formatting and cleanup, automatic code generation and templates, and a lot more productivity features for C#, VB.NET, ASP.NET, XML, and XAML.

NDepend – 10%

http://www.ndepend.com/

NDepend is a tool that simplifies managing a complex .NET code base. Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and master evolution by comparing different versions of the code.

C# – 70%

http://msdn.microsoft.com/en-us/vcsharp/default.aspx

NHibernate – 80%

https://www.hibernate.org/343.html

NHibernate is a port of Hibernate Core for Java to the .NET Framework. It handles persisting plain .NET objects to and from an underlying relational database. Given an XML description of your entities and relationships, NHibernate automatically generates SQL for loading and storing the objects. Optionally, you can describe your mapping metadata with attributes in your source code.

NUnit – 20%

http://www.nunit.org/index.php

NUnit is a unit-testing framework for all .Net languages. Initially ported from JUnit, the current production release, version 2.4, is the fifth major release of this xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages.


MEF – 60%

http://www.codeplex.com/MEF

The Managed Extensibility Framework (MEF) is a new library in .NET that enables greater reuse of applications and components. Using MEF, .NET applications can make the shift from being statically compiled to dynamically composed. If you are building extensible applications, extensible frameworks and application extensions, then MEF is for you.

DDD – 100%

Tags: , , , , ,

Search