Calling non-default constructor where validation exists - guarding immutable types

1 view
Skip to first unread message

bstack

unread,
Oct 22, 2008, 7:01:42 AM10/22/08
to nhusers
All,

We are using immutable types in our domain, that we want to map
directly to to database. The type takes the following format:

public class X
{
private readonly int m_Prop1;
private readonly int m_Prop2;

private X() { }

public X(int p1, int p2)
{
//Perform validation guard check against arguments

this.m_Prop1 = p1;
this.m_Prop2 = p2;
}

public int Prop1 { get { return this.m_Prop1; } }
public int Prop2 { get { return this.m_Prop2; } }
}

The problem we have is that the private constructor is what NHibernate
instantiates (not the public constructor where guard checks are
performed), hence we can end up "getting" an instance of X from the
database that is invalid.

Is there any way to get NHibernate to call the constructor that
performs the guard checks?

Fabio Maulo

unread,
Oct 25, 2008, 7:13:10 AM10/25/08
to nhu...@googlegroups.com
2008/10/22 bstack <bs.s...@gmail.com>

The problem we have is that the private constructor is what NHibernate
instantiates (not the public constructor where guard checks are
performed), hence we can end up "getting" an instance of X from the
database that is invalid.

Is there any way to get NHibernate to call the constructor that
performs the guard checks?

Yes there is but... do you have invalid instance in the DB ?
do you really want that your class throw an exception in a fresh load from DB ?

--
Fabio Maulo
Reply all
Reply to author
Forward
0 new messages