Are ORM's counterproductive to OO design?

∥☆過路亽.° 提交于 2019-12-04 08:25:12

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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.



来源:https://stackoverflow.com/questions/2625098/are-orms-counterproductive-to-oo-design

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!