Code Generator vs ORM

I wrote a code generator that generates POCOs and repositories for a given SQL Server / CE database. Nothing unusual, just simple CRUD routines using the classic ADO.Net. My question is why should I use ORM like L2S / EF4 over a custom code generator? Every 2 or 3 years, Microsoft sends some new technologies / data access technologies, and I know a lot of developers who cannot always support the latest technologies, but each of them knows the classic ADO.Net, how to modify existing code and ways to develop new functionality . ORM tools bring something that is needed today, or can I stick with the classic ADO.Net?

Thanks!

+9
orm code-generation
source share
5 answers

It really depends on your project requirements and your development process. ORMs are packed with a lot of whistles and bring a lot of joy to the table, but if you just buy several individual functions, you may find that the required mental / physical chest is disappointing.

First of all, you should know that there are two types of ORM: those that map an existing scheme to the application logic (you control the scheme) and those that map the application logic to the scheme (ORM controls the scheme). You are better off avoiding the first kind, since they do not make it easier for you to execute / repeat the significant DBA work for each environment, that is, you must make sure that all developers work with the appropriate scheme, in addition to that, the corresponding code is also executed. The second type can completely abstract the fact of your use of the base database, so they allow you to focus on the application domain exclusively, which makes them happy.

No DBA, no local development efforts against an unmanaged remote database instance, no more nuances between RDBMSs, no more stored procedures, more hard-coded link queries, more complex SQL migration queries.

So, ideally, your ORM should:

  • Automatic DB schema management for you
  • Provide on-site implementation of the Active Record Act
  • Indeed, you can encapsulate all business logic

Other good sugars:

  • Automatic management of data migrations (if you expect changes in ontologies to be different from changes)
  • Support for creating / importing fixtures

Keep in mind that you can run any project with ORM, like @Noon, but you cannot run every project without it at the moment. Ideally, ORMs are fantastic for projects where you want developers to be in full control or you need to run private local database instances. In any case, this is a huge leap from the approach retroactively: make a request to DBA, drink coffee until the database is updated, I hope that this happens within a week.

-one
source share

I went for web programming, with its new languages ​​that do not have proper data processing (which leads to the perceived need for ORM), at the same time I built a code generator for all codes. They never looked back.

One of the reasons why I will never consider ORMs is that I really know how databases work, thank you very much. I am not trying to make it look like I am not using a relational database, I want something to allow me to access the database with minimal work, and it will never be ORM, because that is not what it is about they are.

In my experience, a good word-based generator is the most true DRY programming (don't repeat yourself), it can free me from nit-and-pick working with the database and let me focus on what's important, getting good business logic written on top of a solid table.

EDIT : two more points:

1) Transition without an ORM route is, if nothing else, lonely, since ORM is just as fury that it is difficult to find people who have never needed it and see no reason. But let your technical solution help you.

2) A couple of years ago I wrote a blog entry, “Why I don’t use ORM”, which I will not refer to, because it was too inflammatory. After some time, I again tried to understand why you can objectively look at ORM and not see any value without inflammation, and this link: http://database-programmer.blogspot.com/2010/12/historical-perspective-of-orm -and.html

+5
source share

It depends; do you like to do useless work with the database, or would you rather have it all generated?

Seriously, although for me there are absolutely no questions. Every well-known project should start with ORM, and for me LLBLGen is the best. It is not free. But it saves so much time in developing the data layer and provides a good structure for the job.

Indeed, it is a matter of deciding how you want to spend time. If you see a value in working on a data layer, due to a number of reasons that you can justify when they are matched with something like LLBLGen, then do it. But for me this is not so.

Of course, I agree with you, constantly changing ORM, this is not ideal. Therefore, I suggest that you spend a little time (maybe a few weeks), determining which one is best for the style in which you want to develop, and how you structure your projects, and then look for it. Nowadays, most of the basic methods are very well supported, so you cannot go wrong to choose one of them and standardize it.

+4
source share

One point in favor of ORM is compile-time checking. I will use the entity framework as an example, as this is what I use as an ORM.

If you need to make changes to the database schema during development (delete a column, table, etc.), you will update your model from the database and then compile. Wherever this column or table has been indicated, it now appears as a compile-time error, which makes it easy to catch errors that are likely to be noticed only at runtime with standard ado.net.

Another thing to think about is special queries — what if you want to drop just a few columns of data from a table? The generated code you need to add a new request, a class to fill with data, etc. With ORM, you can do something like this, where an anonymous class is generated for you, so you do not need to go through the work of creating it yourself.

In essence, ORM will handle all cross cases that are not usually designed for a home solution.

var q = from c in ctx.contact where c.contactId < 4 select c.firstName, c.lastName; foreach(var temp in q){ string fullname = temp.firstName + " " + temp.LastName; } 
+1
source share

For a simple CRUD, and if the objects are table code generators, you get straight to your goal. ORMs are becoming more interesting if

  • you have a complex relationship with objects and you need to cross object links,
  • need some caching mechanisms
  • need to deal with simultaneous reading / writing,
  • you need some versioning of the table rows,
  • must deal with inheritance,
  • ...

In short: if you need more than a simple mapping between a table and an object, then ORMs can be useful. Therefore, you need to check the list of ORM functions and ask yourself if you need it.

There is an alternative between a code generator and a full-blown ORM: Data mappers (e.g. MyBatis , formerly known as iBatis).

0
source share

All Articles