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

4

7

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:

Jeremiah answered 11/3, 2010 at 15:5 Comment(2)
BTW, I wonder if S#arp Architecture using other DI by default could be changed to use Spring.NET :)Jeremiah
Why not, IoC container is not a major part of S#arp. It uses ServiceLocator in many places, so you only need to change components registration.Jannajannel
M
5

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 );
        }
    }
Meganmeganthropus answered 21/3, 2010 at 17:12 Comment(0)
K
1

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.

Kellum answered 18/3, 2010 at 22:55 Comment(0)
C
1

I too was having a hard time finding a NHibernate, Spring.NET and ASP.NET MVC example. I found the Spring.NET/NHibernate Northwind example (uses WebForms pattern), but could not find a simple way to use NHibernate DAO's with my model.

Finally, I found this one and found it to be very helpful.

Corycorybant answered 19/7, 2011 at 18:48 Comment(0)
J
0

I've finally managed to provide my ASP.NET MVC application with Spring.NET facilities. Just wrote a custom contoller factory (simplistic enough), then, given a global Spring context (which I create manually), I can just pull out my controllers and do whatever I did before.

Some useful links, but not all: http://www.pnpguidance.net/Post/SetDefaultControllerFactoryIControllerFactoryASPNETMVCFramework.aspx

http://weblogs.asp.net/seanmcalinden/archive/2010/01/13/custom-ioc-container-for-dependency-injection-with-an-asp-net-mvc-website-usage-example.aspx

Since then, integration with NHibernate must be pretty straightforward :)

P.S.: The problem is: a) MVCContrib seems to be denying the need for IoC containers now, cause I've heard something about IoC/DI deprecation in ASP MVCContrib. I guess, that's why I couldn't manage to get their SpringControllerFactory work (as usual with WebSupportModule of Spring's)

b) There are some articles, considering the required integration, but they all seem to be.. raw in some sense (maybe because many of the just provide the solutions that do not work? :))

Jeremiah answered 12/3, 2010 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.