Ayende @ Rahien

It's a girl

On SubSonic & NHibernate

I recently got this email, and the author has kindly given me permission to post the email and my response to it:

At this moment, I will be suggesting to my project team in my company to use ORM, hopefully to make our software development faster especially on the data layer side. We use LLBLGen (the old one), and we really have plenty of stored procedures which were hard to maintain.

I also noticed about subsonic today. I watched the videos and were impressed about it.

1)  However I am not too clear about the difference between subsonic and nhibernate. It looks so alike, furthermore both of them have so much similarities with LINQ. What is your view on this? Most people mentioned that nhibernate was bloated.. something like that which I wasn't clear about it.

2)  When LINQ release, I should scrap off either subsonic or nhibernate for this platform. As LINQ is built into the language itself, it is easier to extend. I am aware that there is something like LINQ for NHibernate. Are these 2 the same technology or ???

3) Is nhibernate ready to be used in real enterprise commercial projects? Will there be any payment to it? Or it is completely free?

4) Last but not least, I am not too sure whether nhibernate does code generation like subsonic. But I did heard that nhibernate relied on codesmith, which you needed to purchase codesmith.

Well, obviously I prefer NHibernate, that has absolutely nothing to do with anything about SubSonic, I am simply strongly biased toward NHibernate. That said, let me try to summarize what I think the difference between the two are.

NHibernate and SubSonic takes two different approaches toward OR/M, NHibernate has the "objects is all you see" approach, while SubSonic is much closer to the way that the database view of the world. You still deals with some of the stuff from ADO.Net directly, but SubSonic takes most (all?) of the pain from that. From going over the docs, it looks like SubSonic has a fairly complete support for object view of the world.

Both are valid approaches, and choosing between them is a matter of what you are more comfortable with, and what do you want to accomplish. If you are trying to build a Domain Driven application, NHibernate is the preferred approach, if you are building on an existing database, with existing stored procedures, you should probably see if SubSonic would be right for you.

I would probably not choose that, but I am biased, as I said. I should mention that NHibernate is supplying the OR/M bits only, SubSonic seems to do more than that, code generation, integration with EntLib, WebForms controls, etc.

Now let me try to answer the questions themselves.

1/ There is a difference between Linq and the OR/M that are built on Linq (Microsoft & Naming again). Linq is a set of extensions to the language, which opens up interesting approaches for set based logic in imperative language. In other words, it is wonderful syntactic sugar, and I am looking forward to be able to making use of that in creative ways.

Linq for Sql is an OR/M whose query language is Linq queries, as far as I can see, this is a good approach from Microsoft, but it is limited to SQL Server only, and lacks some of the essentials features that I, as an advance user of OR/M needs. For most basic scenarios, I believe it should suffice.

NHibernate bloated... I haven't heard this before, and I would be interested to know by what criteria.

2/ No, you certainly should not. As I explained, Linq and the Linq for Sql product are two different things. From the way Microsoft is positioning Linq for Sql, I would assume that it would be harder to extend. Linq for NHibernate is a way to query NHibernate using Linq, that is all. You get the nice Linq syntax and the power from NHibernate. I would assume that SubSonic would have similar features (if it doesn't have it already).

3/ NHibernate is more than ready. It is the backbone for quite a lot of enterprise commercial projects right now. It is completely free, and require no payment. Commercial support can be had from JBoss or my company.

4/ NHibernate doesn't do code generation, that is outside the scope of the OR/M. There are several code generators that will output NHibernate mapping and classes, including free ones. CodeSmith certainly has some good templates for NHibernate, but it is not the only one by far.

At the end, it comes done to evaluating whatever you can be effective with the tool in the approach that you prefer. You mentioned that you are currently suffering from pain as a result of hard to maintain stored procedures. Take NHibernate for a spin on your database, see if you are productive, then take SubSonic and do the same. That are numerous sources of information about both, so it shouldn't be a problem to get started.

Either way, have fun, and remember to choose by what will make your life easier, both now and in the long run.

Comments

Haacked
06/03/2007 01:29 AM by
Haacked

This is a great balanced comparison!

Subsonic has its own ActiveRecord pattern and query engine. But it wouldn't be impossible to create NHibernate templates with Subsonic to take advantage of its code generation. But it would sort of defeat the purpose of both, as they do take different "views" of the world.

For legacy databases with stored procs, you can look at my article here: http://haacked.com/archive/2007/05/29/strongly-typed-stored-procedures-using-subsonic.aspx

Joe
06/03/2007 02:09 AM by
Joe

Just curious - do you know of any large / well-known companies using nHibernate (or any commercial applications)? Im just getting started with it (and love it), and am more than a little tired of the "some crap he downloaded off some guys blog" remarks.

Ayende Rahien
06/03/2007 05:10 AM by
Ayende Rahien

Joe,

NH is probably one of the more popular OR/M for .NET.

It is used in many commercial products, has many people that are familiar with it, have commercial support options etc.

It is certainly not something that fit this remark.

Hibsonatic
06/03/2007 09:29 AM by
Hibsonatic

Which 'well-known' companies?

And which well-known project that isn't another toy?

Oh, it must be a 40,000 download out of which 20000 played with it and 10000 ended up testing so 5000 that tried to push it and 2500 that succeeded selling that bloatware to their small, toy-making companies. 1250 probably made it and are feeling GOD-LIKE.

NHibernate is incredibly inefficient, incredibly punishing all RAM you have, incredibly silly to set up for advanced scenarios and makes many DB experts (which none of the people blogging about ORMs are) laugh, read Celko's books and you will get the picture.

Ayende Rahien
06/03/2007 09:59 AM by
Ayende Rahien

Hibsonatic,

I find your manner of speaking insulting.

We don't track our users, so accurate numbers are hard to produce, and would likely violate clients confidentiality.

You can check here to see some of the people who are using it:

http://forum.castleproject.org/viewforum.php?f=6

You are free to your own opinion, but I would suggest that you put out some proof for NHibernate being "incredibly inefficient, punishing to RAM" etc.

I have been using NHibernate for a number of years now, and I have found it is a pleasure to work with, and it scales very well in both performance and the things it can do.

PandaWood
06/03/2007 12:24 PM by
PandaWood

NHIbernate is 'very silly to set up for advanced scenarios' - Hibsonatic

I think Hibsonatic's post is very silly for an advanced blog.

Jim Geurts
06/03/2007 03:27 PM by
Jim Geurts

Joe: We're using it for Property Center (http://adminproperty.com) and couldn't be happier.

Hibsonatic
06/03/2007 06:30 PM by
Hibsonatic

I see how it scales.

First it takes about 6 seconds to start up which makes it useless in some scenarios at least and it all lightweight scenarios, period. CLR contributes a bit but that is not the argument here.

Second, it takes on 50MB for doing absolutelly nothing on a moderate size database, with say a 100 tables and almost no data in it whatsoever. Hello, almost no data whatsoever, great work.

Third, it produces some awful, awful design and assumptions on DB technologies (yes, that is the correct word.)

It is a toy for God's sake.

For an advanced blog, perhaps you should see what an equivalent 2nd undergrad class student does with for example a toy like Delphi, it beats all this Java copycat nonsense. They also have the source and more.

These students also have subjects on the lines of System and Database Design, and it is sad to see many mistakes repeated in 'grown up moa ad-hoc architect' libraries and advanced meta-blogs

You wanted facts, now go sit down and see for yourself the monster it is. And If you need more try it on say on something on a scale of a medium Great Plains database, your OS will blue-screen or run-out of swap file space.

Violate client confidentiality? Sure, just like using Oracle or SQL Server violates confidentiality.

It seems to me you are living in a dream NHibernate is a solution to anything serious (unless you got 1 million dollar hardware to throw away and I am sure you will find a more appropriate application for that kind of overhead, like use databases the way you are meant to).

Play with it and see for yourself. And rather than read blogs, read books from people designing and doing DB work for over 25 years, it helps.

Arne Claassen
06/03/2007 06:53 PM by
Arne Claassen

I was just evaluating SubSonic last week for a project that didn't need a full entity model on the DB and the "magic" it performs to create your DB layer was appealing. After generating the codebase for my schema and started working with it, I wondered what benefit it gave me over DLinq, aside from being released already. So I fired up Orcas and build a codebase against my schema using the DLinq designer. Then I wrote code against both. In the end, I think both are really just a Data Access Layer, a thin, strongly-typed veneer on top of your schema, not OR/Ms and for that approach I liked the way I wrote code with DLinq better. I know I'm not giving SubSonic it's due for the scaffolding and advanced zero-code capabilities, but I personally wouldn't be using them, since i don't like my schema to be that exposed to the UI.

For larger projects that do require an Entity model, I used to write my own ORMs (and hope to never do that again) and am currently looking at nHibernate vs. LLBLGen (i think a better comparison than SubSonic). Right now LLBLGen is winning out because I just generally design with a Schema first mentality and LLBLGen's Designer makes the adaptation of my schema to an entity model very smooth. I haven't seen a good tool for nHibernate to let me do Schema first design, but if there is, i'd love a pointer to it.

Ayende Rahien
06/03/2007 06:54 PM by
Ayende Rahien

Hibsonatic,

I am willing to engage in a debate, but I would demand that it would be a debate without insults or derogatory language. Be informed that further comments with the same language will be removed.

If you are unable to do so, please refrain from posting any further comments.

First it takes about 6 seconds to start up

Not in my experience, and without false modesty, I am seeing some of the biggest NHibernate solutions out there.

It is possible that you are doing something the wrong way, such as opening and closing session factories instead of using a single session factory and opening and closing sessions. That is a somewhat common mistake.

Second, it takes on 50MB for doing absolutelly nothing on a moderate size database

Sounds like you are mixing NHibernate & the working set of a .Net application. Of course, since you can so easily reproduce it, I will be happy to look at any profiler output.

Third, it produces some awful, awful design and assumptions on DB technologies

I would like to hear concrete discussion on this topic, since no one has brought this particular subject before up before. What awful design? What bad assumptions?

You wanted facts

Yes, I am still waiting for them.

Violate client confidentiality? Sure, just like using Oracle or SQL Server violates confidentiality.

You can be sure that Microsoft or Oracle doesn't go and say Xyz Inc. is using our products without getting the agreement of the guyts at Xyz Inc. first.

And If you need more try it on say on something on a scale of a medium Great Plains database

I am using NHibernate on a variety of databases, anything from a three tables sqlite file to Oracle databases that include tens of thousands of tables to Mainframe DBs that holds temporal data from the last 50 years and anything in between.

I assure you, neither the OS or the swap file are issues.

Play with it and see for yourself

I would rather get my work done, using NHibernate, for mission critical stuff, where my ass is on the line if something goes wrong.

It have met all my needs and more.

I am very happy that I have made this choice.

Hibsonatic
06/03/2007 06:55 PM by
Hibsonatic

The client list provided on that site is underwhelming, or better put, an appropriate example who takes it seriously: Hobbyist and Hobbyist Architects and Hobbyist Advanced Architects.

Sad.

Ayende Rahien
06/03/2007 06:59 PM by
Ayende Rahien

Arne,

I would like to hear more about your experiences with DLinq. My understanding it that is a simplified product, but still an OR/M in the terms that I am thinking about.

GenWise studio has a fairly complete product that generate NHibernate mapping. There is Active Writer, which can do the same.

Arne Claassen
06/03/2007 08:13 PM by
Arne Claassen

Ayende,

I will look at GenWise and Active Writer. Thanks for the pointers.

As to DLinq and OR/M, I am probably mixing my definitions, as i usually equate OR/M as building DALs with Entity models, which I guess isn't the formal definition.

As such DLinq generates 1-to-1 object mappings to the Schema. This level is not something I'd want to expose as an object model to the rest of an application, as it usually exposes too much of the DB implementation. I generally try to build my API to consist of Business Objects that either encapsulate or proxy my DAL so that they can be used in the way that the Business Logic dictates without exposing the underlying data structures, such as mapping tables, that are required to to allow for complex Entity relationships to be represented in the DB.

So, going by my own definition (which I need to just discard) the MS Entity Framework is more along the lines of what I usually think of as OR/M, rather than DLinq. That said, the directions the Entity Framework is taking doesn't necessarily make me want to wait for it to make it to production.

LLBLGen is very appealing because it does discover m:n relationships and builds them into the generated code, which I can then either proxy or extend using partial classes. I can't intelligently comment on whether nHibernate fits these needs, since lacking a mapping tool, I've not given it enough of my time. Given your tool recommendations, that's hopefully going to change now.

Arne Claassen
06/03/2007 08:24 PM by
Arne Claassen

Something I forgot to mention is that I do want to draw a distinction between Linq and DLinq.

I think DLinq is a great way to get rid of that hardcoded SQL still to prevalent in many applications, but it means that unless hidden behind another layer, it will still force API changes every time the DB changes.

Linq as a language feature on the other hand is wonderful and I'm glad MS made the distinction between the two. As an end user being able to consume business logic via the Linq syntax without having to worry how the objects deal with the request is immensely appealing. I hope that it will stand up to real world needs to the extend that other ORM's support Linq for querying their object models as well.

Adi
06/03/2007 09:47 PM by
Adi

Oren

Although I don't agree with other claims, Hibsonatic was right to ask you for well-established companies using NHibernate.

While trying to "sell" this tool to my former employer he wanted to talk with others already using it in Israel, and after asking around a bit (I didn't know you blog back than) the only reply we got was from one company "we started using it but the learning curve was very steppe".

Most commercial companies list in their site a list of customers, and I actually think adding such a list to the NHibernate page could be a great selling method.

Adi
06/03/2007 09:48 PM by
Adi

Woops - I meant "steep" and not "steppe"

ewas
06/04/2007 01:32 PM by
ewas

Not in my experience, and without false modesty, I am

seeing some of the biggest NHibernate solutions out

there.

Can you be more specific here, only an idea what constitues or what the numbers are on 'biggest'. I am not looking for anyone to disclose any sensitive information if there is any; I doubt it.

tens of thousands of tables to Mainframe DBs that

holds temporal data from the last 50 years and

anything in between.

Tens of thousands? I've never heard of an Oracle database that has more than 20,000 tables and I have spoken to people that work with banks, retailers, pharmas and few more. Perhaps times have changed but that number really is bugging me, I cannot believe it.

Does NHibernate mapping really deal with 20,000 tables? Perhaps you have xml files you can randomize and demonstrate this is the case? I also wonder how long does it take to start up or, better, say see what it is half way through application running time (reproduce some xyz case).

There isn't really anything sensitive about the following if I may ask directly: What is your working set penalty when working with say 10%, 25% and 50% of that number?

Most importantly, does anyone measure scalability of a solution compared against database size, and is it possible to see NHibernate utilise more than the than the database itself? How easy is it to get to that scenario?

Has anyone tried this?

Mainframe DBs that holds temporal data from the last

50 years and anything in between.

Really, mainframe DBs? There are not many out there of that kind and I again have trouble with this because some dialect of NHibernate would have to talk them.

One big name is also what I am looking for, and most of them do not touch runtime or ADO.NET or similar.

Thanks for your reply in advance.

Ayende Rahien
06/04/2007 01:43 PM by
Ayende Rahien

ewas,

That is going to take some time to answer, and I will try to get

mainframe DBs

NH has a AS/400 dialect, actually.

But what I was talking about was imported MF DB, since the one that I had in mind doesn't have relational query layer.

goodwill
06/04/2007 03:24 PM by
goodwill

ewas,

I think Ayende (correct me if I am wrong) includes Hibernate client. I can say if you include Hibernate there are bank level stuff are actually running against it- I have friends working in HK had similar level of coverage.

I really don't know if NH/CasteAR has big stuff running on it. But I think its reasonable to believe it would work better than any kind of ORM exist (something like LLBLGen/SubSonic, for example), due to the great bleed from JBoss/Red Hat. So you may try to position yourself this way:

  1. IMHO, dont try SubSonic, the only thing which use SubSonic extensively I have seen so far is CSK (commerce starter kit), and this is .... I don't comment, retrieve the source yourself, try to modify, come back and tell me :)

  2. LLBLGen- when you see code gen in ORM, thats not a good sign. I have been doing such sick thing in my old frameworks, and maintainability becomes a big deal sooner when your domain model expand. Bug fixing is hard if you depend a lot on code gen, as code gen does not patch their code automatically :)

  3. If you think the MS way, use ADO.NET in raw, no one will challenge you, but you would get really upset if you are expecting ORM based solution.

So if you ask me, you don't have too much choice, and NH have the following advantage:

  1. Reasonable backup from JBoss and active OSS developer

  2. I don't think you have that much choice other than NH /Castle AR if you are using ORM in .net, honestly.

  3. You have the source, its way better than closed source ORM solutions, don't even consider one. You could eventually come across issues you can't patch without the source.

And, don't believe those 'It's from MS' way of doing things, DLinq for example, while it looks sweet in some stuff, Ayende has spent a lot of posts on explaining the issues, and I think he has clear evidence on these issues. While his opinion might not be 100% correct, its much more valuable than some other 'funny' one in this thread, which I would not spend time to consider if anybody ever use such way to 'pursuade' me.

And- if you still have doubts, why not setup a smaller scale pilot test first? Test yourself is way much better as only you will know the specific scenario you would come across.

goodwill
06/04/2007 03:27 PM by
goodwill

s/CasteAR/Castle AR/

s/bleed/breed/

Forgive my typo :)

Willie Tilton
06/04/2007 06:04 PM by
Willie Tilton

I think guys like Hibsonatic are just angry that it's getting easier to deal with data. Call me one of the hobbists as well as I've used the above frameworks and more over the years with no problems in professional applications that I've built from the ground up. NHibernate and SubSonic make it way easier to process data, and some people can't deal with it.

I prefer to crank out websites, and if there's scalability issues, I would just revisit it later to make performance modifications. I always thinks it's funny how that situation has never happened. Course I'm not as cool as Hibsonatic who has worked on databases the size of the moon for the past 25 years, but I pull in a pretty good paycheck so I'm content with life.

Rob Conery
06/04/2007 08:06 PM by
Rob Conery

Nice writeup Oren! I think you hit the nail on the head with the differences and if I could add one thing - NHibernate is much, MUCH better if people prefer to go the full ORM route. I've tried to take a more balanced approach (as I have my issues with ORM :) and as you mention, both definitely have their advantages.

Nice writeup and thank you for taking the time to do it :).

goodwill
06/05/2007 01:25 AM by
goodwill

Rob,

Long time no see :) I am one of the trouble making guy on your forum on the ORM issues. It seems like you are right to say so- its probably a different favor issue when talk about going NH or SubSonic depends on the ORM sickness we have (I am very sick and addicted, so SubSonic is not my cup of medicine :P)

I have interest to know thought- what makes you feel so allerged on going full ORM way? I have been writing my own ORM couple years ago since VB6 and I never looked back, so it just makes me wonder what had stopped you from using a full ORM approach.

P.S. I DO still have issue with CSK, sorry about that :( Still appreciate your effort though. I hope CSK one day will become MR + ActiveRecord :P

goodwill
06/05/2007 01:25 AM by
goodwill

Rob,

Long time no see :) I am one of the trouble making guy on your forum on the ORM issues. It seems like you are right to say so- its probably a different favor issue when talk about going NH or SubSonic depends on the ORM sickness we have (I am very sick and addicted, so SubSonic is not my cup of medicine :P)

I have interest to know thought- what makes you feel so allerged on going full ORM way? I have been writing my own ORM couple years ago since VB6 and I never looked back, so it just makes me wonder what had stopped you from using a full ORM approach.

P.S. I DO still have issue with CSK, sorry about that :( Still appreciate your effort though. I hope CSK one day will become MR + ActiveRecord :P

siftguard
06/06/2007 06:45 PM by
siftguard

This is my first comment, first off yes I use AR/NHibernate in enterprise environments since the beginning of the year and our struggles were difficult but well worth it as well. Good bye errors due to typing in extremely long SQL statements and hello to strongly-typed AR objects and persistance. We use the latest AR builds with NHibernate 2.0.1 for Nullables support, but currently still on our wishlist is Generics support for ActiveRecordBase.FindAll() (is it in yet and I didnt know?)

As most of the people who stumbled across SubSonic, I too came across it via CSK. SubSonic's screencasts look amazing, but I am skeptical of its long-term maturity. Over the last 5 months we have used AR/NH in a variety of MySQL/MSSQL/Oracle environments and using the various options like GUID, HiLo, Store Procedure for the primary key has been smooth sailing (did I mention we had to read C# example docs from the wiki and adapt to our poor cousin VB.NET? arrghh!!! :P) Even multiple databases have worked like a charm.

Why I even looked at SubSonic are 2 pain points

1) Composite Keys. Legacy databases we work with tend to use composite keys (seemed the rage back then I guess), and putting up a proper AR model for this would kill us. (see the wiki in Castle on this one)

2) I miss DataReaders and native SQL commands. I have encountered times when I have a 100 line SQL statement (ok thats abit too big, maybe 6 lines), and I have it all modelled properly within my SQL Management Studio/SQL Developer and its giving me the proper result. Now I just want to use this native SQL statement in my NHibernate sessionFactory already instantiated. Let me override HQL, let me just put in database-specific SQL statement and return me a DataReader please!!! Don't make me dig into HQL or make me do iterative loops around my AR objects to find the result! :( My solution is now to open up a ADO.NET native connection and run my complex SQL statements (usually reporting) there, or another way was to code it as a stored proc and execute it via HQL and retrieve the columns.

I love NHibernate but I would love some "hybrid" support for IDataReader for days when an OR/M would be difficult to do your 10 JOIN SQL statement.

Another plus side for using NHibernate. Going to see your DBA and telling him you are using NHibernate (thats like java Hibernate for .NET) has a stronger name recognition than saying you are using "d00dads" :)

Ayende Rahien
06/06/2007 06:50 PM by
Ayende Rahien

@siftguard,

Try using: ActiveRecordBase(of T) instead

Composite Keys.

Can you explain? It is not really hard to do.

let me just put in database-specific SQL statement

Well, you have access to the connection, obviously.

In addition to that, what is wrong with SimpleQuery?

siftguard
06/06/2007 07:12 PM by
siftguard

@ayende

Thanks for the Generic support, let me load it up later!

Composite Keys

Nothing "really hard", but with 285 tables spanning 14,000+ fields and all the tables have composite keys, instead of separate classes, being able to tag the properties <PrimaryKey(PrimaryKeyType.Composite)> would be nice =)

Native SQL statements

SimpleQuery does HQL statements, and having a nice wrapper for "ugly" native SQL statements wrapped into a IDataReader is perhaps something on my wishlist. (Similar to the SubSonic QueryCommand for writing native inline SQL statements)

Thanks for the prompt reply!

Ayende Rahien
06/06/2007 07:20 PM by
Ayende Rahien

285 tables

Code generation to start with, I would say.

SimpleQuery does HQL statements

And SQL ones, check the overloads for the ctor

Joe
06/07/2007 01:17 AM by
Joe

Just out of curiosity - has anyone downloaded and played with Developer Express' ORM? If so how does it compare?

Ayende Rahien
06/07/2007 04:42 AM by
Ayende Rahien

@ Joe,

literally the first time that I have ever heard about it.

Bryan Tidd
08/01/2007 05:46 PM by
Bryan Tidd

Can you elaborate on your reply to Siftguard's post? I am getting ready to rewrite some applications in .NET from another platform. It is a complete departure for my team. We will be dealling with Siftguard's pain as well.

We have chosen the Castle ActiveRecord / NHibernate approach based on what we have seen as both development productivity and runtime efficiency in initial testing. But the big apps are going to be full of complex key based tables and long / multi joined SQL statements.

Any ideas or guidance will be appreciated.

PS I like the fact that your blog is open, positive, and informative and that hate speech is not tolerated. I have seen many blogs and forums that could learn form your example.

Comments have been closed on this topic.