O/R Mappers: To Attribute or Xml ?
I learned a lot while I was creating my own O/R Mapper, so I thought a few lessons learned would be good to blog. I'll be adding a few of these over the next couple of days, so stay tuned for some other interesting observations.
First, I think Microsoft has made the right decision to use xml mapping files instead of attributes in code. Note that I'm not commenting on the complexity of theirs, although I suppose it may be necessary for performance. What I'm getting at is that attributes just are NOT the best approach for things that are external to the code! Attributes make perfect sense for things like unit tests, design-time only features, and aspects like logging too. But persistence mappings are very external to the code, like table and field names, and they do often change.
Why should I have to recompile my business objects just because someone changed the table or field names again? I can also easily imagine some cases where applications must work with existing databases as well -- then what? And what if I want to support multiple databases but I must make some schema changes for performance reasons? Finally, I've even seen people say they want to change their schemas on the fly -- that's weird, but what if? Only external xml mappings allow you to handle these.
So far I've stayed away from some of the other reasons that attributes are not the best choice for persistence, but why not list them anyhow just to stir up the kettle. My pet peeve -- persistence attributes make my business objects very ugly for no important development reason. Speaking of development, I can imagine that many shops will have different people creating the business objects from the people that will be worried about the mappings. Finally, if you use a GUI helper to create the mappings then it seems obvious they should go in a separate file.
I can't think of even one reason to prefer attributes other than the argument that external files get messed up. I know attributes are cool in .NET, especially since its one thing that Java does not have, but I do not think that they make much sense for object-relational mapping.
What do you think? I chose xml mappings, not attributes.