views:

796

answers:

1

Hello,

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"&gt;


  <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!

+1  A: 

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.

BennyM
But Spring .NET's doc says"These provider implementations do not take into account usage with NHibernate. NHibernate scopes a SessionFactory, where second level caching is managed, to each connection."
Daniil Harik
I don't understand your question, could you give me a link to that statement?
BennyM