Saturday, October 22, 2011

FluentConfigurationException: Pitfalls of Auto-Completion

Working on some code I ran into a FluentConfigurationException when trying create the session factory. With (much) more experience I probably would have been able to resolve it much faster. I’d like to share my findings with you. Maybe it’ll help you saving time.

The usual message I got was: “An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.” I followed the advice and checked the PotentialReasons collection. It had a count of 0. In other words it was empty.

Next I checked the inner exception and it was of the same type FluentConfigurationException and the message was the same. Again the PotentialReasons collections was empty. However, there was an exception inside of the inner exception. This was of the type System.InvalidOperationException. The message was: “Unsupported mapping type 'DataAccessTest.Job'”. Searching the internet didn’t yield much of an answer.

I tried a lot of different things and created even a new project. In the end I found that I was a victim of autocomplete. Here is the offending code:

public class JobMap : ClassMap<JobMap> {
   public JobMap() {
      Id(x => x.Id());

As I typed Job to provide the type parameter to the generic base class ClassMap<T> the autocomplete feature turned it into JobMap. Since this was just a test all I wanted to map was just the Id. Had I mapped other members I would have noticed that the line where it maps the id is incorrect as well. Have a look at the class that I wanted to map:

public class Job {
   public virtual Guid Id { get; private set; }

You will notice that the member ‘Id’ is a property and not a method. In the mapping class above the lambda expression ‘x => x.Id()’ tries to map a method ‘Id()’. The base class ClassMap<T> obviously has a method ‘Id()’ so was happily inserted by auto-completion as well. Had I mapped additional Job properties in the JobMap class I would have noticed that something was wrong. This way however, I learned that although auto-completion has made me much more productive there are times when it pays off to be very careful about what it does.

To complete this post here is the correct code for the mapping:

public class JobMap : ClassMap<Job> {
   public JobMap() {
      Id(x => x.Id);

Notice the correct class name (‘Job’) as type parameter for the generic based class ClassMap<T> and also the correct lambda expression now mapping a property rather than a method.


Post a Comment

All comments, questions and other feedback is much appreciated. Thank you!