Getting started with NHibernate for SharePoint Part 2 – Getting started

« Previous post (Introduction)Next post (Show Lists and Libraries) »


To get stated we will need to set up the basics of NHibernate and Camelot .NET Connector, this includes the following actions.

  1. Install and include Camelot .NET Connector in your project
  2. Download and include NHibernate in your project
  3. Configure the configuration files
  4. Create a ConnectionProvider and Driver

Prerequisites

To get started with NHibernate for SharePoint please download and install the latest version of the Camelot .NET Connector for SharePoint.

I will use Visual Studio 2012 in this tutorial but most versions will suffice.

You must have access to a SharePoint server. Compatible versions are 2007, 2010, 2013 and SharePoint Online in Office 365.

Create a new project

I will demonstrate this using a console application. Make sure to select the .NET Framework you prefer.

Reference the Camelot .NET Connector assembly

Include the NHibernate binaries. This can be done by either using NuGet or by downloading the NHibernate binaries from Sourceforge.

Using the NuGet GUI

…or by using the NuGet Packet Manager Console, execute the command Install-Package NHibernate

Configuring NHibernate

Create a new file called hibernate.config and add the following code to it

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.connection_string">
        Server=serveraddress;Database=;Domain=;User=username;Password=pass;
        Authentication=Ntlm;TimeOut=50;RecursiveMode=RecursiveAll;
        DecodeName=True;NoListFilters=False;
    </property>
    <property name="connection.release_mode">on_close</property>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="show_sql">false</property>
  </session-factory>
</hibernate-configuration>
  

Please note the connection string, this is where you specify your SharePoint Server whereabouts and whatabouts. The options stated in this example is a suggestion, please read the full docs on connection string options in the official documentation.

Also ensure that the file is copied to output directory at compile time

And register the file in app.config (or web.config for that matter). In this simple example the app.config now looks like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration configSource="hibernate.config"/>
</configuration>

Creating the ConnectionProvider and Driver

Create a new file named CamelotSharePointDriver.cs. This will be the driver for the Camelot .NET Connector that tells NHibernate how to query through the Connector.

namespace NHibernateExample
{
    public class CamelotSharePointDriver : NHibernate.Driver.ReflectionBasedDriver
    {
        public CamelotSharePointDriver()
            : base("Camelot.SharePointConnector", "Camelot.SharePointConnector", "Camelot.SharePointConnector.Data.SharePointConnection", "Camelot.SharePointConnector.Data.SharePointCommand")
        { }

        public override string NamedPrefix { get { return "?"; } }

        public override bool SupportsMultipleOpenReaders { get { return false; } }

        public override bool SupportsMultipleQueries { get { return false; } }

        protected override bool SupportsPreparingCommands { get { return false; } }

        public override bool UseNamedPrefixInParameter { get { return true; } }

        public override bool UseNamedPrefixInSql { get { return true; } }
    }
}

Create another file named CamelotSharePointConnectionProvider.cs. This will be the connection provider object for your application and we will apply it in the NHibernate configuration later. In this example, the connection string is read from the NHibernate configuration. You can easily modify this class to read the connection strings from somewhere else, e.g. the application configuration. It is important that the Driver method returns the CamelotSharePointDriver object that we created previously.
namespace NHibernateExample
{
    public class CamelotSharePointConnectionProvider : NHibernate.Connection.IConnectionProvider
    {
        private string _connectionString;

        public void CloseConnection(System.Data.IDbConnection conn)
        {
            conn.Close();
        }

        public void Configure(System.Collections.Generic.IDictionary<string, string> settings)
        {
            _connectionString = settings["connection.connection_string"];
        }

        public NHibernate.Driver.IDriver Driver { get { return new CamelotSharePointDriver(); } }

        public System.Data.IDbConnection GetConnection()
        {
            var conn = new Camelot.SharePointConnector.Data.SharePointConnection(_connectionString);
            conn.Open();
            return conn;
        }

        public void Dispose()
        {
            return;
        }
    }
}


« Previous post (Introduction)Next post (Show Lists and Libraries) »

This entry was posted in NHibernate for SharePoint, SharePoint and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>