views:

878

answers:

2

I'm starting a new project for work, and I decided I want to give MVC a shot. It's a small internal site for a commute challenge.

I want to use Spring.NET for Validation. I have used Spring.NET before in Web Forms, but with no code behind as in traditional ASP.NET, how do I use the Page Validation framework Spring.NET provides?

Edit 1:

In an attempt to try this myself, here is what I have:

Web.Config

<?xml version="1.0"?>
<configuration>
    <configSections>
     <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
      <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
     </sectionGroup>
    </configSections>
    <appSettings>
     <add key="RouteValidator" value="RouteValidator"/>
     <add key="UserValidator" value="UserValidator"/>  
    </appSettings>
    <spring>
     <context>
      <resource uri="config://spring/objects"/>
      <resource uri="~/Config/Spring.Web.cfg.xml" />
      <resource uri="~/Config/Spring.Validation.cfg.xml" />
     </context>
     <parsers>
      <parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" />
     </parsers>
    </spring>
    <system.web>
      <httpModules>
       <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
      </httpModules>
    </system.web> 
</configuration>

Spring.Web.Cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd"&gt;

    <description>
     Foo MVC Controller declarations.
    </description>

    <object id="HomeController" type="Foo.MVC.Web.Controllers.HomeController, Foo.MVC.Web"></object>
    <object id="AccountController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object>
    <object id="RouteController" type="Foo.MVC.Web.Controllers.RouteController, Foo.MVC.Web"></object>

    <object id="Spring.Web.UI.Controls.ValidationError" abstract="true">
     <property name="Renderer">
      <object type="Spring.Web.UI.Validation.IconValidationErrorsRenderer, Spring.Web">
       <property name="IconSrc" value="validation-error.gif"/>
      </object>
     </property>
    </object>

    <object id="Spring.Web.UI.Controls.ValidationSummary" abstract="true">
     <property name="Renderer">
      <object type="Spring.Web.UI.Validation.DivValidationErrorsRenderer, Spring.Web">
       <property name="CssClass" value="validationError"/>
      </object>
     </property>
    </object>

    <object id="standardPage" abstract="true">
     <property name="MasterPageFile" value="~/Views/Shared/Site.master"/>
     <property name="CssRoot" value="~/Content/"/>
     <property name="ImagesRoot" value="~/Content"/>
    </object>
</objects>

My validation file is very standard and basically a copy and paste from another project, therefore I didn't include it.

Now the problem I have is how do I use it? How do I get application context? My web forms project users Spring.Web.UI.Page, but I'm worried because the default pages in MVC derive from System.Web.Mvc.ViewPage, so that isn't going to work.

Or am I just not able to use Spring.NET's framework for MVC quite yet?

Thanks!

Thanks for any assistance.

A: 

You definitely can use Spring with ASP.Net MVC. You need to register that you are using it in the Global.ascx class then the framework will create Controllers based on what you have defined in your config file.

public class MvcApplication : System.Web.HttpApplication
{

    ...Routes stuff...

    protected void Application_Start()
    {
       ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));

       RegisterRoutes(RouteTable.Routes);    
    }
}
public class ControllerFactory : IControllerFactory
{
    public IController CreateController(RequestContext requestContext, string controllerName)
    {
        return IoC.Resolve<IController>(controllerName);
    }

    public void ReleaseController(IController controller)
    {
        //This is a sample implementation 
        //If pooling is used write code to return the object to pool 
        if (controller is IDisposable)
        {
            (controller as IDisposable).Dispose();

        }
        controller = null;
    }
}
public static class IoC
{
    static readonly IObjectFactory Factory
        = new XmlObjectFactory(new FileSystemResource
            (HttpContext.Current.Server.MapPath("~/Config/Spring.config")));

    public static T Resolve<T>(string name)
    {
        return (T)Factory.GetObject(name);
    }
}

Just make sure that the path to your spring config file is correct! This was adapted from this link.

On a wider note, this approach does not allow you to Spring the page classes, and being an MVC architecture, where views are pretty dumb classes, does not really support rich validation in the view itself in the manner you suggest. Look at either including the validation in the Model (post-back) in JQuery.

Colin Desmond
A: 

In best of my knowledge, the Spring Validation is not supported up to the latest release of ASP.NET MVC (1.0) and Spring.NET framework (1.3).

As far as incorporating Spring.NET with MVC, you can use MvcContrib project and come to the same code-base as posted by Colin Desmond (but you don't have to do the dirty work yourself).

Raj Arj