Commit 8c18a674 authored by Sam Saffron's avatar Sam Saffron

Added nhibernate

parent da11e958
using System.Data;
using NHibernate;
using NHibernate.Cfg;
namespace SqlMapper.NHibernate
{
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure(@"..\..\NHibernate\hibernate.cfg.xml");
configuration.AddAssembly(typeof(Post).Assembly);
configuration.AddXmlFile(@"..\..\NHibernate\Post.hbm.xml");
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping namespace="SqlMapper" assembly="Smackdown" xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="Post" table="Posts" schema="dbo">
<id name="Id" access="property" column="Id">
<generator class="native" />
</id>
<property name="Counter1" type="Int32" column="Counter1" />
<property name="Counter2" type="Int32" column="Counter2" />
<property name="Counter3" type="Int32" column="Counter3" />
<property name="Counter4" type="Int32" column="Counter4" />
<property name="Counter5" type="Int32" column="Counter5" />
<property name="Counter6" type="Int32" column="Counter6" />
<property name="Counter7" type="Int32" column="Counter7" />
<property name="Counter8" type="Int32" column="Counter8" />
<property name="Counter9" type="Int32" column="Counter9" />
<property name="CreationDate" type="DateTime" column="CreationDate" />
<property name="LastChangeDate" type="DateTime" column="LastChangeDate" />
<property name="Text" type="String" column="Text" />
</class>
</hibernate-mapping>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string_name">Smackdown.Properties.Settings.tempdbConnectionString</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
<property name="cache.use_minimal_puts">false</property>
<property name="use_outer_join">false</property>
</session-factory>
</hibernate-configuration>
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
using System.Data.Linq; using System.Data.Linq;
using System.Diagnostics; using System.Diagnostics;
using Massive; using Massive;
using SqlMapper.NHibernate;
namespace SqlMapper namespace SqlMapper
{ {
...@@ -118,6 +119,21 @@ public void Run(int iterations) ...@@ -118,6 +119,21 @@ public void Run(int iterations)
SubSonic.tempdbDB db=new SubSonic.tempdbDB(); SubSonic.tempdbDB db=new SubSonic.tempdbDB();
tests.Add(id => new SubSonic.Query.CodingHorror(db.Provider, "select * from Posts where Id = @0", id).ExecuteTypedList<Post>(), "SubSonic Coding Horror"); tests.Add(id => new SubSonic.Query.CodingHorror(db.Provider, "select * from Posts where Id = @0", id).ExecuteTypedList<Post>(), "SubSonic Coding Horror");
// NHibernate
var nhSession1 = NHibernateHelper.OpenSession();
tests.Add(id => nhSession1.CreateSQLQuery(@"select * from Posts where Id = :id")
.SetInt32("id", id)
.List(), "NHibernate SQL");
var nhSession2 = NHibernateHelper.OpenSession();
tests.Add(id => nhSession2.CreateQuery(@"from Post as p where p.Id = :id")
.SetInt32("id", id)
.List(), "NHibernate HQL");
// HAND CODED // HAND CODED
var connection = Program.GetOpenConnection(); var connection = Program.GetOpenConnection();
......
--- PerformanceTests.cs
+++ PerformanceTests.cs
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
+using System.Data.Linq;
+using System.Data.SqlClient;
+using System.Diagnostics;
using System.Linq;
-using System.Text;
-using System.Data.SqlClient;
-using SqlMapper.Linq2Sql;
-using System.Data.Linq;
-using System.Diagnostics;
using Massive;
using Simple.Data;
+using SqlMapper.Linq2Sql;
+using SqlMapper.NHibernate;
namespace SqlMapper
{
@@ -113,6 +113,16 @@
var simpleDataDb5 = Database.OpenConnection(Program.connectionString);
tests.Add(id => simpleDataDb5.Posts.FindAllById(id).ToList<Post>(), "Simple.Data FindAllBy ToList<Post>");
+ var nhSession1 = NHibernateHelper.OpenSession();
+ tests.Add(id => nhSession1.CreateSQLQuery(@"select * from Posts where Id = :id")
+ .SetInt32("id", id)
+ .List(), "NHibernate SQL");
+
+ var nhSession2 = NHibernateHelper.OpenSession();
+ tests.Add(id => nhSession2.CreateQuery(@"from Post as p where p.Id = :id")
+ .SetInt32("id", id)
+ .List(), "NHibernate HQL");
+
// HAND CODED
var connection = Program.GetOpenConnection();
...@@ -51,12 +51,12 @@ static void RunPerformanceTests() ...@@ -51,12 +51,12 @@ static void RunPerformanceTests()
static void Main(string[] args) static void Main(string[] args)
{ {
#if DEBUG //#if DEBUG
RunTests(); // RunTests();
#else //#else
EnsureDBSetup(); EnsureDBSetup();
RunPerformanceTests(); RunPerformanceTests();
#endif //#endif
Console.ReadKey(); Console.ReadKey();
} }
......
--- Program.cs
+++ Program.cs
@@ -32,7 +32,7 @@
class Program
{
- public static readonly string connectionString = @"Data Source=.\sql2k8r2;Initial Catalog=tempdb;Integrated Security=True";
+ public static readonly string connectionString = @"Data Source=.;Initial Catalog=tempdb;Integrated Security=True";
public static SqlConnection GetOpenConnection()
{
...@@ -35,6 +35,18 @@ ...@@ -35,6 +35,18 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Iesi.Collections">
<HintPath>NHibernate\Iesi.Collections.dll</HintPath>
</Reference>
<Reference Include="LinFu.DynamicProxy">
<HintPath>NHibernate\LinFu.DynamicProxy.dll</HintPath>
</Reference>
<Reference Include="NHibernate">
<HintPath>NHibernate\NHibernate.dll</HintPath>
</Reference>
<Reference Include="NHibernate.ByteCode.LinFu">
<HintPath>NHibernate\NHibernate.ByteCode.LinFu.dll</HintPath>
</Reference>
<Reference Include="SubSonic.Core"> <Reference Include="SubSonic.Core">
<HintPath>SubSonic\SubSonic.Core.dll</HintPath> <HintPath>SubSonic\SubSonic.Core.dll</HintPath>
</Reference> </Reference>
...@@ -64,6 +76,7 @@ ...@@ -64,6 +76,7 @@
</Compile> </Compile>
<Compile Include="Linq2Sql\Post.cs" /> <Compile Include="Linq2Sql\Post.cs" />
<Compile Include="Massive\Massive.cs" /> <Compile Include="Massive\Massive.cs" />
<Compile Include="NHibernate\NHibernateHelper.cs" />
<Compile Include="PerformanceTests.cs" /> <Compile Include="PerformanceTests.cs" />
<Compile Include="PetaPoco\PetaPoco.cs" /> <Compile Include="PetaPoco\PetaPoco.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
...@@ -139,7 +152,10 @@ ...@@ -139,7 +152,10 @@
<DependentUpon>DataClasses.dbml</DependentUpon> <DependentUpon>DataClasses.dbml</DependentUpon>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup>
<Content Include="NHibernate\hibernate.cfg.xml" />
<Content Include="NHibernate\Post.hbm.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
......
--- SqlMapper.csproj
+++ SqlMapper.csproj
@@ -35,6 +35,18 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="Iesi.Collections">
+ <HintPath>Dependencies\Iesi.Collections.dll</HintPath>
+ </Reference>
+ <Reference Include="LinFu.DynamicProxy">
+ <HintPath>Dependencies\LinFu.DynamicProxy.dll</HintPath>
+ </Reference>
+ <Reference Include="NHibernate">
+ <HintPath>Dependencies\NHibernate.dll</HintPath>
+ </Reference>
+ <Reference Include="NHibernate.ByteCode.LinFu">
+ <HintPath>Dependencies\NHibernate.ByteCode.LinFu.dll</HintPath>
+ </Reference>
<Reference Include="Simple.Data, Version=0.5.6.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Dependencies\Simple.Data.dll</HintPath>
@@ -72,6 +84,7 @@
</Compile>
<Compile Include="Linq2Sql\Post.cs" />
<Compile Include="Massive\Massive.cs" />
+ <Compile Include="NHibernate\NHibernateHelper.cs" />
<Compile Include="PerformanceTests.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -107,6 +120,14 @@
<DependentUpon>DataClasses.dbml</DependentUpon>
</None>
</ItemGroup>
+ <ItemGroup>
+ <EmbeddedResource Include="NHibernate\Post.hbm.xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="NHibernate\hibernate.cfg.xml">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </Content>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment