tags:

views:

302

answers:

9

In OOD, design of an object is said to be characterized by its identity and behavior.

Having used ORM's in the past, the primary purpose, in my opinion, revolves around the ability to store/retrieve data. That is to say, ORM objects are not design by behavior, but rather data (i.e. database tables). Case and point: Many ORM tools come with a point-to-a-database-table-and-click-object-generator.

If objects are no longer characterized by behavior this will, in my opinion, muddy the identity and responsibility of the objects. Subsequently, if objects are not defined by a responsibility this could lend a hand to having tightly coupled classes and overall poor design.

Furthermore, I would think that in an application setting, you would be heading towards scalability issues.

So, my question is, do you think that ORM's are counterproductive to OO design? Perhaps the underlying question would be whether or not they are counterproductive to application development.

A: 

I'm answering this from a C# perspective, since that is where I do the majority of my development....

I see your point, but I think with the ability to create partial classes you can still create objects with any behavior you like and still get the power that an OR/M brings to the table for data retrieval.

jaltiere
+2  A: 

I don't believe ORM are counterproductive to OO design, unless you want to insist that persistence is an integral part of behavior.

I'd separate persistence from business behavior. You're certainly free to add busines behavior to any object that an ORM generates for you.

duffymo
A: 

From what I've seen of ORMs they do not go against OO principles - fairly orthogonal to them in fact. (for info - pretty new to ORM technology, Java perspective)

My reasoning is that ORMs help you store the data members of a class to a persistent store without having to couple to that store and write that code yourself. You still decide on the data members and write the behaviours of a class.

I guess you could abuse ORMs to break OO principles, but then you can do that with anything. You might use tooling to create skeletal data classes from a pre-existing table, but you would still create methods etc.

Brabster
+3  A: 

Case and point: Many OR/M tools come with a point-to-a-database-table-and-click-object-generator.

Yes but there are equal, if not greater numbers or ORM solutions that base themselves off your objects and generate your database tables.

If you start with data and then tramp down forcing auto-generated objects to have object behaviours, yes, you might get confused... But if you start with the object and generate the database as a secondary layer, you end up with something a lot more usable, even if the database isn't perhaps as optimised as it could be.

If you're looking for an excuse not to use ORM, don't use it. I personally find it saves me thousands of lines of code doing trivial things that the ORM does just great.

Oli
+1 Many ORM solutions also allow you to specify whatever mapping you would want between your entity classes and database tables, hence not forcing you to do one-to-one mappings from tables to "dumb" classes.
Jørn Schou-Rode
If ORM saves you thousands of lines of code--you're honestly not coding right. NEVER write boilerplate code like that. If your programmer instincts didn't force you to write some kind of automation into the system then--well why on earth not? Creating thousands of lines of boilerplate should be considered the worst sin a programmer could do--equivalent to police taking a bribe. Within the terms of doing our job what more serious sin can you commit?
Bill K
@Bill - That doesn't make much sense. You have to write the boilerplate code at least once, be it in some kind of generation tool like CodeSmith or as part of a library that you reuse. It still has to get done, at least by someone.
Mystere Man
@Mystere Man: Yes, but if that boilerplate is properly encapsulated in a library, it's not going to amount to "thousands of lines of code doing trivial things". (OTOH, there's a good chance that it *will* amount to a personalized mini-ORM.)
Dave Sherohman
No you don't have to write it @Mystere Man. You can solve problems reflectively and through configuration systems. I virtually never write boilerplate code--if you ever feel like you have to, it's either (very rarely) the language you are using or (nearly always) your level of understanding of the language you are using. Trust me, in Active Record all that boilerplate isn't just hiding under the covers somewhere, it's completely gone--so it's a solvable problem.
Bill K
It strikes me that the definition of Boilerplate code may not be understood--it is code that is copy/pasted and edited to change values. If you copy each field from an object into a different control on the screen with code, that's boilerplate. If you create data that pairs each field with the respective control then write some library code that reads the data and makes the transfers then there is no boilerplate code in the system, just library code and data. Boilerplate is never necessary.
Bill K
+2  A: 

I've also seen ORM systems which tend to go from the OO model and generate the database.

If anything, I would say ORMs are more biased towards producing good OO code than they are to producing good database code.

Ideally, a successful ORM bridges the two worlds and your application code would be great from a business domain problem-solving and implementation perspective and your database code and model would be great from a normalization, performance and ETL/reporting/replication whatever perspective.

Cade Roux
+1  A: 

As with most questions of this type, it depends on your usage.

The main ways to use ORM tools are:

  1. Define object by data, use this object throughout application code (BAD BAD BAD)
  2. Use ORM objects only for data access, define your own objects with an interpretive layer between (Much Better)

If starting from scratch a 3rd method is to design data from your object model. (Best if possible)

So yes, if you define the object by the data tables and use that throughout your code you will not be using OOD and introducing very poor design and maintenance issues.

But if you only use the ORM objects as a data access tool (replacing ADO) then you are free to use good OOD and ORM together. Sure, more code is required to build the interpretation layer, but enables much better practices, with not much more code required than old ADO code.

Carlton Jenke
The question was, do ORM tools encourage usage #1. I'd say yes.
Bill K
@Bill K and I'd say that's a sweeping generalization :)
hobbs
Yes, a generalization is what the author was asking for (kind of like voting)--addressing every experience would be incomprehensible. On the average, is it good or bad in real day-to-day use. In my history there is no question, it encourages bad OO coding on average. Always? Nope, it can be done well.
Bill K
+2  A: 

On systems where it separates your data from behavior it's absolutely counterproductive.

Orm systems tend to analyze existing database tables to create stupid "Objects" in your language. These things are not true objects because they do not contain business behavior, but since people have these structures, they tend to want to use them.

Ruby on Rails (Active Record) actually binds your data to a "Live" class--this is much better.

I've never seen a system I really liked--ActiveRecord is close but it makes a few rubiesque assumptions that I'm not quite comfortable with--the biggest being supplying public setters & getters by default.

But to sum up--I've seen a lot of good OO programmers write screwed up code because of ORM.

Bill K
+3  A: 

If objects are no longer characterized by behavior this will, in my opinion, muddy the identity and responsibility of the objects.

The objects in question do have database reading and writing as defined behavior. They just don't have much other than that.

The reality of the situation is pretty simple: object orientation isn't an end in itself, it's a means to an end -- but in some cases it just doesn't do much to improve the end result. A lot of uses for ORM form a case in point -- they are thousands of variations of CRUD applications that don't need or want to attach any real behavior to most of the data they process.

The application as a whole gains flexibility by not encoding much (if any) of the data's "behavior" into the code of the application itself. Instead, they're often better off with that as "dumb" data, that they simply pass through from UI to database and back out to reports and such. With a bit of care, this can allow a substantial level of user customization that's almost impossible to match when you try to treat the data as real objects with real behavior encoded into the application proper.

Of course, there's another side to that: it can make it substantially more difficult to ensure the integrity of the data or that the data is only used appropriately -- I've seen code that accidentally used the wrong field in a calculation, so they were averaging the office numbers instead of office sizes in square feet. Both were user-defined fields that just said the contents should be numeric. The application had no way to know that one made perfect sense, and the other didn't at all.

Jerry Coffin
+5  A: 

There's a well-known and oft-ignored impedance mismatch between the requirements of good database design and the requirements of good OO design. Most developers (in my experience) either do not understand this impedance mismatch or do not care. Since it's more common to start with the database and generate the objects from it (rather than the reverse), then yes, you'll end up with objects that are great as a persistence layer but sub-optimal from an OO perspective. (The reverse, generating the database from the object model, makes me want to stab my eyes out.)

Why are they sub-optimal from an OO perspective? Because the objects produced by an ORM are not business objects, even with partial classes and the like. Business objects model behavior. ORM objects model persistence. I'm not going to spend ten paragraphs arguing this distinction. It's something Rocky Lhotka has covered quite well in his books on Business Objects and his CSLA framework. Whether or not you like or use CSLA, I think his arguments are solid ones.

Gregory Higley
@Gregory Higley - I think you hit this on the head. I'm very familiar with Rocky Lhotka and I think he has some extremely good points. If you throw Martin Fowler into the mix, I think that ORM's will have a low initial cost of dev, but leave you with complex domain logic and subsequently a higher cost to maintain. Therein lies the problem and where my hypothesis was derived from. In other words, objects designed by behavior (as characterized by proper OOD) as opposed to data, will provide good OO design. Whereas objects derived from data will negate some of the benefits of proper OOD.
Jeremiah
@Gregory Higley - After this post was made, I realized that I had agreed with your answers in the past:http://stackoverflow.com/questions/15241/does-anyone-have-any-real-world-experience-of-csla
Jeremiah