views:

750

answers:

5

I come from a java background.

But I would like a cross-platform perspective on what is considered best practice for persisting objects.

The way I see it, there are 3 camps:

  • ORM camp
  • direct query camp e.g. JDBC/DAO, iBatis
  • LINQ camp

Do people still handcode queries (bypassing ORM) ? Why, considering the options available via JPA, Django, Rails.

+3  A: 

I am currently reading up on persisting objects in .net. As such I cannot offer a best practice, but maybe my insights can bring you some benefit. Up until a few months ago I have always used handcoded queries, a bad habit from my ASP.classic days.

Linq2SQL - Very lightweight and easy to get up to speed. I love the strongly typed querying possibilities and the fact that the SQL is not executed at once. Instead it is executed when your query is ready (all the filters applied) thus you can split the data access from the filtering of the data. Also Linq2SQL lets me use domain objects that are separate from the data objects which are dynamically generated. I have not tried Linq2SQL on a larger project but so far it seems promising. Oh it only supports MS SQL which is a shame.

Entity Framework - I played around with it a little bit and did not like it. It seems to want to do everything for me and it does not work well with stored procedures. EF supports Linq2Entities which again allows strongly typed queries. I think it is limited to MS SQL but I could be wrong.

SubSonic 3.0 (Alpha) - This is a newer version of SubSonic which supports Linq. The cool thing about SubSonic is that it is based on template files (T4 templates, written in C#) which you can easily modify. Thus if you want the auto-generated code to look different you just change it :). I have only tried a preview so far but will look at the Alpha today. Take a look here SubSonic 3 Alpha. Supports MS SQL but will support Oracle, MySql etc. soon.

So far my conclusion is to use Linq2SQL until SubSonic is ready and then switch to that since SubSonics templates allows much more customization.

MortMan
Re: EF being limited to MS SQL - out of the box - yes, but there are 3rd party partners developing providers for other RDBMS. So for example if you want ORACLE then a company called DevArt has both EF provider for ORACLE and also a LINQ-to-ORACLE implementation.
rohancragg
+3  A: 

There is at least another one: System Prevalence.

As far as I can tell, what is optimal for you depends a lot on your circumstances. I could see how for very simple systems, using direct queries still could be a good idea. Also, I have seen Hibernate fail to work well with complex, legacy database schemata, so using an ORM might not always be a valid option. System Prevalence is supposed to unbeatingly fast, if you have enough memory to fit all your objects into RAM. Don't know about LINQ, but I suppose it has its uses, too.

So, as so often, the answer is: know a variety of tools for the job, so that you are able to use the one that's most appropriate for your specific situation.

Ilja Preuß
system prevalence assumes incorrectly that if you can fit all your objects into RAM, it will work fast. well, it won't. there are reports where even modern garbage collectors go nuts when they have to handle millions of objects. also these days, if you have enough memory in your database server, you get similar and often even faster performance than using system prevalence as databases are optimized for performance while your general purpose language and garbage collector is not.
lubos hasko
+6  A: 

There is no one best practice for persistence (although the number of people screaming that ORM is best practice might lead you to believe otherwise). The only best practice is to use the method that is most appropriate for your team and your project.

We use ADO.NET and stored procedures for data access (though we do have some helpers that make it very fast to write such as SP class wrapper generators, an IDataRecord to object translator, and some higher order procedures encapsulating common patterns and error handling).

There are a bunch of reasons for this which I won't go into here, but suffice to say that they are decisions that work for our team and that our team agrees with. Which, at the end of the day, is what matters.

Greg Beech
+2  A: 

The best practice depends on your situation.

If you need database objects in table structures with some sort of meaningful structure (so one column per field, one row per entity and so on) you need some sort of translation layer inbetween objects and the database. These fall into two camps:

  • If there's no logic in the database (just storage) and tables map to objects well, then an ORM solution can provide a quick and reliable persistence system. Java systems like Toplink and Hibernate are mature technologies for this.

  • If there is database logic involved in persistence, or your database schema has drifted from your object model significantly, stored procedures wrapped by Data Access Objects (with further patterns as you like) is a little more involved than ORM but more flexible.

If you don't need structured storage (and you need to be really sure that you don't, as introducing it to existing data is not fun), you can store serialized object graphs directly in the database, bypassing a lot of complexity.

Dan Vinton
Re: the first camp, I don't agree that it's easier with an ORM solution. There's a lot of setup in the build system to get it to work correctly.
ashitaka
+2  A: 

I prefer to write my own SQL, but I apply all my refactoring techniques and other "good stuff" when I do so.

I have written data access layers, ORM code generators, persistence layers, UnitOfWork transaction management, and LOTS of SQL. I've done that in systems of all shapes and sizes, including extremely high-performance data feeds (forty thousand files totaling forty million transactions per day, each loaded within two minutes of real-time).

The most important criteria is destiny, as in control thereof. Don't ever let your ORM tool be an obstacle to getting your work done, or an excuse for not doing it right. Ultimately, all good SQL is hand-written and hand-tuned, but some decent tools can help you get a good first draft quickly.

I treat this issue the same way that I do my UI design. I write all my UIs directly in code, but I might use a visual designer to prototype some essential elements that I have in mind, then I tear apart the code it generates in order to kickstart my own.

So, use an ORM tool in any of its manifestations as a way to get a decent example--look at how it solves many of the issues that arise (key generation, associations, navigation, etc.). Tear apart its output, make it your own, then reuse the heck out of it.

Rob Williams
Rob, Thanks for your comments. It's very enlightening. I also prefer to write my own SQL. But almost all projects that I enter into, they prefer to use an ORM. That 40 million/day transactions system you mentioned, what platform was it running on?
ashitaka
Rob Williams