Nels_P_Olsen wrote:
NHibernate is essentially a serialization mechanism, and standard .NET serialization works with fields, not properties.
Good point.
jchapman wrote:
Really this is exactly the same thing that .NET Serialization. It is trying to take your object move it to another persistence store besides memory and then allow you to retrieve exactly the same object. .NET Serialization will access your private fields, that's how you the same object back!
Not really a good comparison. Yes, while .NET uses fields for serialization the process of serialization is much easier to control.
Basically, in automatic serialization the system need to serialize all fields except these, which are specially marked out with [NonSerialized] attribute.
In case of a custom serialization the class which is to be serialized do all the work, so information about fields not exposed to anyone.
But consider this code (I know I can use Find in this case:) ):
Code:
public class Foo
{
private int m_id;
}
public class FooService
{
public Foo LoadFooById(ISession session, int id)
{
ICriteria criteria = session.CreateCriteria(typeof(Foo));
criteria.Add(Expression.Eq("id", id)); // here!
return criteria.UniqueResult();
}
}
Now I have a bind to the name of a private field in an outer class. If I want to refactor the code and rename the field I would have to look through all the code that creates queries to the DB to find if it doesn't use old field name.
Of course this also will happen if I rename a public property, but the difference is that if I am renaming a public property it is I who responsible for fixing all such places in other code.