Why is specifying an abstract base class as the proxy for a persistent class not allowed?
Code:
MappingException: proxy must be either an interface, or the class itself
Would this cause undesirable side-effects of some description?
Since the constructor of my persistent class does a lot of initialization that is redundant for a proxy instance, I want to split it into two classes.
1. A minimal abstract one.
2. A concrete one with all the important initialization in the constructor.
I understand that specifying an inteface is the usual way to achieve this, but as my following example should illustrate, I need to prefetch certain properties so they can be read without causing initialization.
That is, I need to control which members are virtual and ensure that "hibernate.use_proxy_validator" is set to false.
I would really prefer not to have to contaminate my domain layer with calls to !NHibernateUtil.IsInitialized(this) in all the constructors.
Code:
interface IBlogItem
{
DateTime ItemDate { get; set; }
string Text { get; set; }
}
abstract class AbstractBlogItem : IBlogItem
{
private DateTime _itemDate;
private string _text;
//will NOT cause initialization
public DateTime ItemDate
{
get { return _itemDate; }
set { SetItemDate(value); }
}
//will cause initialization
public virtual void SetItemDate(DateTime name)
{
_itemDate = name;
}
//will cause initialization
public virtual string Text
{
get { return _text; }
set { _text = value; }
}
}
class BlogItem : AbstractBlogItem
{
public BlogItem()
{
//intenstive initialization that is
//not relevant to a proxied instance
}
}
static class Program
{
static void Main()
{
using (ISession session = _factory.OpenSession())
{
IList<object[]> rows = session.CreateQuery(
"select b.ItemDate " +
"from BlogItem b").List<object[]>();
IList<IBlogItem> blogItems = new List<IBlogItem>(rows.Count);
foreach (object[] row in rows)
{
AbstractBlogItem blogItem =
session.Load<AbstractBlogItem>(row[0]);
typeof (AbstractBlogItem).GetField("_itemDate",
BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(blogItem, row[0]);
blogItems.Add(blogItem);
}
}
}
}
Cheers.