For the NHibernate config the reasonable alternative is to use Fluent NHibernate, which uses lambda's to get the propertynames.
For the queries you have to write such a solution yourself.
I have a base class for all entities that looks like this
public Entity<T> where T : Entity<T> { public static string PropertyName(Expression<Func<T, object>> e) { UnaryExpression ue = e.Body as UnaryExpression; MemberExpression me = (ue == null ? e.Body as MemberExpression : ue.Operand as MemberExpression); return me.Member.Name; } }After that I can use this method to get the propertynames in queries:
Criteria.Add(Expression.Eq( User.PropertyName(x => x.Emailaddress), "email@example.com"));There are disadvantages. First, using Lambda expressions is slower than using the string value. Second, this only works for public properties. Third: in VB this will look very ugly. Until now, I can live with these disadvantages.
No comments:
Post a Comment