代码生成器与 ORM
我编写了一个代码生成器,它为给定的 SQL Server/CE 数据库生成 POCO 和存储库。没什么花哨的,只有使用经典 ADO.Net 的简单 CRUD 程序。我的问题是,为什么我应该使用像 L2S/EF4 这样的 ORM 而不是自定义代码生成器? 微软每隔两三年就会发布一些新的数据访问框架/技术,我认识很多开发人员,他们不能总是接触最新的技术,但他们每个人都知道经典的 ADO.Net,如何修改现有代码以及如何开发新功能。 ORM 工具是否带来了当今必须的东西,或者我可以坚持使用经典的 ADO.Net?
谢谢你!
I wrote a code generator which generates POCOs and repositories for a given SQL Server/CE database. Nothing fancy, only simple CRUD procedures using classic ADO.Net. My question is, why should I use an ORM like L2S/EF4 over custom code generator?
Every 2 or 3 years Microsoft ships some new data access framework/technology and I know quite a few developers who can't always be in touch with the latest technologies, but every one of them knows the classic ADO.Net, how to modify the existing code and how to develop a new functionalities. Do ORM tools bring something which is a must nowdays, or I can stick with a classic ADO.Net?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我开始进行 Web 编程,其较新的语言缺乏适当的数据处理(这导致人们认为需要 ORM),同时我构建了我的万能代码生成器。从未回头。
我永远不会考虑 ORM 的原因之一是因为我实际上知道数据库是如何工作的,非常感谢。我不想让它看起来像我没有使用关系数据库,我想要一些东西让我以尽可能少的工作获得数据库的力量——而这永远不会是一个 ORM,因为那是不是他们的目的。
根据我的经验,一个好的基于字典的生成器是最真实的 DRY 编程(不要重复自己),它可以让我摆脱使用数据库的麻烦,让我专注于重要的事情,获得良好的业务逻辑写在可靠的表设计之上。
编辑:还有两点:
1)走非 ORM 路线,如果没有别的原因的话,孤独,因为 ORM 如此流行,很难找到合适的方法。那些从来不需要它并且看不到意义的人。但让您的技术判断来指导您。
2)几年前,我写了一篇博客文章“为什么我不使用 ORM”,我不会链接到该文章,因为它太煽动性了。一段时间后,我再次尝试理解为什么可以客观地看待 ORM 并且看不到任何价值,而不会引起煽动,该链接是: http://database-programmer.blogspot.com/2010/12/historical-perspective-of-orm-and.html
I went to web programming, with its newer languages that lack proper data handling (which leads to the perceived need for ORM) at the same time I built my be-all-end-all code generator. Have never looked back.
One reason I would never consider an ORM is because I actually know how databases work, very well thank you. I don't something to try to make it look like I'm not using a Relational database, I want something to get me the power of the database with as little work as possible -- and that will never be an ORM because that is not what they are about.
In my experience a good dictionary-based generator is the truest D-R-Y programming (Don't repeat yourself), it can free me from the nit-and-pick of working with the DB and allow me to concentrate on what matters, getting good biz logic written on top of a solid table design.
EDIT: Two more points:
1) Going a non-ORM route is, if nothing else, lonely, inasmuch as ORM is so much the rage it is hard to find the people who never needed it and don't see the point. But let your technical judgement guide you.
2) A couple of years ago I wrote a blog entry, "Why I do not use ORM", which I will not link to because it was too inflammatory. After some time I tried again to capture the sense of why it is possible to look at ORM objectively and see no value, without being inflammatory, and that link is: http://database-programmer.blogspot.com/2010/12/historical-perspective-of-orm-and.html
视情况而定;您喜欢对数据库进行无用的忙碌工作,还是更喜欢将其全部生成?
但说实话,对我来说,这是绝对没有问题的。每个已知的项目都应该从 ORM 开始,对我来说,LLBLGen 是最好的。但它不是免费的。但它节省了开发数据层的大量时间,并提供了一个很好的使用结构。
事实上,问题在于你想如何度过时间。如果您看到在数据层工作的价值,并且在与 LLBLGen 之类的东西进行权衡时,由于一系列您可以证明的原因,那么就去做吧。但对我来说,我不这样做。
当然,我同意你的观点,不断地改变 ORM 并不理想。因此,我建议您花一些时间(也许几周)来确定哪一种最适合您喜欢的开发风格以及您构建项目的方式,然后继续使用。无论如何,现在大多数主要方式都得到了很好的支持,因此您选择其中一种并将其标准化是无可厚非的。
Depends; do you like doing useless busywork with the database, or would you prefer to have it all generated?
Seriously though, for me, there is absolutely no question. Every knew project should start with an ORM, and for me, LLBLGen is the best. It's not free though. But it saves so much time in developing the data layer, and provides a nice structure to work with.
Really, it's a matter of deciding how you want to spend time. If you see value in working in the data layer, because of some series of reasons you can justify, when weighed against something like LLBLGen, then do it. But for me, I do not.
Of course, I agree with you, having to constantly change ORM's is not ideal. So I suggest you spend a bit of time (perhaps a few weeks) determining which one is the best for the style you like to develop in, and the way you structure your projects, and then go for it. Most of the main ways are very well supported these days anyway, so you couldn't be faulted to choosing one of them and standardising it.
支持 ORM 的一点是编译时检查。我将使用实体框架作为示例,因为我将其用作 ORM。
如果您需要在开发过程中更改数据库架构(删除列、表等),则可以从数据库更新模型,然后进行编译。现在,引用该列或表的任何地方都显示为编译时错误,从而可以轻松捕获可能仅在标准 ado.net 运行时才会注意到的错误。
另一件需要考虑的事情是即席查询 - 如果您只想从表中提取几列数据怎么办?使用生成的代码,您需要添加新的查询、用于填充数据的类等。使用 ORM,您可以执行类似的操作,为您生成一个匿名类,这样您就不必经历繁琐的工作自己创建一个。
本质上,ORM 将处理本土解决方案通常无法处理的所有边缘情况。
One point in favor of an ORM is compile time checking. I'll use the entity framework as an example, since that's what I use as an ORM.
If you need to make a database schema change during development (remove a column, table, etc.), you update your model from the database and then compile. Everywhere that column or table was referenced now shows up as a compile time error, making it easy to catch bugs that would likely only be noticed at runtime with standard ado.net.
Another thing to think about is ad-hoc queries - what if you want to pull back just a few columns of data from a table? With generated code, you need to add a new query, class to fill with data, etc. With an ORM, you can do something like this, where an anonymous class is generated for you so you don't have to go through the busywork of creating one yourself.
Essentially, an ORM will handle all the edge cases that a home grown solution generally doesn't.
这确实取决于您的项目要求和开发过程。 ORM 充满了很多奇才,给桌面带来了很多乐趣,但如果您只是购买一些独特的功能,您可能会发现所需的心理/物理基础令人失望。
您应该知道的第一件事是,有两种 ORM:将现有架构映射到应用程序逻辑(您管理架构)的 ORM 和将应用程序逻辑映射到架构(ORM 管理架构)的 ORM。您最好避免第一种,因为它们不会减轻您必须为每个环境执行/重复大量 DBA 工作的情况,即,您除了确保所有开发人员都运行适当的模式之外,还必须确保他们'还运行适当的代码。第二种可以完全抽象您使用底层数据库的事实,因此它们允许您仅关注应用程序领域,这让开发人员感到高兴。
无需 DBA,不再需要针对无法管理的远程数据库实例进行本地开发工作,不再需要跨 RDBMS 的细微差别,不再需要存储过程,不再需要使用硬编码引用进行查询,不再需要复杂的 SQL 迁移查询。
因此,理想情况下,您的 ORM 应该:
其他不错的好处:
请记住,您可以使用以下命令启动任何项目像 @Noon 所说的 ORM,但现在如果没有它,你就无法启动每个项目。理想情况下,ORM 非常适合您希望开发人员完全控制或需要它们运行私有本地数据库实例的项目。不管怎样,这与倒退的方法相比是一个巨大的飞跃:向 DBA 提出请求,喝咖啡直到 DB 更新,希望它在一周内发生。
It really does depend on your project requirements and your development process. ORMs are packed with a lot of whiz and bring much joy to the table, but if your just shopping for a few distinct features, you might find the required mental/physical crud to be disappointing.
First thing you should know is that there are two kinds of ORMs: those that map an existing schema to application logic (you manage the schema) and those that map application logic to a schema (the ORM manages the schema). You should preferably avoid the first kind since they do not alleviate you of having to do/repeat considerable DBA work for each environment, i.e. you'll have to make sure all the devs are running an appropriate schema, in addition to making sure they're also running appropriate code. The second kind can completely abstract the fact your using an underlying database at all, so they allow you to focus on the application domain solely, which makes devs happy.
No DBA, no more local development efforts against an unmanageable remote DB instance, no more cross-RDBMS nuances, no more stored procedures, no more queries with hard-coded references, no more complex SQL migration queries.
So, ideally your ORM should:
Other nice sugars:
Keep in mind that you can start any project with an ORM like @Noon said, but you can't start every project without it nowdays. Ideally, ORMs are a fantastic fit for projects where you want the devs to be in full control or you need them to run private, local DB instances. Either way, it's a huge leap from the ass-backwards approach: make request to DBA, drink coffee until DB is updated, hope it happens within the week.
对于简单的 CRUD 以及如果对象代表表,代码生成器可以让您直接实现目标。 ORM 变得越来越有趣,如果
简而言之:如果您只需要表和对象之间的简单映射,那么 ORM 可能会很有用。因此,您需要查看 ORM 的功能列表并询问自己是否需要它。
在代码生成器和完整的 ORM 之间还有一种替代方案:数据映射器(例如 MyBatis 以前称为 iBatis)。
For simple CRUD and if objects represent a table code generators get you straight to your goal. ORMs become increasingly interesting, if
In short: If you need more just a plain mapping between a table and an object, then ORMs can be useful. Therefore you need to check out the feature list of the ORM and ask yourself if you need it.
There is an alternative something in between a code generator and an full blown ORM: Data mappers (like MyBatis formerly known as iBatis).