使用 Spring.NET 和 FluentNHibernate 配置 ASP.NET MVC 2

发布于 2024-08-17 23:20:45 字数 2386 浏览 5 评论 0原文

我正在尝试配置 ASP.NET MVC 2 RC 和 Spring .NET 1.3 以使用 FluentNHibernate。

我已经设法让 FluentNHibernate 在控制台应用程序中运行。

目前 ASP.NET MVC 2 RC 和 Spring .NET 对我来说工作得很好,但我在配置 FluentHibernate 时遇到了麻烦。

在问这个问题之前我已经 Google 了很多,也浏览了 StackOverflow 上的相关问题。

我知道有这个(http ://www.bennymichielsen.be/post/2009/01/04/Using-Fluent-NHibernate-in-SpringNet.aspx)博客文章和 Spring .NET 文档 ORM 章节 (http://www.springframework.net/doc-latest/reference/html/orm.html)

就像 Benny 的博客建议我创建了“FluentNHibernateLocalSessionFactoryObject”一样,该类的内容与博客文章中的内容相同。

我的 Spring 配置文件如下所示:

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


  <object id="MySessionFactory" type="Project.Core.NHinbernate.FluentNHibernateLocalSessionFactoryObject, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="FluentNHibernateMappingAssemblies">
      <list>
        <value>Project.Core.NHibernate</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
        <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
      </dictionary>
    </property>
  </object>

  <object id="HomeController" singleton="false" type="Project.UI.Controllers.HomeController">
    <property name="MySessionFactory" value="MySessionFactory" />
  </object>
</objects>

我认为此配置缺少很多东西(连接字符串、正确的 SQL Server 2008 方言和数据库提供程序)。

我的目标是简单地将 SessionFactory 注入 HomeController 中。

如果您能指出我的配置文件中应该更改哪些内容,我将非常感激。

非常感谢!

I'm trying to configure ASP.NET MVC 2 RC and Spring .NET 1.3 to use FluentNHibernate.

I've managed to get FluentNHibernate running inside console application.

At moment ASP.NET MVC 2 RC and Spring .NET are working fine for me, but I'm having trouble configuring FluentHibernate.

Before asking this question I have Googled a lot, I have also looked through relevant questions on StackOverflow.

I know that there is this (http://www.bennymichielsen.be/post/2009/01/04/Using-Fluent-NHibernate-in-SpringNet.aspx) blog post and Spring .NET documentation ORM chapter (http://www.springframework.net/doc-latest/reference/html/orm.html)

Just like Benny's blog suggests I have created "FluentNHibernateLocalSessionFactoryObject" the contents of this class is same as in blog post.

My Spring configuration file looks like this:

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


  <object id="MySessionFactory" type="Project.Core.NHinbernate.FluentNHibernateLocalSessionFactoryObject, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="FluentNHibernateMappingAssemblies">
      <list>
        <value>Project.Core.NHibernate</value>
      </list>
    </property>
    <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
        <entry key="hibernate.dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
        <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
      </dictionary>
    </property>
  </object>

  <object id="HomeController" singleton="false" type="Project.UI.Controllers.HomeController">
    <property name="MySessionFactory" value="MySessionFactory" />
  </object>
</objects>

I think this configuration has a lot of things missing (connection string, correct SQL Server 2008 dialect and DB provider).

My goal is to simply inject SessionFactory into HomeController.

I would be very greatful if you could point out what should be changed in my configuration file.

Thank You very much!

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

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

发布评论

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

评论(1

怎会甘心 2024-08-24 23:20:45

虽然我没有使用过 ASP.NET MVC,但您确实需要将 DbProvider 添加到您的配置文件中。 DbProvider 将保存连接字符串。因此,将其添加到您的配置文件中。

  <db:provider id="DbProvider" 
  provider="System.Data.SqlClient" 
  connectionString="Data Source=(local);Database=Spring;User ID=springqa;Password=springqa;Trusted_Connection=False"/>

为了使用 db:provider 语法,您还需要将其添加到应用程序配置文件 (web/app.config)

<configuration>
    <configSections>
       <sectionGroup name="spring">
           <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
       </sectionGroup>
    </configSections>
<spring>
    <parsers>
       <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
    </parsers>
</spring>
</configuration>

这是 Spring.NET 文档的相关链接

另请参阅 Benny 博文的评论,其中有更新的代码示例。

While I haven't worked with ASP.NET MVC you indeed need to add a DbProvider to your configuration file. The DbProvider will hold the connection string. So add this to your configuration file.

  <db:provider id="DbProvider" 
  provider="System.Data.SqlClient" 
  connectionString="Data Source=(local);Database=Spring;User ID=springqa;Password=springqa;Trusted_Connection=False"/>

In order to use the db:provider syntax you'll also need to add this to your application configuration file (web/app.config)

<configuration>
    <configSections>
       <sectionGroup name="spring">
           <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
       </sectionGroup>
    </configSections>
<spring>
    <parsers>
       <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
    </parsers>
</spring>
</configuration>

Here's the relevant link to Spring.NET's documentation

Also look in the comments of the blogpost of Benny, there's an updated code sample.

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