tags:

views:

565

answers:

6

I want to use an ORM for learning purposes and am try nhibernate. I am using the tutorial and then I have a real project. I can go the "old way" or use an ORM. I'm not sure I totally understand the benefit. On the one hand I can create my abstractions in code such that I can change my databases and be database independent. On the other it seems that if I actually change the database columns I have to change all my code.

Why wouldn't I have my application without the ORM, change my database and change my code, instead of changing my database, orm, and code? Is it that they database structure doesn't change that much?

I believe there are real benefits because ORMs are used by so many. I'm just not sure I get it yet.

Thank you.

EDIT: In the tutorial they have many files that are used to make the ORM work

http://www.hibernate.org/362.html

In the event of an application change, it seems like a lot of extra work just to say that I have "proper" abstraction layers. Because I'm new at it it doesn't look that easy to maintain and again seems like extra work, not less.

+4  A: 

The basic benefit of using ORM tools is to facilitate the principal of separating the business logic from the data access in a multi-layered application. If you can successfully build a data access layer that is responsive to changes in the database (by changing your mapping), there is less overall code for you to have to muck around with when changes are made.

Separation of the layers is typically the goal of 3 tier or n-tiered applications, and ORM is a good method of doing that.

TheTXI
+3  A: 

Primary driver: less overall code to manage/maintain

CodeToGlory
It seems like more to me. That's the reason for my question.
johnny
Maybe more - but a lot of it can and will be generated, doesn't need to be manually written, and you get to work with OBJECTS vs. bare data rows and data fields. That's the biggest plus for me.
marc_s
many people believe in persistence ignorance, this way Objects know how to load and save themselves.
CodeToGlory
@johnny with a tool like NHibernate there is a rather sizable addition in an initial setup to a project but with the appropriate scaffholding and initial design considerations added to the project it allows you to basically magically create any object structure you wish and then perform all of the CRUD operations against it without writing a single line of code. You need to consider the thousands of lines of Sql you will write when not using an ORM.
Chris Marisic
A: 

1 advantage you get is that your data layer is separated from the object layer, so when you have multiple applications/projects dependent on that layer you change it once in the ORM and all applications/projects that reference the object layer don't have to change, where as without it you would have to make changes in all projects.

chrishawn
but if I change the database structure don't I have to change everything not just what you described?
johnny
+6  A: 
Marcelo Cantos
I am down voting you because I disagree with your argument that an ORM adds unneeded complexity and that yes you absolutely MUST separate your layers in software design. ORMs are very complex (if they are fully functional) because they solve a very complex problem. The reason ORMs are important is because with the support of an ORM you can correctly design an application in a way that you build your application independently of the database. Without using an ORM it is standard that the end result is the database will own the application and all associated changes will reverse up to the app.
Chris Marisic
When you have your database dictating design of your app you lose the ability to correctly model object orientated design and start letting a database dictate how you design an application which is absurd. I care nothing about the database. It is a persistent data store and nothing else. Using a database with an application is solely a means of having a persistent data store that can be queried in some fashion. This is the reason the NoSQL movement has gained so much traction recently because relational databases don't solve problems developers face.
Chris Marisic
The other prime factor is that by choosing to dumb down your application and just couple it to a database and datareaders (or worse datasets/datatables) you easily triple the amount of work required to develop an application. At this point you need to write all of the CRUD operations yourself. There is obviously tooling that can generate some of this for you but that's the backwards approach to software design and once again furthers the database will control the design of the application because most codegen tools are opinionated and will cause developers to alter their development to mesh
Chris Marisic
A: 

I responded thoroughly to the answer by @Marcelo Cantos however I will summurize the main benefits of using an ORM.

Persistence Ignorance (PI) and Domain Drive Design (DDD)

A ORM lends itself perfectly to both of these overall design patterns. For correct object orientated design you will work with hierarchical and polymorphic objects for expressing how data flows inside your application. With a good design you will frequently strive for POCO objects (plain old C/C# objects, I've seen other definitions of this word) because if I have a Person that has a List I should have just that. I shouldn't have a DataTable of persons and a DataTable of addresses that I somehow force to work with my application. Along with that I shouldn't need to tie tons of database specific logic to my objects, why should the FirstName field of my person need to be have something like [ColumnName("First_Name")] [Length(255)] [DataType(Types.Nvarchar)] or any other assortment of crazy attributes or code that defines how many database exists forced into my domain design?

Less Hand Written Code

There is a substantial reduction in the number of lines of code written when you remove the need to write an Select, Insert, Update and Delete statement for every single object in your application to save it to the database. This leaves you with only the need to write queries that mean something. Also many ORMs (like NHibernate) will include a language that is layered on top of SQL which is more defined to interact with and can be less syntactically filled of hoops.

The time this shows up fullest is consider an application that has a UserTable that is linked to every single object and for some reason you HAVE to change the primary key name or type. At this point you will potentially need to alter every single stored procedure in the database where with a correctly implemented ORM solution you will only need to alter a few configuration mappings (or possibly none) and it's done.

Chris Marisic
+6  A: 

In my experience with NHibernate it's a bit like Visual Basic: it makes easy problems really easy, but it also makes anything harder than that really hard or even completely impossible.

The basic idea is that ORM avoids having to write persistence code. There is a lot of duplication in such code, so it's very tempting to make that code generic, rather than specific to a particular business layer, and re-use it across projects. So far so good. For simple object hierarchies and business requirements this actually works really well. If your database changes, yes, you do have to change the ORM mapping files, but that's usually quite simple and you only have to make the change in one place - much easier than changing code that accesses the database.

The problem is that as your database and requirements get more complex the ORM finds it increasingly hard to keep up. So the ORM gets more and more complex. It also takes shortcuts, doing some things inefficiently, because it just isn't smart enough to figure out how to do it efficiently in all cases. What's more, because the whole idea is that it works transparently, you often can't see these performance problems until they become bad enough to affect the user experience. A related problem is that bugs are much harder to find, because you just don't know what's going on inside the ORM unless you debug it. (Yes, I've had to step through NHibernate code and it's no picnic!)

So you start bypassing the ORM for some things and use SQL directly instead. Of course, you then have to make that code work with the code that does use the ORM, which is more work. You end up writing code to load and save some objects manually and to somehow work that into the ORM code. Eventually you start wondering if the ORM is creating more work for you than it's saving - not to mention the performance and bug hunting headaches.

So if you're writing a very simple application, the kind you find in a tutorial, ORM will do a good job. If it's more complex than that then I think it's not worth it. Of course, for a simple application the absolute amount of time saved will be small as well. My conclusion: just don't bother with ORM. ORM is the path that leads to the dark side.

Evgeny