Rajeeshcv.com

Sharing my knowledge

Abstractions can also put you in trouble

Posted on: 19 Nov 2011 | Filed under: NHibernate, .NET | No comments yet

We all have enjoyed the beauty of abstracting out functionality, so that it simplifies the underlying complexity. Sometimes it can come back and hit you on the forehead like a boomerang if we don’t know what is going underneath those hidden layers.

Recently it happened to me with the LINQ provider. If you have a data source then you can build your own Query provider on top of it, so that the users can access your data using the LINQ statements without understand how it is fetched.

For e.g. Linq2Twitter it provides the IQueryable interface which you can use fetch the twitter API without understanding the REST API’s exposed by the Twitter.

Similarly Ayende has created a Linq provider for NHibernate (NHibernate.Linq) which hides complexities of writing Criteria API’s.

This is what happened to me – we had a repository method which is using the NHibernate.Linq and it returned an IQueryable interface of domain model.
Like to the below code(only an example, this is not from the actual codebase itself)

public IQueryable<Customer> QueryableCustomers()
{
    session = SessionFactory.OpenSession();
    return session.Linq<Customer>().AsQueryable();
}

 

In the consumer part (Action method in the controller), we are filtering the customer with a name containing particular string using the Contains method

this.dataProvider.QueryableCustomers().Where(x => x.Name.Contains("custo")).ToList();

 

Everything worked as expected, but one day the filtering stopped working, I couldn’t figure it out initially, but when I looked into the “QueryableCustomers()” method, it has been changed to something like this

Read more...

FluentConfigurationException: An invalid or incomplete configuration was used while creating Session Factory.

Posted on: 01 Jan 2010 | Filed under: NHibernate, TDD | No comments yet

I got this exception when I tried to run my Repository tests and couldn't find the exact reason why these tests failed.

All the unit tests where passing before I removed the default constructors from Domain Model, when I reverted the default constructors back, everything started working again..... !!!

Do you know the exact reason for this?

Update: In order to support lazy loading, NHibernate needs to create proxy objects. Those proxy classes derived from the actual domain model class itself, and they are instantiated using the default constructor. So the parent class also should have a default constructor.