Code Generators or ORM?

What do you suggest for a data access layer? Using ORMs like Entity Framework and Hibernate OR Code Generators like Subsonic, .netTiers, T4 etc.?

+4
source share
5 answers

This is not a problem for me, you are generating code.

I'm going to get a little distracted from the topic, because the game has more serious errors. The fallacy is that this ORM framework solves the mismatch of object-relational impedance. This statement is a false lie.

I believe that the best way to resolve the object-relational impedance mismatch is to use OOP exclusively and use the object database, or use the idioms of the relational database exclusively and ignore OOP.

The abstraction "all is a table" for me is much more powerful than the abstraction "all is a class". It requires less code, less intellectual effort and leads to faster code when you encode a database, not an object model.

It seems obvious to me. If your application is data driven, then of course should your code also be data driven? But to say that this is extremely controversial.

The central problem here is that OOP becomes a really leaky abstraction when used in conjunction with a database. Code that looks completely reasonable when writing in OOP idioms looks completely crazy when you see traffic that generates code in a database. When this mess becomes a performance issue, OOP is the first victim.

There really is no way to resolve this. Databases work with datasets. OOP focuses on class instances. The attempt to marry two always ends in divorce.

Therefore, to answer your question, I believe that you should generate your classes and try to make them display the base structure of the database as close as possible.

+8
source

It may be debatable, I always felt that using code generators for ADO.NET piped fundamentally solved the problem incorrectly.

At some point, I hope, not too long after studying the connection strings, SqlCommands, DataAdapters and all this, we notice that:

  • Such a code is ugly
  • Very boring to write
  • It is very easy to skip something if you do it manually.
  • It should be repeated every time you want to access the database.

So, the problem to solve is “how to do the same thing many times quickly”?

I say "No".

Using code generators to quickly complete this process means that you have a ton of code, all the same, in all business classes (or at the data access level if you separate it from your business logic).

And then, if you want to do something in common (for example, using the stored procedures of a track), you will have to configure the code generator if it does not already have the necessary function. And even if this is the case, you still need to restore all the time.

I like to do something once, not many times, no matter how fast I can do them.

So, I turned my own data access class, which knows how to add parameters, configure and close connections, manage transactions and other cool things. It was necessary to write it only once and call its methods from the Business object, which requires that part of the database consist of one line of code.

When I needed to get the application to support multi-threaded database access, it only needed to change the data access class, and all business classes just did what they already did.

+3
source

There is no right answer, it all depends on your project. Since Simon indicates that your application is data-driven, then it may make sense, depending on the size and complexity of the domain, to use the non-oop paradigm. I had great success in creating a system using the Transaction Script template, which transmitted XML messages throughout the system.

However, this system began to break down after five or six years, when the application increased in size and complexity (5 or 6 websites, several web services, many COM + components, outdated and .net code, 8 + databases with 800 + tables 4000+ procedures). No one knew there was anything, and the replication was rampant.

There are other ways to make things easier, then OOP; however, if you have a very complex domain, then the hainvg rich domain model is ideal IMHO, as it allows business rules to be expressed in good encapsulated components.

To answer your question, avoid code generators if you can. Code generators are a recipe for disaster, but if you go with code generation, do not modify the generated code. Also, make sure that you have a good process that makes it easy for developers to get new generated code.

I recommend using either the following: ORM or the manual handle of a light DAL. Currently, I am transitioning to a project on nHibernate with my handmade DAL and have great success; however, I like to be able to use any option. In addition, if you properly share your concerns (“Accessing data from a business layer from a presentation”), you may have one service level that can talk to Dao (Data Access Object), which for one object is ORM, and for another - manually). I like this flexibility because it allows you to use the best tool for the job.

I like nHibernate over a handled DAL because, although my DAL abstracts most of the ADO.Net code, you still have to write code that takes a data reader for an object or object and creates parameters.

+1
source

I always preferred to go on generating code code, especially in C #, where you can use advanced classes to add functionality to underlying data objects.

0
source

I hate to say that, but it depends. If you find an ORM tool that suits your needs, check out it. We developed our own system in small steps when developing the application. We use C ++, and there are not many tools there. As a result, we got an XML description of the database from which the SQL script was generated, as well as the base layer of the object and metadata.

Do your homework and evaluate these tools and you will find the right one for your needs.

0
source

All Articles