Fluent NHibernate - mixing table-per-subclass and table-per-class-hierarchy - fluent-nhibernate

Give the following structure,
MyBaseClass {
public int Id {get; private set;}
}
MySubclassWithDiscriminator : MyBaseClass {
}
MySubclass : MyBaseClass {
public string SomeThing {get; set;}
}
How would I use Fluent NH to map these correctly, using a combination of table-per-subclass and table-per-class-hierarchy? I've tried a custom AutomappingConfiguration, but seem to be going around in circles:
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace.Contains("Entities");
}
public override bool IsDiscriminated(Type type)
{
// only classes with additional properties should be
// using the table-per-subclass strategy
if ((type.IsAssignableFrom(typeof(MyBaseClass)) ||
type.IsSubclassOf(typeof(MyBaseClass)) &&
type.GetProperties(BindingFlags.Public |
BindingFlags.FlattenHierarchy)
.Count() <= 1))
{
return true;
}
return false;
}
}
public class SubclassConvention : ISubclassConvention
{
public void Apply(ISubclassInstance instance)
{
// Use the short name of the type, not the full name
instance.DiscriminatorValue(instance.EntityType.Name);
}
}
It seems to me from my investigation that the use of the Discriminator is a binary choice when using FNH, while an HBM has the ability to have a Discriminator column and a subclass at the same time.

EDIT - 2011-05-12
I rewrote this post to try to address James Gregory's comments.
Here's the HBM that I'm trying to achieve:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class
xmlns="urn:nhibernate-mapping-2.2"
name="Mixed_Parent"
abstract="true"
table="`Mixed_Parent`">
<id name="Id" type="System.Int32">
<generator class="identity" />
</id>
<discriminator type="String">
<column name="discriminator" />
</discriminator>
<subclass
name="Mixed_TPCH_Child"
discriminator-value="Mixed_TPCH_Child" />
<subclass
name="Mixed_TPS_Child"
discriminator-value="Mixed_TPS_Child">
<join table="`Mixed_TPS_Child`" >
<key column="Id" />
<property name="Description" type="String">
<column name="Description" />
</property>
</join>
</subclass>
</class>
</hibernate-mapping>
So, what I've seen is that the HBM that's generated is EITHER a <joined-subclass> or <subclass> without the <join> sub-element, never a combination of the two. Am I missing something here?
Here's a failing test that can be added to the SubclassPersistenceModelTests to illustrate:
namespace MixedTablePerSubclassWithTablePerClassHierarchy
{
public class Mixed_Parent
{
public virtual int Id { get; set; }
}
public class Mixed_TPCH_Child
{
}
public class Mixed_TPS_Child
{
public virtual string Description { get; set; }
}
public class Mixed_ParentMap : ClassMap<Mixed_Parent>
{
public Mixed_ParentMap()
{
Id(x => x.Id);
DiscriminateSubClassesOnColumn("discriminator");
}
}
public class Mixed_TPCH_ChildMap : SubclassMap<Mixed_TPCH_Child>
{ }
public class Mixed_TPS_ChildMap : SubclassMap<Mixed_TPS_Child>
{
public Mixed_TPS_ChildMap()
{
Map(x => x.Description);
}
}
}
[Test]
public void ShouldAllowMixedTablePerSubclassWithTablePerClassHierarchy()
{
var model = new PersistenceModel();
model.Add(
new MixedTablePerSubclassWithTablePerClassHierarchy
.Mixed_ParentMap());
model.Add(
new MixedTablePerSubclassWithTablePerClassHierarchy
.Mixed_TPCH_ChildMap()
);
model.Add(
new MixedTablePerSubclassWithTablePerClassHierarchy
.Mixed_TPS_ChildMap());
var classMapping = model.BuildMappings()
.First()
.Classes.First();
// WHAT SHOULD THIS NUMBER BE (0, 1 or 2)?
classMapping.Subclasses.Count().ShouldEqual(1);
classMapping
.Subclasses
.First()
.Type
.ShouldEqual(
typeof(
MixedTablePerSubclassWithTablePerClassHierarchy
.Mixed_TPS_Child)
); // WHICH OF THE CHILDREN WOULD BE FIRST?
}

Related

NHibernate mapping by code ManyToOne with CompositeIdentity

I am trying to convert my FluentNHibernate mappings to NHibernate Mapping By-code using NHibernate 3.3.3. The goal is to upgrade to NHibernate 3.3.3 and to cut down on the number of assemblies being distributed. I am having some trouble with translating FluentNHibernate's References mapping to a Many-To-One mapping.
Many of my entities have descriptions that need translations. For this I use a Texts table that contains these texts in all available languages. I use the text ID to reference the Texts table and then in the Data Access Object I filter the required language. This works create using NHibernate 3.1 and FluentNHibernate, with NHibernate 3.3.3 and mapping by-code however I just a MappingException saying : property mapping has wrong number of columns: Category.Description type: Text.
Where is my new mapping wrong? Or is this type of mapping not possible in NHibernate 3.3.3.
This is the Texts table (SQL-server 2008).
CREATE TABLE Texts (
ID int NOT NULL,
languageID nvarchar(10) NOT NULL,
Singular nvarchar(max) NOT NULL,
Plural nvarchar(max) NULL,
CONSTRAINT PK_Texts PRIMARY KEY CLUSTERED (ID ASC, languageID ASC)
WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
The Text class:
public class Text
{
public Text(int id, string language, string singular, string plural)
{
this.ID = new TextCompositeID(id, language);
this.Singular = singular;
this.Plural = plural;
}
public TextCompositeID ID { get; private set; }
public string Plural { get; private set; }
public string Singular { get; set; }
public override bool Equals(object obj)
{
var text = (Text)obj;
if (text == null)
{
return false;
}
return this.ID.Equals(text.ID);
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
}
As an example here is the Category class:
public class Category
{
public int ID { get; set; }
public Text Description { get; set; }
}
The FluentNHibernate xml mapping for the Category class looks like this:
<class xmlns="urn:nhibernate-mapping-2.2"
mutable="true"
name="Category"
lazy="false"
table="Category"
where="IsObsolete=0">
<id name="ID" type="System.Int32">
<column name="ID" not-null="true" />
<generator class="native" />
</id>
<many-to-one cascade="none"
class="Text"
name="Description">
<column name="TextID"
not-null="true"
unique="false" />
</many-to-one>
</class>
Which was generated from this:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
this.Table("Category");
Not.LazyLoad();
this.Where("IsObsolete=0");
Id(x => x.ID)
.Column("ID")
.GeneratedBy.Native()
.Not.Nullable();
References(x => x.Description)
.Column("DescriptionID")
.Cascade.None()
.Not.Unique()
.Not.Nullable();
}
}
This is the NHibernate ClassMapping I created:
public CategoryMap()
{
this.Lazy(false);
this.Mutable(true);
this.Table("Category");
this.Where("IsObsolete=0");
this.Id(
x => x.ID,
map =>
{
map.Column("ID");
map.Generator(Generators.Native);
});
this.ManyToOne(
x => x.Description,
map =>
{
map.Cascade(Cascade.None);
map.Class(typeof(Text));
map.Column("TextID");
map.Fetch(FetchKind.Join);
map.Lazy(LazyRelation.NoLazy);
map.ForeignKey("none");
});
}
From this I get this xml mapping:
<class name="Category"
lazy="false"
table="Category"
where="IsObsolete=0">
<id name="ID"
column="ID"
type="Int32">
<generator class="native" />
</id>
<many-to-one name="Description"
class="Text"
column="TextID"
fetch="join"
foreign-key="none"
lazy="false" />
</class>
I found an answer myself. I had to remove the composite ID from the Text class:
public class Text
{
public Text(int id, string language, string singular, string plural)
{
this.ID = id;
this.LanguageID = language;
this.Singular = singular;
this.Plural = plural;
}
public int ID { get; private set; }
public string LanguageID { get; private set; }
public string Plural { get; private set; }
public string Singular { get; set; }
public override bool Equals(object obj)
{
var text = (Text)obj;
if (text == null)
{
return false;
}
return this.ID.Equals(text.ID);
}
public override int GetHashCode()
{
return this.ID.GetHashCode();
}
}
The Category mapping has become:
public CategoryMap()
{
this.Lazy(false);
this.Mutable(true);
this.Table("Category");
this.Where("IsObsolete=0");
this.Id(
x => x.ID,
map =>
{
map.Column("ID");
map.Generator(Generators.Native);
});
this.ManyToOne(
x => x.Description,
map =>
{
map.Column("TextID");
map.Fetch(FetchKind.Join);
map.ForeignKey("none");
map.Lazy(LazyRelation.NoLazy);
});
}
In the Data Access Object the old QueryOver query now gets me the required result.

Many-To-Many relationships with two natural keys

I have the following mapping
UserProfile.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="AngusBook.Domain"
namespace="AngusBook.Domain">
<class name="UserProfile">
<id name="UserId" type="int">
<generator class="identity" />
</id>
<natural-id mutable="false">
<property name="UserName" />
</natural-id>
<set name="Companies" table="Users_Companies">
<key column="UserId"/>
<many-to-many column="CompanyId" class="Company" />
</set>
</class>
</hibernate-mapping>
Company.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="AngusBook.Domain" namespace="AngusBook.Domain" >
<class name="Company">
<id name="Id">
<generator class="hilo" />
</id>
<natural-id mutable="false">
<property name="CompanyName" />
</natural-id>
<set name="Users" table="Users_Companies">
<key column="CompanyId"/>
<many-to-many column="UserId" class="UserProfile" />
</set>
</class>
</hibernate-mapping>
Table design
With this mapping I have, I can have two identical rows in Users_Companies table (ie: two rows with the same pair of foreign keys that belong to the UserProfile and Company table). Using mappings, how do I cause NHibernate or SQL to throw an error/exception when an attempt is made to insert a pair of foreign keys into Users_Companies that already exist in the table? I would like each row in Users_Companies to be unique and not have repetitive data.
I found out the problem. I didn't implement the Equals() and GetHashCode() correctly.
I ended up refactored my entities to use the following base entity class and that fixed the issue. No error is thrown when a duplicated entity is added to the set but the set will simply not add the duplicated entity.
Entity.cs
public abstract class Entity
{
public virtual TId Id { get; protected set; }
protected virtual int Version { get; set; }
public override bool Equals(object obj)
{
return Equals(obj as Entity<TId>);
}
private static bool IsTransient(Entity<TId> obj)
{
return obj != null &&
Equals(obj.Id, default(TId));
}
private Type GetUnproxiedType()
{
return GetType();
}
public virtual bool Equals(Entity<TId> other)
{
if (other == null)
return false;
if (ReferenceEquals(this, other))
return true;
if (!IsTransient(this) &&
!IsTransient(other) &&
Equals(Id, other.Id))
{
var otherType = other.GetUnproxiedType();
var thisType = GetUnproxiedType();
return thisType.IsAssignableFrom(otherType) ||
otherType.IsAssignableFrom(thisType);
}
return false;
}
public override int GetHashCode()
{
if (Equals(Id, default(TId)))
return base.GetHashCode();
return Id.GetHashCode();
}
}
public abstract class Entity : Entity<Guid>
{
}
UserProfile.cs
public class UserProfile : Entity<int>
{
public UserProfile()
{
Companies = new HashedSet<Company>();
}
public virtual string UserName { set; get; }
public virtual ISet<Company> Companies { set; get; }
}
Company.cs
public class Company : Entity<int>
{
public Company()
{
Users = new HashedSet<UserProfile>();
}
public Company(string name) :this()
{
this.CompanyName = name;
}
public virtual string CompanyName { set; get; }
public virtual ISet<UserProfile> Users { set; get; }
}

NHibernate Stale State Issue

I'm curious if anyone could help me resolve an issue of stale state in nHibernate.
First, the .Net class code:
public class Test
{
public static Test Get(int testId) { return Factory.GetTest(testId); }
public Test() { Related = new List<TestRelate>(); }
public virtual int ID { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<TestRelate> Related { get; set; }
public virtual void Delete() { Factory.Delete(this); }
public virtual void Save() { Factory.Save(this); }
}
public class TestRelate
{
protected TestRelate() { }
public TestRelate(Test test) { TestID = test.ID; }
public virtual int ID { get; protected set; }
public virtual int TestID { get; set; }
public virtual string Data { get; set; }
public virtual void Delete() { Factory.Delete(this); }
public virtual void Save() { Factory.Save(this); }
}
class Factory
{
public static Test GetTest(int testId)
{
ISession session = Session.HybridSessionBuilder.Instance;
IList<Test> ret = null;
ITransaction tx = null;
tx = session.BeginTransaction();
ret = session.CreateCriteria(typeof(Test))
.Add(Expression.Eq("ID", testId))
.List<Test>();
tx.Commit();
return ret.Count == 0 ? null : ret[0];
}
public static void Save<T>(T element)
{
ISession session = Session.HybridSessionBuilder.Instance;
ITransaction tx = null;
tx = session.BeginTransaction();
session.Save(element);
tx.Commit();
}
public static void Delete<T>(T element)
{
ISession session = Session.HybridSessionBuilder.Instance;
ITransaction tx = null;
tx = session.BeginTransaction();
session.Delete(element);
tx.Commit();
}
}
Then the nHibernate mapping XML:
<class name="Data.Test.Test, Data" table="test_info">
<id name="ID" column="testid">
<generator class="native" />
</id>
<property name="Name" />
<bag name="Related" table="test_relate" lazy="false" cascade="none">
<key column="testid"></key>
<one-to-many class="Data.Test.TestRelate, Data"></one-to-many>
</bag>
</class>
<class name="Data.Test.TestRelate, Data" table="test_relate">
<id name="ID" column="relateid">
<generator class="native" />
</id>
<property name="TestID" />
<property name="Data" />
</class>
And finally the code I'm having trouble with:
Data.Test.Test Test = new Data.Test.Test();
Test.Name = "Hello World";
Test.Save();
Data.Test.TestRelate Relate = new Data.Test.TestRelate(Test);
Relate.Data = "How are you?";
Relate.Save();
Test = Data.Test.Test.Get(Test.ID);
int Count = Test.Related.Count;
The problem is that the Test.Related list is always empty when I run this code. However if I destroy the NHibernate session and load up the Test again it populates the list as expected. I realize I could probably flush all caching data but it seems like there should be a cleaner solution to this issue. Any suggestions?
When you do new Data.Test.TestRelate(Test) there is nothing that adds the new TestRelate instance to the collection in the owner Test. (Unless you do that in the constructor, but I assume you only set TestId there).
You should Add() the new TestRelate instance to Test.Related. Nhibernate will notice the change in the collection and save the new item when the session is flushed.
NHibernate doesn't populate one-to-many collections automatically on commit. You should simply add TestRelate instances to the Related list, as you would do without NHibernate, and then (if you set a "cascade save" mapping) even commit the Test instance only.
There is no need to use the TestID property inside the program at all, as this property is actually only a byproduct of relational DB mapping.
Alright so I realized that my approach was due to some past failed attempts at utilizing NHibernate's cascading. I'll go over each one of the issues and what I did to resolve it.
If I set up cascading saves NHibernate would fail when I would try to add Related elements to a new Test element because the TestID value is not allowed to be null in the database. Altering the property from an integer type to the Test type itself remedied this situation as NHibernate was able to populate the field value after saving the new Test element.
Attempting to delete a Related record by removing it from the list would result in an error due to NHibernate attempting to Update the TestID field to null prior to a delete. Adding the inverse="true" attribute to the Bag mapping element resolved this issue.
Deleting the Test object would not delete the orphaned Related records. Setting the cascade attribute to all-orphan-delete remedied this.
Here's all the new code (there were no changes to the Factory class):
public class Test
{
public static Test Get(int testId) { return Factory.GetTest(testId); }
public Test() { Related = new List<TestRelate>(); }
public virtual int ID { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<TestRelate> Related { get; set; }
public virtual void Delete() { Factory.Delete(this); }
public virtual void Save() { Factory.Save(this); }
}
public class TestRelate
{
protected TestRelate() { }
public TestRelate(Test test) { Test = test; }
public virtual int ID { get; protected set; }
public virtual Test Test { get; set; }
public virtual string Data { get; set; }
public virtual void Delete() { Factory.Delete(this); }
public virtual void Save() { Factory.Save(this); }
}
Mapping changes:
<class name="Data.Test.Test, Data" table="test_info">
<id name="ID" column="testid">
<generator class="native" />
</id>
<property name="Name" />
<bag name="Related" table="test_relate" lazy="false" cascade="all-delete-orphan" inverse="true">
<key column="testid"></key>
<one-to-many class="Data.Test.TestRelate, Data"></one-to-many>
</bag>
</class>
<class name="Data.Test.TestRelate, Data" table="test_relate">
<id name="ID" column="relateid">
<generator class="native" />
</id>
<many-to-one name="Test" column="testid" />
<property name="Data" />
</class>
The following code now behaves as expected:
Data.Test.Test Test;
Data.Test.TestRelate Relate;
Test = new Data.Test.Test();
Test.Name = "Hello World";
Relate = new Data.Test.TestRelate(Test);
Relate.Data = "How are you?";
Test.Related.Add(Relate);
Test.Save();
Relate = new Data.Test.TestRelate(Test);
Relate.Data = "Relate #2";
Test.Related.Add(Relate);
Test.Save();
Test.Related.RemoveAt(0);
Test.Save();
Test = Data.Test.Test.Get(Test.ID);
int Count = Test.Related.Count;
Test.Delete();
I was able to glean most of these answers from http://ayende.com . I highly recommend this site as a resource for nHibernate questions.

NHibernate.QueryException: could not resolve property

I'm using FluentNHibernate and Linq To Nhibernate, with these entities (only the relevant parts):
public class Player : BaseEntity<Player>
{
private readonly IList<PlayerInTeam> allTeams = new List<PlayerInTeam>();
public IEnumerable<Team> Teams
{
get
{
return from playerInTeam in allTeams
where playerInTeam.Roster.Match == null
select playerInTeam.Roster.Team;
}
}
}
public class PlayerInTeam : BaseEntity<PlayerInTeam>
{
public int PlayerNumber { get; set; }
public Player Player { get; set; }
public Position Position { get; set; }
public Roster Roster { get; set; }
}
public class Roster : BaseEntity<Roster>
{
private readonly IList<PlayerInTeam> players = new List<PlayerInTeam>();
public Team Team { get; set; }
public IEnumerable<PlayerInTeam> Players { get { return players; } }
}
public class Team : BaseEntity<Team>
{
private readonly IList<Roster> allRosters = new List<Roster>();
public Team(string name, Sex sex)
{
allRosters.Add(new Roster(this));
}
public Roster DefaultRoster
{
get { return allRosters.Where(r => r.Match == null).First(); }
}
}
and the matching mappings:
public class PlayerMap : ClassMap<Player>
{
public PlayerMap()
{
HasMany<PlayerInTeam>(Reveal.Member<Player>("allTeams"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class PlayerInTeamMap : ClassMap<PlayerInTeam>
{
public PlayerInTeamMap()
{
References(pit => pit.Player)
.Not.Nullable();
References(pit => pit.Roster)
.Not.Nullable();
}
}
public class RosterMap : ClassMap<Roster>
{
public RosterMap()
{
References(tr => tr.Team)
.Not.Nullable();
HasMany<PlayerInTeam>(Reveal.Member<Roster>("players"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
public class TeamMap : ClassMap<Team>
{
public TeamMap()
{
HasMany<Roster>(Reveal.Member<Team>("allRosters"))
.Inverse()
.Cascade.AllDeleteOrphan()
.Access.CamelCaseField();
}
}
I have this repository method:
public IEnumerable<Player> PlayersNotInTeam(Team team)
{
return from player in Session.Linq<Player>()
where !player.Teams.Contains(team)
select player;
}
Which gives me this exception: NHibernate.QueryException: could not resolve property: Teams of: Emidee.CommonEntities.Player [.Where(NHibernate.Linq.NhQueryable`1[Emidee.CommonEntities.Player], Quote((player, ) => (Not(.Contains(player.Teams, p1, )))), )]
I've looked inside the hbm file, generated by Fluent NHibernate, concerning Player, and here is what I get:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Emidee.CommonEntities.Player, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" table="Players">
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="increment" />
</id>
<bag access="field.camelcase" cascade="all-delete-orphan" inverse="true" name="allTeams" mutable="true">
<key>
<column name="Player_id" not-null="true" />
</key>
<one-to-many class="Emidee.CommonEntities.PlayerInTeam, Emidee.CommonEntities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=65c7ad487c784bec" />
</bag>
</class>
</hibernate-mapping>
Indeed, I don't have a Teams property for Player in that file.
But shouldn't my mapping take care of this?
Where do you think the problem is?
Thanks in advance
Mike
Technically, it's not possible to look inside a method body in C-sharp. So FluentNhibernate can not do that as well. You have to apply a different strategy to model your entities and/or generate the mapping yourself.
The hbm.xml file is your mapping. So, as long as you do not inform NHibernate that your Player class has a Teams property, NHibernate doesn't know anything about it.
Next to that, why do you have an hbm.xml mapping file, and a mapping using Fluent ?
(It seems that the hbm.xml file has priority/precedes the Fluent NH mapping).

Nhibernate mapping

I am trying to map Users to each other. The senario is that users can have buddies, so it links to itself
I was thinking of this
public class User
{
public virtual Guid Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string Password { get; set; }
public virtual DateTime? DateCreated { get; set; }
**public virtual IList<User> Friends { get; set; }**
public virtual bool Deleted { get; set; }
}
But am strugling to do the xml mapping.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyVerse.Domain"
namespace="MyVerse.Domain" >
<class name="User" table="[User]">
<id name="Id">
<generator class="guid" />
</id>
<property name="FirstName" />
<property name="LastName" />
<property name="EmailAddress" />
<property name="Password" />
<property name="DateCreated" />
<property name="Deleted" />
<set name="Friends" table="UserFriend">
<key foreign-key="Id"></key>
<many-to-many class="User"></many-to-many>
</set>
</class>
</hibernate-mapping>
something like
<bag name="Friends" table="assoc_user_table" inverse="true" lazy="true" cascade="all">
<key column="friend_id" />
<many-to-many class="User,user_table" column="user_id" />
</bag>
Consider using the repository pattern. Create a Repository contract and a base abstract class that takes one of your entities as a type (your mapped class)
Open the session when the repository is initialized and close when destroyed. (implement IDisposable).
Then make sure all of your access to the session happens within the using statement:
[pseudo-code]:
using(var repository = RepositoryFactory<EntityType>.CreateRepository())
{
var entity = repository.get(EntityID);
foreach (somesubclass in entity.subclasscollection)
{
//Lazy loading can happen here, session is still open with the repository
... Do Something
}
}
I use a base abstract class for my Repositories. This one is for my readonly repository but you'll get the drift. They key is to keep your units of work small, open the session only when you have something to do with the database, then let it close on the dispose. Here's the base class, disclaimer YMMV:
public interface IEntity
{
int Id { get; set; }
}
public interface IRORepository<TEntity> : IDisposable where TEntity : IEntity
{
List<TEntity> GetAll();
TEntity Get(int id);
}
public abstract class RORepositoryBase<T> : IRORepository<T> where T : IEntity
{
protected ISession NHibernateSession;
protected RORepositoryBase()
{
NHibernateSession = HibernateFactory.OpenSession();
NHibernateSession.DefaultReadOnly = true;
}
public ISession Session { get { return NHibernateSession; } }
public void Dispose()
{
NHibernateSession.Flush();
NHibernateSession.Close();
NHibernateSession.Dispose();
}
public virtual List<T> GetAll()
{
return NHibernateSession.Query<T>().ToList();
}
public virtual T Get(int id)
{
return NHibernateSession.Get<T>(id);
}
}