ASP.NET MVC、Spring.NET、NHibernate 初始设置/示例/教程

发布于 2024-08-24 08:46:56 字数 342 浏览 10 评论 0原文

您是否做过一些涉及 Spring.NET 和 NHibernate 的 ASP.NET MVC 开发?我希望看到此类设置的信息丰富的示例,这样我就可以以此为基础构建自己的项目。

我尝试谷歌搜索,发现了一些漂亮的东西,比如 S#arp Architecture,一篇关于常规 ASP.NET (WebForms) 与框架集成的文章等等。尽管如此,我仍然缺少有关 ASP.NET MVC 和 ASP.NET MVC 的优秀教程。主题。

PS:我确实知道 Spring 和 Hibernate 是如何工作的,我只需要将它们插入到 MVC 应用程序中即可。现在不想使用 S#arp 架构。

PPS:我稍后会更新链接,包括这个:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慈悲佛祖 2024-08-31 08:46:56

NHibernate 配置与 Spring.Net Webforms 应用程序没有什么不同。将 OpenSessionInView 模块添加到 web.config 中,并在 spring 配置中定义一个名为 SessionFactory 的会话工厂。

Spring.Net 和 MVC 集成是通过在应用程序启动时注册自定义 IControllerFactory 来完成的,这将应用自定义 ControllerActionInvoker。控制器工厂创建或配置控制器,操作调用程序配置任何 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 有一个类似的 SpringControllerFactory,尽管它没有配置操作过滤器。它是在应用程序启动时配置的:

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

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

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 );
        }
    }
执手闯天涯 2024-08-31 08:46:56

对于 nhibernate,请查看 Stephen Bohlen 的网络广播 Summer of Nhibernate敏捷之秋

就我个人而言,我没有使用过 Sprint.net,但我发现这个 screencast 对获得总体概述。 Fredrik Normen 还在 asp.net MVC 和 spring.net

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.

痴意少年 2024-08-31 08:46:56

我也很难找到 NHibernate、Spring.NET 和 ASP.NET MVC 示例。我找到了 Spring.NET/NHibernate Northwind 示例(使用 WebForms 模式),但找不到在我的模型中使用 NHibernate DAO 的简单方法。

最后,我找到了 这个,发现它非常有帮助。

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.

酒绊 2024-08-31 08:46:56

我终于成功地为我的 ASP.NET MVC 应用程序提供了 Spring.NET 设施。只需编写一个自定义控制器工厂(足够简单),然后,给定一个全局 Spring 上下文(我手动创建),我就可以拉出我的控制器并执行我之前所做的任何操作。

一些有用的链接,但不是全部:
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

从那时起,与 NHibernate 的集成一定非常简单:)

PS:问题是:a) MVCContrib 现在似乎否认对 IoC 容器的需要,因为我听说过一些事情关于 ASP MVCContrib 中的 IoC/DI 弃用。我想,这就是为什么我无法让他们的 SpringControllerFactory 工作(与 Spring 的 WebSupportModule 一样)

b)有一些文章考虑了所需的集成,但是从某种意义上说,它们似乎都是原始的(也许是因为其中许多只是提供了不起作用的解决方案?:))

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文