views:

120

answers:

2

I just had a NHibernate related problem where I forgot to map one property of a class.

A very simplified example:

public class MyClass
{
    public virtual int ID { get; set; }
    public virtual string SomeText { get; set; }
    public virtual int SomeNumber { get; set; }
}

...and the mapping file:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyAssembly"
                   namespace="MyAssembly.MyNamespace">

    <class name="MyClass" table="SomeTable">
        <property name="ID" />
        <property name="SomeText" />      
    </class>

</hibernate-mapping>

In this simple example, you can see the problem at once:
there is a property named "SomeNumber" in the class, but not in the mapping file.
So NHibernate will not map it and it will always be zero.

The real class had a lot more properties, so the problem was not as easy to see and it took me quite some time to figure out why SomeNumber always returned zero even though I was 100% sure that the value in the database was != zero.

So, here is my question:

Is there some simple way to find this out via NHibernate?
Like a compiler warning when a class is mapped, but some of its properties are not.
Or some query that I can run that shows me unmapped properties in mapped classes...you get the idea.

(Plus, it would be nice if I could exclude some legacy columns that I really don't want mapped.)

EDIT:
Okay, I looked at everything you proposed and decided to go with the meta-data API...that looks the easiest to understand for me.
Now that I know what to search for, I found some examples which helped me to get started.
So far, I have this:

Type type = typeof(MyClass);

IClassMetadata meta = MySessionFactory.GetClassMetadata(type);

PropertyInfo[] infos = type.GetProperties();

foreach (PropertyInfo info in infos)
{
    if (meta.PropertyNames.Contains(info.Name))
    {
        Console.WriteLine("{0} is mapped!", info.Name);
    }
    else
    {
        Console.WriteLine("{0} is not mapped!", info.Name);
    }
}

It nearly works, except one thing: IClassMetadata.PropertyNames returns the names of all the properties except the ID.
To get the ID, I have to use IClassMetadata.IdentifierPropertyName.

Yes, I could save .PropertyNames in a new array, add .IdentifierPropertyName to it and search that array.
But this looks strange to me.
Is there no better way to get all mapped properties including the ID?

+4  A: 

You could use the NHibernate meta-data API to find the mapped properties, and reflection to find all the properties.

Edit No, there isn't any other way list all the properties including the id. It isn't that hard to use:

foreach (PropertyInfo info in infos)
{
    if (meta.PropertyNames.Contains(info.Name) || info.Name = meta.IdentifierPropertyName)
    {
        Console.WriteLine("{0} is mapped!", info.Name);
    }
    else
    {
        Console.WriteLine("{0} is not mapped!", info.Name);
    }
}
Stefan Steinegger
We do this, it only happens once at app-init and dump the results on a log
Jaguar
The meta-data API sounds good...I tried to use it, but I ran into one problem (see above, I edited my question).
haarrrgh
Oh yes, you're right - it's really simple. It's a shame that I didn't think of this myself :-)
haarrrgh
+1  A: 

There are two tools I'm aware of that can help with this:

but they don't specifically address the problem you had with an unmapped property. The best solution is to write good unit tests that ensure that the properties you want to persist are persisted correctly. It's tedious but necessary.

Jamie Ide