Skip to content
This repository has been archived by the owner on Aug 16, 2018. It is now read-only.

Commit

Permalink
Replacing NHibernateSession with NHibernateSessionFactoryBuilder, see s…
Browse files Browse the repository at this point in the history
…harparchitecture#61

* ValidatableObject now requires ValidationContext;
* HasUniqueDomainSignatureAttribute now requires ValidationContext;
* DataAnnotationsEventListener how provides IServiceProvider with ISession;

* TardisBank - use CastleWindsor to register ISession and ISessionFactory;
* TardisBank - update CastleWindsor installers.
  • Loading branch information
cd21h committed May 31, 2015
1 parent dba1fc4 commit fc857c5
Show file tree
Hide file tree
Showing 63 changed files with 574 additions and 475 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ namespace Suteki.TardisBank.Tasks
using System;
using System.Linq;

using NHibernate.Linq;

using SharpArch.NHibernate;
using Domain;

using Suteki.TardisBank.Domain;
using NHibernate;
using NHibernate.Linq;

public interface ISchedulerService
{
void ExecuteUpdates(DateTime now);
}

public class SchedulerService : ISchedulerService
{
public SchedulerService()
{
private ISession session;

public SchedulerService(ISession session)
{
this.session = session;
}

/// <summary>
Expand All @@ -27,7 +29,7 @@ public void ExecuteUpdates(DateTime now)
{
var today = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);

var results = NHibernateSession.Current.Query<Child>().
var results = this.session.Query<Child>().
Where(c => c.Account.PaymentSchedules.Any(p => p.NextRun < today)).Fetch(c => c.Account);

foreach (var child in results.ToList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
16 changes: 1 addition & 15 deletions Samples/TardisBank/Solutions/Suteki.TardisBank.Tests/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<!-- Accepts a comma delimited list of assembly names containing mapping artifacts; the ".dll" is optional -->
Expand Down Expand Up @@ -88,16 +86,4 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
</configuration>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// ReSharper disable InconsistentNaming
using NUnit.Framework;

namespace Suteki.TardisBank.Tests.Model
{
using System;

using SharpArch.Domain.PersistenceSupport;
using Domain;
using NUnit.Framework;
using SharpArch.NHibernate;
using SharpArch.Testing.NUnit;
using SharpArch.Testing.NUnit.NHibernate;

using global::Suteki.TardisBank.Domain;

[TestFixture]
public class ChildTests : RepositoryTestsBase
{
Expand All @@ -20,21 +17,21 @@ public class ChildTests : RepositoryTestsBase
protected override void LoadTestData()
{
var parent = new Parent("Mike Hadlow", "[email protected]", "yyy");
NHibernateSession.Current.Save(parent);
Session.Save(parent);

parentId = parent.Id;

var child = parent.CreateChild("Leo", "leohadlow", "xxx");
NHibernateSession.Current.Save(child);
RepositoryTestsHelper.FlushSessionAndEvict(child);
RepositoryTestsHelper.FlushSessionAndEvict(parent);
Session.Save(child);
FlushSessionAndEvict(child);
FlushSessionAndEvict(parent);
this.childId = child.Id;
}

[Test]
public void Should_be_able_to_create_and_retrieve_a_child()
{
var child = new LinqRepository<Child>().Get(childId);
var child = new LinqRepository<Child>(TransactionManager, Session).Get(childId);
child.Name.ShouldEqual("Leo");
child.UserName.ShouldEqual("leohadlow");
child.ParentId.ShouldEqual(parentId);
Expand All @@ -45,7 +42,7 @@ public void Should_be_able_to_create_and_retrieve_a_child()
[Test]
public void Should_be_able_to_add_schedule_to_account()
{
var childRepository = new LinqRepository<Child>();
var childRepository = new LinqRepository<Child>(TransactionManager, Session);
var childToTestOn = childRepository.Get(childId);
childToTestOn.Account.AddPaymentSchedule(DateTime.UtcNow, Interval.Week, 10, "Weekly pocket money");
FlushSessionAndEvict(childToTestOn);
Expand All @@ -57,7 +54,7 @@ public void Should_be_able_to_add_schedule_to_account()
[Test]
public void Should_be_able_to_add_transaction_to_account()
{
var childRepository = new LinqRepository<Child>();
var childRepository = new LinqRepository<Child>(TransactionManager, Session);
var childToTestOn = childRepository.Get(childId);
childToTestOn.ReceivePayment(10, "Reward");
FlushSessionAndEvict(childToTestOn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class MessageTests : RepositoryTestsBase
protected override void LoadTestData()
{
User user = new Parent("Dad", "[email protected]", "xxx");
NHibernateSession.Current.Save(user);
Session.Save(user);
this.FlushSessionAndEvict(user);
userId = user.Id;
}

[Test]
public void Should_be_able_to_add_a_message_to_a_user()
{
var parentRepository = new LinqRepository<Parent>();
var parentRepository = new LinqRepository<Parent>(TransactionManager, Session);
User userToTestWith = parentRepository.Get(userId);

userToTestWith.SendMessage("some message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public class ParentTests : RepositoryTestsBase
protected override void LoadTestData()
{
var parent = new Parent(name: "Mike Hadlow", userName: string.Format("{0}@yahoo.com", "mike"), password: "yyy");
NHibernateSession.Current.Save(parent);
Session.Save(parent);
this.FlushSessionAndEvict(parent);
parentId = parent.Id;
}

[Test]
public void Should_be_able_to_create_and_retrieve_Parent()
{
var parent = new LinqRepository<Parent>().Get(parentId);
var parent = new LinqRepository<Parent>(TransactionManager, Session).Get(parentId);
parent.ShouldNotBeNull();
parent.Name.ShouldEqual("Mike Hadlow");
parent.UserName.ShouldEqual("[email protected]");
Expand All @@ -34,7 +34,7 @@ public void Should_be_able_to_create_and_retrieve_Parent()
[Test]
public void Should_be_able_to_add_a_child_to_a_parent()
{
var linqRepository = new LinqRepository<Parent>();
var linqRepository = new LinqRepository<Parent>(TransactionManager, Session);
var savedParent = linqRepository.Get(parentId);
savedParent.CreateChild("jim", "jim123", "passw0rd1");
savedParent.CreateChild("jenny", "jenny123", "passw0rd2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

namespace Suteki.TardisBank.Tests.Model
{
using System;
using System.Linq;

using NHibernate.Linq;

using SharpArch.NHibernate;
using SharpArch.Testing.NUnit.NHibernate;
using Domain;

using System;
using NHibernate.Linq;

using NUnit.Framework;

using global::Suteki.TardisBank.Domain;
using global::Suteki.TardisBank.Tasks;
using SharpArch.Testing.NUnit.NHibernate;

using Tasks;

[TestFixture]
public class PaymentSchedulingQueryTests : RepositoryTestsBase
Expand All @@ -26,26 +25,26 @@ public class PaymentSchedulingQueryTests : RepositoryTestsBase
protected override void LoadTestData()
{
parent = new Parent("parent", "parent", "xxx");
NHibernateSession.Current.Save(parent);
Session.Save(parent);
this.someDate = new DateTime(2010, 4, 5);
NHibernateSession.Current.Save(CreateChildWithSchedule("one", 1M, this.someDate.AddDays(-2)));
NHibernateSession.Current.Save(CreateChildWithSchedule("two", 2M, this.someDate.AddDays(-1)));
NHibernateSession.Current.Save(CreateChildWithSchedule("three", 3M, this.someDate));
NHibernateSession.Current.Save(CreateChildWithSchedule("four", 4M, this.someDate.AddDays(1)));
NHibernateSession.Current.Save(CreateChildWithSchedule("five", 5M, this.someDate.AddDays(2)));
NHibernateSession.Current.Flush();
Session.Save(CreateChildWithSchedule("one", 1M, this.someDate.AddDays(-2)));
Session.Save(CreateChildWithSchedule("two", 2M, this.someDate.AddDays(-1)));
Session.Save(CreateChildWithSchedule("three", 3M, this.someDate));
Session.Save(CreateChildWithSchedule("four", 4M, this.someDate.AddDays(1)));
Session.Save(CreateChildWithSchedule("five", 5M, this.someDate.AddDays(2)));
Session.Flush();
}

[Test]
public void Should_be_able_to_query_all_pending_scheduled_payments()
{
ISchedulerService schedulerService = new SchedulerService();
ISchedulerService schedulerService = new SchedulerService(Session);
schedulerService.ExecuteUpdates(someDate);
NHibernateSession.Current.Flush();
Session.Flush();

// check results

var results = NHibernateSession.Current.Query<Child>().ToList();
var results = Session.Query<Child>().ToList();

results.Count().ShouldEqual(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ public class UserTests : RepositoryTestsBase
protected override void LoadTestData()
{
var mike = new Parent("Mike Hadlow", "[email protected]", "yyy");
NHibernateSession.Current.Save(mike);
Session.Save(mike);

var leo = mike.CreateChild("Leo", "leohadlow", "xxx");
var yuna = mike.CreateChild("Yuna", "yunahadlow", "xxx");
NHibernateSession.Current.Save(leo);
NHibernateSession.Current.Save(yuna);
Session.Save(leo);
Session.Save(yuna);

var john = new Parent("John Robinson", "[email protected]", "yyy");
NHibernateSession.Current.Save(john);
Session.Save(john);

var jim = john.CreateChild("Jim", "jimrobinson", "xxx");
NHibernateSession.Current.Save(jim);
Session.Save(jim);

NHibernateSession.Current.Flush();
Session.Flush();
}

[Test]
public void Should_be_able_to_treat_Parents_and_Children_Polymorphically()
{
var users = NHibernateSession.Current.Query<User>().ToArray();
var users = Session.Query<User>().ToArray();

users.Length.ShouldEqual(5);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,39 @@
public class MappingIntegrationTests
{
private Configuration configuration;
private ISessionFactory sessionFactory;
private ISession session;

[SetUp]
public virtual void SetUp()
{
string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
this.configuration = NHibernateSession.Init(
new SimpleSessionStorage(),
mappingAssemblies,
new AutoPersistenceModelGenerator().Generate(),
"../../../../Solutions/Suteki.TardisBank.Web.Mvc/NHibernate.config");
this.configuration = new NHibernateSessionFactoryBuilder()
.AddMappingAssemblies(mappingAssemblies)
.UseAutoPersitenceModel(new AutoPersistenceModelGenerator().Generate())
.UseConfigFile("../../../../Solutions/Suteki.TardisBank.Web.Mvc/NHibernate.config")
.BuildConfiguration();
sessionFactory = configuration.BuildSessionFactory();
session = sessionFactory.OpenSession();
}

[TearDown]
public virtual void TearDown()
{
NHibernateSession.CloseAllSessions();
NHibernateSession.Reset();
if (sessionFactory != null)
{
sessionFactory.Dispose();
}
}

[Test]
public void CanConfirmDatabaseMatchesMappings()
{
var allClassMetadata = NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();
var allClassMetadata = sessionFactory.GetAllClassMetadata();

foreach (var entry in allClassMetadata)
{
NHibernateSession.Current.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
.SetMaxResults(0).List();
}
}
Expand All @@ -62,8 +68,6 @@ public void CanConfirmDatabaseMatchesMappings()
[Test]
public void CanGenerateDatabaseSchema()
{
var session = NHibernateSession.GetDefaultSessionFactory().OpenSession();

using (TextWriter stringWriter = new StreamWriter("../../../../Database/UnitTestGeneratedSchema.sql"))
{
new SchemaExport(this.configuration).Execute(true, false, false, session.Connection, stringWriter);
Expand Down
Loading

0 comments on commit fc857c5

Please sign in to comment.