views:

58

answers:

3

When you create a criteria, you can add Restrictions that apply to a property. There are 2 ways of creating a Restriction:

Restrictions.Eq(string propertyName, object value)
or
Restrictions.Eq(IProjection projection, object value)

Thing is, I don't feel comfortable passing property names as strings, since if they ever change, my project will compile as usual and any possible naming mismatch will only be found during unit testing. I'm trying to implement TDD but it won't happen anytime soon, so I'm trying to limit dependencies of unit testing to check for errors (until we manage to embrace TDD).

Any ideas? Thanks in advance!

+3  A: 

Use LINQ, QueryOver (NH3.x) or NH Lambda Extensions (NH2.x) or NHibernate Expression Extensions (NH1.2)

All of these are more strongly-typed than just using strings.

Mauricio Scheffer
Thanks! Those are very interesting. I can't wait to see NH 3.0 stable.
scardazzi
A: 

I can feel your pain.

But there is no way to put dynamic reference to your properties using projections in criteria API, just like you can't make nHibernate mapping detect a table or column name change.

Doing a find and replace step by step with "Property" with work in most cases.

Pierre 303
Yeah, I thought of it too, but sadly I can't really rely on my teammates, if you know what I mean...
scardazzi
A: 

As long as you have complete coverage of unit tests (you don't you?) it shouldn't be a problem, should it :-)?

Another reason to avoid using strings for property names is that refactoring using e.g. Resharper cannot fix those references. This is where Fluent NHibernate really shines.

Andreas Paulsson
Fluent NHibernate is only for mapping. The OP is asking how to avoid strings when *querying*
Mauricio Scheffer