What are the advantages/disadvantages of using NHibernate ? What kind of applications should be (& should not be) built using NHibernate ?
Advantages:
- Caching
- Simplicity in your code
- Power
- Flexibility
- Multi-database support
Disadvantages:
- Stops you having to write your own persistence code
- May reduce your knowledge of SQL
Applications you should use it for:
- Any that use a database
The high level answer is that NHibernate is in a class by itself and there is no near competition.
If you need CRUD against a database from a .NET application, you should be using NHibernate, for at least two reasons:
1) You get Linq support (which requires something like an ORM)
2) NHibernate is very mature
There are no significant disadvantages. There are other options, but those other options have significant disadvantages.
I wrote some more on this a while ago:
Since other ppl have listed advantages I will just list the disadvantages
Disadvantages
- Increased startup time due to metadata preparation ( not good for desktop like apps)
- Huge learning curve without orm background.
- Comparatively Hard to fine tune generated sql.
- Hard to get session management right if used in non-typical environments ( read non webapps )
- Not suited for apps without a clean domain object model ( no all apps in world dont need clean domain object models) .
- Have to jump through hoops if you have badly designed ( legacy ) db schema.
Disadvantages: NHibernate is not a Microsoft product and therefore will face some resistance from coworkers who haven't heard of it. Especially FOSS bigots. Configuring the mapping files and lazy/eager loading behavior can be time-consuming. If your database has a bizarre naming convention, atypical design or very strict performance requirements, more work may be required than expected.
I say this a lot but ActiveRecord is a great layer over NHibernate. It uses attributes to map the data points to class members right in the classes themselves. People are not using this thing enough.
Advantages:
- Open source
- Based on widely approved patterns
- NH is not code-generator :)
Disadvantages:
- Half-done LINQ support
- Low performance
(see for example performance and LINQ tests on ormbattle.net)
Advantages:
- Flexible and very powerful mapping capabilities.
- Caching.
- Very polished UnitOfWork implementation.
- Future query (article).
- Model classes are POCO - which effectively means you can easily implement anemic domain antipatter.
- Interceptors - you can do a kind of aspect oriented programming... Like very easily implementing audition, logging, authorization, validation, ect for your domain.
- Lucene.NET and NHibernate are well integrated with each other - gives you a very fast and effective implementation of full-text indexing.
- It's very mature and popular in enterprise environment.
- Big community.
Disadvantages:
Already mentioned learning curve. You can start using NHibernate very fast but it will take you months to master it. I'd highly recomend to read Manning NHibernate book.
Writing XML mapping can be very tedious especially for big databases with hundreds and thousands of tables and views and stored procedures. Yes, there is tools that will help you by generating those mappings but you still will have to do quite a lot of manual work there. Fluent NHibernate seem to simplify this process by getting rid of XML mappings, so is Castle ActiveRecord (AR though is impossible to use for anemic domain as you define mappings in attributes on your model classes).
Performance may be low for certain scenarious. For instance large bulk operations. For those you might have to use IStatelessSession but its awkward experience, least to say...