One of the most useful things we can do (especially if we are using some sort of code generation to keep it in sync!) is to generate a FieldNames structure. One of the complaints that I hear from other developers coming to NHibernate is that the queries aren't strongly typed, so if the database structure changes or if they have a typo in their code, they won't know at compile time that they have broken code.
Traditional NHibernate queries are generally written as follows:
criteria.Add(Restriction.Eq("FirstName", fName);
In this case, if we change the field name in the database from FirstName
to FName
(and we don't adjust our mapping file, and leave FName
to map to FirstName
in our class), our code will compile just fine. However, when we run it, we will get a runtime exception because the FirstName
field doesn't exist on our object.
One simple way to overcome this issue is to use a FieldNames structure, which simply maps string names of the properties to a local structure...