Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC, Spring.NET, NHibernate initial setup/example/tutorial

Have you been doing some ASP.NET MVC developement involving Spring.NET and NHibernate both? I would like to see an informative example of such setup, so I could build my own project off that.

I tried googling, found some pretty things like S#arp Architecture, an article about regular ASP.NET (WebForms) integrated with the frameworks and so on. Still, I'm missing a good tutorial on ASP.NET MVC & the subj.

P.S.: I do know how Spring and Hibernate works, I just need to plug them into an MVC application. Don't want to use S#arp Architecture by now.

P.P.S: I'll update the links later, including this one:

like image 674
Bubba88 Avatar asked Mar 11 '10 15:03

Bubba88


2 Answers

NHibernate configuration is no different to a Spring.Net webforms app. Add the OpenSessionInView module to web.config and define a session factory named SessionFactory in the spring config.

Spring.Net and MVC integration is done by registering a custom IControllerFactory in application startup, this applies a custom ControllerActionInvoker. The controller factory creates or configures controllers and the action invoker configures any ActionFilter.

public class MvcApplication: System.Web.HttpApplication
{
    public static void RegisterRoutes( RouteCollection routes )
    {
        //
    }

    protected void Application_Start()
    {
        RegisterRoutes( RouteTable.Routes );

        lock (this) {
            ControllerBuilder.Current.SetControllerFactory( new SpringControllerFactory() );
        }
    }
}

public class SpringControllerFactory: DefaultControllerFactory
{
    public SpringControllerFactory()
    {
        SpringContext = WebApplicationContext.Current;
    }
    protected override IController GetControllerInstance( Type controllerType )
    {
        IController controller = null;
        if (SpringContext.ContainsObject( controllerType.Name )) {
            controller = (IController) SpringContext.GetObject( controllerType.Name );
        }

        if (controller == null) {
            controller = base.GetControllerInstance( controllerType );
            SpringContext.ConfigureObject( controller, controllerType.FullName );
        }

        var standardController = controller as Controller;
        if (standardController != null) {
            standardController.ActionInvoker = new SpringActionInvoker();
        }

        return controller;
    }

    private IApplicationContext SpringContext
    { get; set; }
}

public class SpringActionInvoker: ControllerActionInvoker
{
    public SpringActionInvoker()
    {
        SpringContext = WebApplicationContext.Current;
    }
    protected override FilterInfo GetFilters( ControllerContext controllerContext, ActionDescriptor actionDescriptor )
    {
        var filterInfo = base.GetFilters( controllerContext, actionDescriptor );

        foreach (IActionFilter filter in filterInfo.ActionFilters.Where( f => f != null )) {
            SpringContext.ConfigureObject( filter, filter.GetType().FullName );
        }

        foreach (IAuthorizationFilter filter in filterInfo.AuthorizationFilters.Where( f => f != null )) {
            SpringContext.ConfigureObject( filter, filter.GetType().FullName );
        }

        foreach (IExceptionFilter filter in filterInfo.ExceptionFilters.Where( f => f != null )) {
            SpringContext.ConfigureObject( filter, filter.GetType().FullName );
        }

        foreach (IResultFilter filter in filterInfo.ResultFilters.Where( f => f != null )) {
            SpringContext.ConfigureObject( filter, filter.GetType().FullName );
        }

        return filterInfo;
    }

    private IApplicationContext SpringContext
    { get; set; }
}

Mvc Contrib has a similar SpringControllerFactory, though it does not configure action filters. It is configured in application startup:

    protected void Application_Start()
    {
        RegisterRoutes( RouteTable.Routes );

        lock (this) {
            ControllerBuilder.Current.SetControllerFactory( new SpringControllerFactory() );
            SpringControllerFactory.Configure( WebApplicationContext.Current );
        }
    }
like image 145
Lachlan Roche Avatar answered Oct 19 '22 08:10

Lachlan Roche


For nhibernate have a look at Stephen Bohlen's webcasts Summer of Nhibernate and Autumn of Agile.

Personally I haven't used Sprint.net but this screencast I found useful to get a general overview. Fredrik normen also has a post on asp.net MVC and spring.net.

like image 40
Nathan Fisher Avatar answered Oct 19 '22 10:10

Nathan Fisher