开发者

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 l开发者_运维问答ater, including this one:


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


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.


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.


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? :))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜