NHibernate version: 1.0.2
Hi @ll,
currently I'm struggeling with an interesting problem concerning private accessors explicitly generated for NHibernate.
My classes look like the following (an excerpt). All have generated public accessors with some plausibility checks and private accessors for NHibernate.
Code:
public sealed class ItemDto:ICreated, IStatus, IModified
{
...
public string ItemDescription
{
get { return _item_description; }
set
{
if( value != null && value.Length > 100)
throw new ArgumentOutOfRangeException("Invalid value for ItemDescription", value, value.ToString());
_item_description = value;
}
}
public DateTime DateCreated
{
get { return _date_created; }
set
{
_date_created = value;
}
}
public DateTime DateModified
{
get { return _date_modified; }
set
{
_date_modified = value;
}
}
...
private string _ITEM_DESCRIPTION
{
get { return _item_description; }
set { _item_description = value; }
}
private DateTime _DATE_CREATED
{
get { return _date_created; }
set { _date_created = value; }
}
private DateTime _DATE_MODIFIED
{
get { return _date_modified; }
set { _date_modified = value; }
}
...
}
To access NHibernate and all stuff concerning the database I implemented some sort of interface (in general terms) with a factory and generic stuff. To retrieve objects by an example I coded the following method.
Code:
public List<T> GetByExample(T exampleInstance, params string[] exclude) {
ICriteria criteria = session.CreateCriteria(persitentType);
Example example = Example.Create(exampleInstance);
example.ExcludeNulls();
example.ExcludeZeroes();
foreach (string propertyToExclude in exclude) {
example.ExcludeProperty(propertyToExclude);
}
//example.ExcludeProperty("DateCreated");
//example.ExcludeProperty("DateModified");
example.ExcludeProperty("_DATE_CREATED");
example.ExcludeProperty("_DATE_MODIFIED");
example.EnableLike();
criteria.Add(example);
return ConvertToGenericList(criteria.List());
}
As you can see two lines are marked as comments. Here is the "problem". If I use the public accessors to tell NHibernate which properties should be ignored it doesn't work. It only works if I use the private (NHibernate) accessors. So far it makes sense, but it's a problem with the array which provides dynamcally property names. The object that calls this method have to know the private accessors in order to exclude properties correctly. This violates some OO principles, so my question is whether there is an other possibility to choose the properties correctly without using the private accessors or not?
Many thanks in advance for your help.
Chavez