Microsoft Linq to SQL, Entity Framework (EF), and nHibernate, etc are all proposing ORMS as the next generation of Data Mapping technologies, and are claiming to be lightweight, fast and easy. Like for example this article that just got published in VS magazine:
Microsoft Linq to SQL,Entity Framework(EF)和nHibernate等都提议将ORMS作为下一代数据映射技术,并声称它们轻量级,快速且简单。例如刚刚在VS杂志上发表的这篇文章:
http://visualstudiomagazine.com/features/article.aspx?editorialsid=2583
Who all are excited about implementing these technologies in their projects? Where is the innovation in these technologies that makes them so great over their predecessors?
谁都对在项目中实施这些技术感到兴奋?这些技术的创新在哪里使它们比以前的产品更加优秀?
19 个解决方案
#1
17
I have written data access layers, persistence components, and even my own ORMs in hundreds of applications over the years (one of my "hobbies"); I have even implemented my own business transaction manager (discussed elsewhere on SO).
多年来,我已经在数百个应用程序中编写了数据访问层,持久性组件甚至我自己的ORM(我的一个“爱好”);我甚至已经实现了自己的业务事务管理器(在其他地方讨论了SO)。
ORM tools have been around for a long time on other platforms, such as Java, Python, etc. It appears that there is a new fad now that Microsoft-centric teams have discovered them. Overall, I think that is a good thing--a necessary step in the journey to explore and comprehend the concepts of architecture and design that seems to have been introduced along with the arrival of .NET.
ORM工具已经在其他平台上存在了很长时间,例如Java,Python等。现在看来,以微软为中心的团队已经发现了一种新的时尚。总的来说,我认为这是一件好事 - 这是探索和理解建筑和设计概念的必要步骤,这些概念似乎随着.NET的到来而被引入。
Bottom line: I would always prefer to do my own data access rather than fight some tool that is trying to "help" me. It is never acceptable to give up my control over my destiny, and data access is a critical part of my application's destiny. Some simple principles make data access very manageable.
一句话:我总是喜欢自己做数据访问,而不是打击一些试图“帮助”我的工具。放弃对命运的控制是绝对不可接受的,数据访问是我应用程序命运的关键部分。一些简单的原则使数据访问非常易于管理。
Use the basic concepts of modularity, abstraction, and encapsulation--so wrap your platform's basic data access API (e.g., ADO.NET) with your own layer that raises the abstraction level closer to your problem space. DO NOT code all your data access DIRECTLY against that API (also discussed elsewhere on SO).
使用模块化,抽象和封装的基本概念 - 将您的平台的基本数据访问API(例如,ADO.NET)与您自己的层一起包装,从而将抽象级别提升到更接近您的问题空间。不要直接针对该API编码所有数据访问(也在SO的其他地方讨论)。
Severely apply the DRY (Don't Repeat Yourself) principle = refactor the daylights out of your data access code. Use code generation when appropriate as a means of refactoring, but seek to eliminate the need for code generation whenever you can. Generally, code generation reveals that something is missing from your environment--language deficiency, designed-in tool limitation, etc.
严格应用DRY(不要重复自己)原则=重构数据访问代码中的日光。在适当的时候使用代码生成作为重构的方法,但是尽可能地消除对代码生成的需要。通常,代码生成表明您的环境中缺少某些东西 - 语言不足,设计工具限制等。
Meanwhile, learn to use the available API well, particularly regarding performance and robustness, then incorporate those lessons into your own abstracted data access layer. For example, learn to make proper use of parameters in your SQL rather than embedding literal values into SQL strings.
同时,学习如何使用可用的API,特别是在性能和健壮性方面,然后将这些课程纳入您自己的抽象数据访问层。例如,学习在SQL中正确使用参数,而不是将文字值嵌入到SQL字符串中。
Finally, keep in mind that any application/system that becomes successful will grow to encounter performance problems. Fixing performance problems relies more on designing them out rather than just "tweaking" something in the implementation. That design work will affect the database and the application, which must change in sync. Therefore, seek to be able to make such changes easily (agile) rather than attempt to avoid ever changing the application itself. In part, that eventually means being able to deploy changes without downtime. It is not hard to do, if you don't "design" away from it.
最后,请记住,任何成功的应用程序/系统都会遇到性能问题。修复性能问题更多地依赖于设计它们而不仅仅是“调整”实现中的某些东西。该设计工作将影响数据库和应用程序,它们必须同步更改。因此,寻求能够轻松(敏捷)进行此类更改,而不是试图避免不断更改应用程序本身。在某种程度上,这最终意味着能够在不停机的情况下部署变更。如果你不“设计”它,那就不难做到。
#2
12
I'm a huge ORM advocate. Code generation with ORM saves my shop about 20-30% on most of our projects.
我是一个巨大的ORM倡导者。使用ORM生成代码可以使我的商店在大多数项目中节省大约20-30%。
And we do contract development, so this is a big win.
我们合同开发,所以这是一个巨大的胜利。
Chris Lively made an interesting point about having to do a redeploy any time a query gets touched. This may be a problem for some people, but it does not touch us at all. We have rules against making production database changes directly.
Chris Lively提出了一个有趣的观点,即每当查询被触及时都必须进行重新部署。对某些人来说这可能是一个问题,但它根本不会触及我们。我们有针对直接进行生产数据库更改的规则。
Also, you can still rely on traditional sprocs and even views when appropriate... We are not dogmatically 100% ORM, that's for sure.
此外,你仍然可以依赖传统的sprocs甚至适当的视图...我们不是教条100%ORM,这是肯定的。
#3
6
I have been on the ORM train for the longest time, since the free version of LLBLGen to the latest and greatest commercial product LLBLGen Pro. I think ORMs fit in very well for a lot of the common data input output systems.
自从LLBLGen的免费版本发布到最新最好的商业产品LLBLGen Pro以来,我一直在ORM火车上工作了很长时间。我认为ORM非常适合许多常见的数据输入输出系统。
That isn't to say they solve all problems however. It is a tool which can be used where it makes sense to be used. If your database schema is relativly close to how your business objects need to be, ORMs are the best.
这并不是说他们解决了所有问题。它是一种可以在有意义的地方使用的工具。如果您的数据库模式与业务对象的关系相对接近,那么ORM是最好的。
#4
6
It's not a bandwagon to jump on, is a reaction to a real problem! Object Relational Mapping (ORM) has been around for a long time and it solves a real problem.
跳起来不是一个潮流,是对真正问题的反应!对象关系映射(ORM)已存在很长时间,它解决了一个真正的问题。
Original Object Oriented(OO) languages were all about simulating real world problems using a computer language. It could be argued that if you are really using an OO language to build systems you will be simulating the real world problem domain using a Domain Driven Design (DDD). This logically takes you to a separation of concerns model in order to keep your DDD clean and clear from all the clutter of data persistence and application controls.
原始面向对象(OO)语言都是关于使用计算机语言模拟现实世界的问题。可以说,如果您真的使用OO语言来构建系统,那么您将使用域驱动设计(DDD)模拟真实世界的问题域。这在逻辑上将您带到一个关注点分离模型,以保持您的DDD清洁和清除数据持久性和应用程序控件的所有混乱。
When you build systems following a DDD pattern and use a Relational database for persistence then you really need a good ORM or you will be spending too much time building and maintaining database crud (pun intended).
当您按照DDD模式构建系统并使用Relational数据库进行持久化时,您确实需要一个好的ORM,否则您将花费太多时间来构建和维护数据库crud(双关语)。
ORM is an old problem and was solved years ago by products like Object Lens and Top Link. Object Lens was a Smalltalk ORM built by ParkPlace in the 90's. Top Link was built by Object People for Smalltalk, then converted for Java, and is currently used by Oracle. Top Link has also been around since the 90's. DDD advocates are now beginning to clearly articulate the case for DDD and gaining mind share. Therefore ORM, by necessity, is becoming mainstream and Microsoft is just reacting as usual.
ORM是一个老问题,几年前被Object Lens和Top Link等产品解决了。 Object Lens是由ParkPlace在90年代建造的Smalltalk ORM。 Top Link由Object People for Smalltalk构建,然后转换为Java,目前由Oracle使用。 Top Link自90年代以来也一直存在。 DDD倡导者现在开始清楚地阐明DDD的案例并获得思想共享。因此,ORM必然会成为主流,微软只是像往常一样做出反应。
#5
5
No. Not everyone is.
不,不是每个人都是。
Here's the number one big ass elephant in the room with most of the ORM tools (especially LINQ to SQL:
这里是大多数ORM工具(尤其是LINQ to SQL)的房间里头号大屁股大象:
You are guaranteed that ANY data related change will require a full redeployment of your application.
您可以保证,任何与数据相关的更改都需要完全重新部署您的应用程序。
For example, my day job can currently fix most query problems by modifying an existing stored procedure and redeploying just that one piece. With Linq, et al, your data queries are moved into your actual code. Guess what that means?
例如,我的日常工作当前可以通过修改现有存储过程并重新部署该一个部分来解决大多数查询问题。使用Linq等人,您的数据查询将移至实际代码中。猜猜那意味着什么?
#6
4
ORM is a good match for people who get along ok with software that writes software for them; but if you are obsessive about controlling what's happening and why, ORM can be suboptimal particularly with database optimization. Any abstraction layer has costs and benefits; ORM has both, but the balance isn't right yet IMHO. And ORM, in its current form, ironically adds an abstraction layer that still puts classes and unabstacted database schemas to intimately together.
ORM非常适合与那些为他们编写软件的软件相处的人;但是如果你对控制正在发生的事情以及为什么会这么着迷,那么ORM可能不是最理想的,特别是在数据库优化方面。任何抽象层都有成本和收益; ORM既有,但余额还不对恕我直言。而ORM,以其当前的形式,具有讽刺意味的是添加了一个抽象层,仍然将类和未经反应的数据库模式紧密地放在一起。
My experience is that it can help you get a proof-of-concept version together quickly, but can introduce refactoring requirements you may not be familiar with (at least yet.)
我的经验是,它可以帮助您快速获得概念验证版本,但可以引入您可能不熟悉的重构要求(至少尚未。)
Add to that, that the tool is evolving, and best-practices and patterns are not well established, nor a concensus of the kind that lets other programmers (or future programmers) feel comfortable with your code. So I expect to see higher-than-usual refactoring requirements for a while.
除此之外,该工具正在不断发展,最佳实践和模式尚未建立,也没有让其他程序员(或未来程序员)对您的代码感到满意的一致意见。因此,我希望看到一段时间内的重构要求高于平常。
I'll reserve judgment (optimistically) about where it will settle in terms of being mainstream. I wouldn't bet a current project on it at this point. My patterns for dealing with the impedance mismatch are satisfactory for my purposes.
我会保持(乐观地)判断它在主流方面的定位。此时我不会打赌当前的项目。我处理阻抗不匹配的模式对我来说是令人满意的。
#7
2
You have to fight with the ORM system once you want to do anything beyond the simplest select, update or delete. And your performance goes into the toilet once you begin doing real stuff.
一旦你想做除最简单的选择,更新或删除之外的任何事情,你必须与ORM系统作斗争。一旦你开始做真实的东西,你的表现就会进入厕所。
So no.
#8
2
I look forward to the day my team starts looking into ORM solutions. Until that day, we are a pure DataSet/Stored Procedure shop and let me tell you that it isn't all biscuits and gravy being "pure".
我期待着我的团队开始研究ORM解决方案的那一天。直到那天,我们才是一个纯粹的DataSet / Stored Procedure商店,让我告诉你,并非所有的饼干和肉汁都是“纯净的”。
Firstly, the new Linq to SQL is performing close to that of stored procs and data readers. We use datasets everywhere, so performance would improve. So far so good for ORM.
首先,新的Linq to SQL的性能接近于存储过程和数据读取器的性能。我们在任何地方使用数据集,因此性能会提高。到目前为止ORM非常好。
Secondly, stored procs have the added benefit of being released separate of code, but they also have the detriment of being released separate of code. Pretend for a second that you have a database with more than 5 systems connecting to it and more than 10 people working on it. Now think about managing all those stored procedure changes and dependencies, especially when there is a common code base. It is a nightmare...
其次,存储过程具有单独发布代码的额外好处,但它们也不利于单独发布代码。假装有一个数据库,其中有超过5个系统连接到它,超过10个人正在使用它。现在考虑管理所有这些存储过程更改和依赖项,尤其是在存在公共代码库时。这是一场噩梦......
Third, it is difficult to debug stored procs. They often result in erroneous behavior for any number of reasons. That is not to say the same could no result from the dynamic sql being generated by the ORM, but one less problem is one less problem. No permissions issues (though mostly resolved in SQL 2005), no more multi step development. Writing the query, testing the query, deploying the query, tying it into the code, testing the code, deploying the code. The query is part of the code and I see this as a good thing.
第三,调试存储过程很困难。由于各种原因,它们经常导致错误的行为。这并不是说ORM生成的动态sql不会导致同样的问题,但少一个问题就是问题少一个。没有权限问题(尽管大多数在SQL 2005中解决),没有更多的多步骤开发。编写查询,测试查询,部署查询,将其绑定到代码中,测试代码,部署代码。查询是代码的一部分,我认为这是一件好事。
Fourth, you can still used stored procedures. Running some reports that take a long time? Stored procs are a better solution. Why? Query execution plans can be optimized by the database engine. I won't pretend to understand all the workings of the database, but I do know there are some limitations to optimizing dynamic sql currently and that is a trade off we make when going with an ORM. However, stored procs are not ruled out when using an ORM solution.
第四,您仍然可以使用存储过程。运行一些需要很长时间的报告?存储过程是更好的解决方案。为什么?查询执行计划可以由数据库引擎优化。我不会假装理解数据库的所有工作方式,但我知道当前优化动态sql存在一些限制,这是我们在使用ORM时所做的权衡。但是,使用ORM解决方案时不会排除存储过程。
Really the biggest reason I see people avoiding an ORM is that they simply don't have experience with one. There will be an obvious learning curve and ignorance stage. However, if it is going to improve development performance and hardly hinder (or in my case improve) performance. It is a trade off worth making.
真的,我认为人们避免使用ORM的最大原因是他们根本没有经验。将有一个明显的学习曲线和无知阶段。但是,如果要提高开发性能并且几乎不会阻碍(或者在我的情况下改进)性能。这是一个值得做出的折衷。
#9
1
I'm a big fan as well, using EF and Linq-to-SQL. My reasons are:
我也是使用EF和Linq-to-SQL的忠实粉丝。我的理由是:
Since LINQ is compiled and type safe, you don't get the problems of typos in "string-based" SQL. I don't know how many hours I've spent of my life tracking down an error in an SP or other SQL where a "tick" or some other keyword was in the wrong place.
由于LINQ是编译的并且类型安全,因此您不会在“基于字符串的”SQL中遇到拼写错误的问题。我不知道我花了多少时间追踪SP或其他SQL中的错误,其中“tick”或其他关键字位于错误的位置。
The above and other factors make development faster.
上述因素和其他因素使发展更快。
Though there certainly is overhead compared to "closer to the metal" methods of database querying, none of us would be using .NET at all, or even C++ if performance was our #1 concern. For most apps, I've been able to get excellent performance from Linq-To-SQL, even without using the stored proc approach (the client-based compiled queries is my usual approach).
虽然与“更接近金属”的数据库查询方法相比,肯定存在开销,但如果性能成为我们的首要考虑因素,我们根本就不会使用.NET,甚至不会使用C ++。对于大多数应用程序,我已经能够从Linq-To-SQL获得出色的性能,即使不使用存储过程方法(基于客户端的编译查询是我通常的方法)。
Certainly for some applications you still want to do things the old fashioned way though.
当然,对于某些应用程序,您仍然希望以旧式方式执行操作。
#10
1
I guess what I meant was, what is the innovation that ORMs provide over building your DAL using traditional ADO.NET, SQL and mapping them to objects in code?
我想我的意思是,ORM使用传统的ADO.NET,SQL并将它们映射到代码中的对象来构建DAL的创新是什么?
Here are the three major peices of my DAL and I am comparing with ORMs to see the benefits:
以下是我的DAL的三个主要方面,我正在与ORM进行比较以了解其中的好处:
-
You still have to have a query in an ORM = SQL (SQL is more powerful by far)
您仍然需要在ORM = SQL中进行查询(到目前为止SQL更强大)
-
Mapping code moves to configuration but still not eliminated, just shifts from one paradigm to another
映射代码移动到配置但仍未消除,只是从一个范例转移到另一个范例
-
Objects have to be defined and managed tightly relatedto your Data Schema unlike in the traditional approach which I can keep them decoupled.
对象必须与您的数据模式紧密相关地定义和管理,这与传统的方法不同,我可以将它们分离。
Am I missing something?
我错过了什么吗?
#11
0
I have been following Fluent-NHibernate very closely as it has some of the most potential I've ever seen in a project.
我一直非常密切关注Fluent-NHibernate,因为它有一些我在项目中见过的最有潜力。
#12
0
I am a big ORM guy, get the logic out of the database, use the database only for speed.
我是一个很大的ORM人,从数据库中获取逻辑,仅使用数据库来提高速度。
I love the speed you can develop an application. The biggest advantage, depending on the ORM, is you can change the back end without having to translate your business logic.
我喜欢你开发应用程序的速度。取决于ORM,最大的优势是您可以在不必翻译业务逻辑的情况下更改后端。
I switched to LINQ and have never looked back. DBLinq is awesome for doing other database than MSSQL. I have used it with MY SQL and it is GREAT.
我切换到LINQ并且从未回头。 DBLinq对于使用除MSSQL之外的其他数据库非常棒。我用它和MY SQL一起使用它很棒。
#13
0
not yet, still skeptical; like most microsoft products, i wait for SP2 or a year and a half before trusting them in a producton environment
还没有,仍然持怀疑态度;像大多数微软产品一样,我等待SP2或一年半之后再相信它们在产品环境中
and note that pretty much every new thing introduced by anyone, not just microsoft, is hailed as "lightweight, fast and easy" - take it with a block of salt. They do not advertise the problems/issues quite as loudly as the benefits/features! That's why we wait for early adopters to discover them.
并注意到几乎每个人都推出的新东西,不仅仅是微软,都被称为“轻巧,快速和简单” - 带上一块盐。他们不会像利益/功能那样大声宣传问题/问题!这就是我们等待早期采用者发现它们的原因。
This is not to disparage ORM or LINQ or anything like that; I'm reserving judgement until
这不是为了贬低ORM或LINQ或类似的东西;直到,我才会保留判断力
- I have time to evaluate them,
- some need arises that only they can satisfy,
- the technology appears stable and well-supported enough to risk in one of my clients' production environments, and/or
- a client requests it
我有时间评估它们,
有些需要只有他们才能满足,
该技术看起来稳定且受到良好支持,足以在我的客户的生产环境中冒险,和/或
客户要求它
Please note: I've done ORM before, manually, and it worked just fine, so I have high hopes for the newer ORM systems.
请注意:我之前已经手动完成了ORM,它运行得很好,所以我对新的ORM系统寄予厚望。
#14
0
If codesmith generates code based on your tables, aren't you still tightly coupled to your data schema? I would prefer a solution that decouples my objects from my database schema for mor flexibility in the architecture
如果codesmith根据您的表生成代码,您是否仍然与数据模式紧密耦合?我更喜欢一种解决方案,它将我的对象与我的数据库模式分离,以便在架构中实现灵活性
That's from one of your comments - It's true, CodeSmith tightly couples you to your tables.
这是你的一条评论 - 这是真的,CodeSmith将你与你的桌子紧密地联系在一起。
NHibernate on the other hand has allot of features that can help with that : you have Components so that in code you can have : Person with a property Address ,where Address is a separate class .
另一方面,NHibernate有许多功能可以帮助你:你有组件,所以在代码中你可以拥有:具有属性地址的人,其中地址是一个单独的类。
You also inheritance mapping. So it does a fair job of decoupling your schema from your domain.
你还继承映射。因此,它可以很好地将您的架构与您的域解耦。
#15
0
We still use a hand rolled, repetitive cut'n'paste DAL where I work. It's extremely painful, complex, and error prone, but it's something all developers can understand. Although it is working for us at the moment, I don't recommend it as it begins to break down quickly on large projects. If someone doesn't want to go to full blown ORM, I'd at least advocate some sort of DAL generation.
在我工作的地方,我们仍然使用手工卷制,重复切割'D'Paste DAL。它非常痛苦,复杂且容易出错,但它是所有开发人员都能理解的。虽然它目前对我们有用,但我不推荐它,因为它开始在大型项目上迅速崩溃。如果有人不想进行全面的ORM,我至少会提倡某种DAL生成。
#16
0
I'm actually working on writing an ORM tool in .NET as a side project to keep me entertained.
我正在努力在.NET中编写一个ORM工具作为一个让我受理的副项目。
SQL is dead to me. I hate writing it, especially having it in my code anywhere. Manually writing select/insert/update/delete queries for each object is a waste of time IMO. And don't even get me started on handling NULLs ("where col_1 = ?" vs "where col_1 is null") when dynamically generating queries. The ORM tools can handle that for me.
SQL对我来说是死的。我讨厌写它,特别是在我的代码中随处可见。手动为每个对象编写select / insert / update / delete查询是浪费时间IMO。在动态生成查询时,甚至不让我开始处理NULL(“where col_1 =?”vs“col_1 is null”)。 ORM工具可以为我处理。
Also, having 1 place that SQL may be dynamically generated would hopefully go a long was to eliminating SQL injection attacks.
此外,有一个地方可以动态生成SQL可能需要很长时间才能消除SQL注入攻击。
On the other hand, I've used Hibernate, and I absolutely hate it. In a real-word project, we ran into limitations, unimplemented bits, and bugs every few weeks.
另一方面,我使用过Hibernate,我绝对讨厌它。在一个真实的单词项目中,我们每隔几周就遇到限制,未实现的位和错误。
Keeping the query logic DB side (usually as a view or stored procedure) also has the benefit of being available for DBAs to tune. That was a constant battle at my last job, using Hibernate. DBAs: "give us all the possible queries so we can tune" Devs: "uh, I don't know because Hibernate will generate them on the fly. I can give you some HQL and an XML mapping though!" DBAs: "Don't make me punch you in the face!"
保持查询逻辑数据库端(通常作为视图或存储过程)也具有可供DBA调整的优点。使用Hibernate,这是我上一份工作的一场持续战斗。 DBA:“给我们所有可能的查询,以便我们可以调整”Devs:“呃,我不知道,因为Hibernate会动态生成它们。我可以给你一些HQL和XML映射!” DBA:“不要让我打你的脸!”
#17
0
I dislike the code generation used in most ORMs. In fact, code generation in general I find to be a weak tool that is usually indicative of using the wrong language in the first place.
我不喜欢大多数ORM中使用的代码生成。实际上,一般来说,代码生成我发现它是一个弱工具,通常表示首先使用错误的语言。
In particular with .Net reflection, I don't see any need for code gen for ORM purposes.
特别是.Net反射,我认为不需要代码生成用于ORM目的。
#19
0
No, I dumped ORMs and switched to an smalltalk and an OODB: Gemstone. Better code, less code, faster development.
不,我倾倒了ORM并转而使用了一个小屁股和一个OODB:Gemstone。更好的代码,更少的代码,更快的开发。
#1
17
I have written data access layers, persistence components, and even my own ORMs in hundreds of applications over the years (one of my "hobbies"); I have even implemented my own business transaction manager (discussed elsewhere on SO).
多年来,我已经在数百个应用程序中编写了数据访问层,持久性组件甚至我自己的ORM(我的一个“爱好”);我甚至已经实现了自己的业务事务管理器(在其他地方讨论了SO)。
ORM tools have been around for a long time on other platforms, such as Java, Python, etc. It appears that there is a new fad now that Microsoft-centric teams have discovered them. Overall, I think that is a good thing--a necessary step in the journey to explore and comprehend the concepts of architecture and design that seems to have been introduced along with the arrival of .NET.
ORM工具已经在其他平台上存在了很长时间,例如Java,Python等。现在看来,以微软为中心的团队已经发现了一种新的时尚。总的来说,我认为这是一件好事 - 这是探索和理解建筑和设计概念的必要步骤,这些概念似乎随着.NET的到来而被引入。
Bottom line: I would always prefer to do my own data access rather than fight some tool that is trying to "help" me. It is never acceptable to give up my control over my destiny, and data access is a critical part of my application's destiny. Some simple principles make data access very manageable.
一句话:我总是喜欢自己做数据访问,而不是打击一些试图“帮助”我的工具。放弃对命运的控制是绝对不可接受的,数据访问是我应用程序命运的关键部分。一些简单的原则使数据访问非常易于管理。
Use the basic concepts of modularity, abstraction, and encapsulation--so wrap your platform's basic data access API (e.g., ADO.NET) with your own layer that raises the abstraction level closer to your problem space. DO NOT code all your data access DIRECTLY against that API (also discussed elsewhere on SO).
使用模块化,抽象和封装的基本概念 - 将您的平台的基本数据访问API(例如,ADO.NET)与您自己的层一起包装,从而将抽象级别提升到更接近您的问题空间。不要直接针对该API编码所有数据访问(也在SO的其他地方讨论)。
Severely apply the DRY (Don't Repeat Yourself) principle = refactor the daylights out of your data access code. Use code generation when appropriate as a means of refactoring, but seek to eliminate the need for code generation whenever you can. Generally, code generation reveals that something is missing from your environment--language deficiency, designed-in tool limitation, etc.
严格应用DRY(不要重复自己)原则=重构数据访问代码中的日光。在适当的时候使用代码生成作为重构的方法,但是尽可能地消除对代码生成的需要。通常,代码生成表明您的环境中缺少某些东西 - 语言不足,设计工具限制等。
Meanwhile, learn to use the available API well, particularly regarding performance and robustness, then incorporate those lessons into your own abstracted data access layer. For example, learn to make proper use of parameters in your SQL rather than embedding literal values into SQL strings.
同时,学习如何使用可用的API,特别是在性能和健壮性方面,然后将这些课程纳入您自己的抽象数据访问层。例如,学习在SQL中正确使用参数,而不是将文字值嵌入到SQL字符串中。
Finally, keep in mind that any application/system that becomes successful will grow to encounter performance problems. Fixing performance problems relies more on designing them out rather than just "tweaking" something in the implementation. That design work will affect the database and the application, which must change in sync. Therefore, seek to be able to make such changes easily (agile) rather than attempt to avoid ever changing the application itself. In part, that eventually means being able to deploy changes without downtime. It is not hard to do, if you don't "design" away from it.
最后,请记住,任何成功的应用程序/系统都会遇到性能问题。修复性能问题更多地依赖于设计它们而不仅仅是“调整”实现中的某些东西。该设计工作将影响数据库和应用程序,它们必须同步更改。因此,寻求能够轻松(敏捷)进行此类更改,而不是试图避免不断更改应用程序本身。在某种程度上,这最终意味着能够在不停机的情况下部署变更。如果你不“设计”它,那就不难做到。
#2
12
I'm a huge ORM advocate. Code generation with ORM saves my shop about 20-30% on most of our projects.
我是一个巨大的ORM倡导者。使用ORM生成代码可以使我的商店在大多数项目中节省大约20-30%。
And we do contract development, so this is a big win.
我们合同开发,所以这是一个巨大的胜利。
Chris Lively made an interesting point about having to do a redeploy any time a query gets touched. This may be a problem for some people, but it does not touch us at all. We have rules against making production database changes directly.
Chris Lively提出了一个有趣的观点,即每当查询被触及时都必须进行重新部署。对某些人来说这可能是一个问题,但它根本不会触及我们。我们有针对直接进行生产数据库更改的规则。
Also, you can still rely on traditional sprocs and even views when appropriate... We are not dogmatically 100% ORM, that's for sure.
此外,你仍然可以依赖传统的sprocs甚至适当的视图...我们不是教条100%ORM,这是肯定的。
#3
6
I have been on the ORM train for the longest time, since the free version of LLBLGen to the latest and greatest commercial product LLBLGen Pro. I think ORMs fit in very well for a lot of the common data input output systems.
自从LLBLGen的免费版本发布到最新最好的商业产品LLBLGen Pro以来,我一直在ORM火车上工作了很长时间。我认为ORM非常适合许多常见的数据输入输出系统。
That isn't to say they solve all problems however. It is a tool which can be used where it makes sense to be used. If your database schema is relativly close to how your business objects need to be, ORMs are the best.
这并不是说他们解决了所有问题。它是一种可以在有意义的地方使用的工具。如果您的数据库模式与业务对象的关系相对接近,那么ORM是最好的。
#4
6
It's not a bandwagon to jump on, is a reaction to a real problem! Object Relational Mapping (ORM) has been around for a long time and it solves a real problem.
跳起来不是一个潮流,是对真正问题的反应!对象关系映射(ORM)已存在很长时间,它解决了一个真正的问题。
Original Object Oriented(OO) languages were all about simulating real world problems using a computer language. It could be argued that if you are really using an OO language to build systems you will be simulating the real world problem domain using a Domain Driven Design (DDD). This logically takes you to a separation of concerns model in order to keep your DDD clean and clear from all the clutter of data persistence and application controls.
原始面向对象(OO)语言都是关于使用计算机语言模拟现实世界的问题。可以说,如果您真的使用OO语言来构建系统,那么您将使用域驱动设计(DDD)模拟真实世界的问题域。这在逻辑上将您带到一个关注点分离模型,以保持您的DDD清洁和清除数据持久性和应用程序控件的所有混乱。
When you build systems following a DDD pattern and use a Relational database for persistence then you really need a good ORM or you will be spending too much time building and maintaining database crud (pun intended).
当您按照DDD模式构建系统并使用Relational数据库进行持久化时,您确实需要一个好的ORM,否则您将花费太多时间来构建和维护数据库crud(双关语)。
ORM is an old problem and was solved years ago by products like Object Lens and Top Link. Object Lens was a Smalltalk ORM built by ParkPlace in the 90's. Top Link was built by Object People for Smalltalk, then converted for Java, and is currently used by Oracle. Top Link has also been around since the 90's. DDD advocates are now beginning to clearly articulate the case for DDD and gaining mind share. Therefore ORM, by necessity, is becoming mainstream and Microsoft is just reacting as usual.
ORM是一个老问题,几年前被Object Lens和Top Link等产品解决了。 Object Lens是由ParkPlace在90年代建造的Smalltalk ORM。 Top Link由Object People for Smalltalk构建,然后转换为Java,目前由Oracle使用。 Top Link自90年代以来也一直存在。 DDD倡导者现在开始清楚地阐明DDD的案例并获得思想共享。因此,ORM必然会成为主流,微软只是像往常一样做出反应。
#5
5
No. Not everyone is.
不,不是每个人都是。
Here's the number one big ass elephant in the room with most of the ORM tools (especially LINQ to SQL:
这里是大多数ORM工具(尤其是LINQ to SQL)的房间里头号大屁股大象:
You are guaranteed that ANY data related change will require a full redeployment of your application.
您可以保证,任何与数据相关的更改都需要完全重新部署您的应用程序。
For example, my day job can currently fix most query problems by modifying an existing stored procedure and redeploying just that one piece. With Linq, et al, your data queries are moved into your actual code. Guess what that means?
例如,我的日常工作当前可以通过修改现有存储过程并重新部署该一个部分来解决大多数查询问题。使用Linq等人,您的数据查询将移至实际代码中。猜猜那意味着什么?
#6
4
ORM is a good match for people who get along ok with software that writes software for them; but if you are obsessive about controlling what's happening and why, ORM can be suboptimal particularly with database optimization. Any abstraction layer has costs and benefits; ORM has both, but the balance isn't right yet IMHO. And ORM, in its current form, ironically adds an abstraction layer that still puts classes and unabstacted database schemas to intimately together.
ORM非常适合与那些为他们编写软件的软件相处的人;但是如果你对控制正在发生的事情以及为什么会这么着迷,那么ORM可能不是最理想的,特别是在数据库优化方面。任何抽象层都有成本和收益; ORM既有,但余额还不对恕我直言。而ORM,以其当前的形式,具有讽刺意味的是添加了一个抽象层,仍然将类和未经反应的数据库模式紧密地放在一起。
My experience is that it can help you get a proof-of-concept version together quickly, but can introduce refactoring requirements you may not be familiar with (at least yet.)
我的经验是,它可以帮助您快速获得概念验证版本,但可以引入您可能不熟悉的重构要求(至少尚未。)
Add to that, that the tool is evolving, and best-practices and patterns are not well established, nor a concensus of the kind that lets other programmers (or future programmers) feel comfortable with your code. So I expect to see higher-than-usual refactoring requirements for a while.
除此之外,该工具正在不断发展,最佳实践和模式尚未建立,也没有让其他程序员(或未来程序员)对您的代码感到满意的一致意见。因此,我希望看到一段时间内的重构要求高于平常。
I'll reserve judgment (optimistically) about where it will settle in terms of being mainstream. I wouldn't bet a current project on it at this point. My patterns for dealing with the impedance mismatch are satisfactory for my purposes.
我会保持(乐观地)判断它在主流方面的定位。此时我不会打赌当前的项目。我处理阻抗不匹配的模式对我来说是令人满意的。
#7
2
You have to fight with the ORM system once you want to do anything beyond the simplest select, update or delete. And your performance goes into the toilet once you begin doing real stuff.
一旦你想做除最简单的选择,更新或删除之外的任何事情,你必须与ORM系统作斗争。一旦你开始做真实的东西,你的表现就会进入厕所。
So no.
#8
2
I look forward to the day my team starts looking into ORM solutions. Until that day, we are a pure DataSet/Stored Procedure shop and let me tell you that it isn't all biscuits and gravy being "pure".
我期待着我的团队开始研究ORM解决方案的那一天。直到那天,我们才是一个纯粹的DataSet / Stored Procedure商店,让我告诉你,并非所有的饼干和肉汁都是“纯净的”。
Firstly, the new Linq to SQL is performing close to that of stored procs and data readers. We use datasets everywhere, so performance would improve. So far so good for ORM.
首先,新的Linq to SQL的性能接近于存储过程和数据读取器的性能。我们在任何地方使用数据集,因此性能会提高。到目前为止ORM非常好。
Secondly, stored procs have the added benefit of being released separate of code, but they also have the detriment of being released separate of code. Pretend for a second that you have a database with more than 5 systems connecting to it and more than 10 people working on it. Now think about managing all those stored procedure changes and dependencies, especially when there is a common code base. It is a nightmare...
其次,存储过程具有单独发布代码的额外好处,但它们也不利于单独发布代码。假装有一个数据库,其中有超过5个系统连接到它,超过10个人正在使用它。现在考虑管理所有这些存储过程更改和依赖项,尤其是在存在公共代码库时。这是一场噩梦......
Third, it is difficult to debug stored procs. They often result in erroneous behavior for any number of reasons. That is not to say the same could no result from the dynamic sql being generated by the ORM, but one less problem is one less problem. No permissions issues (though mostly resolved in SQL 2005), no more multi step development. Writing the query, testing the query, deploying the query, tying it into the code, testing the code, deploying the code. The query is part of the code and I see this as a good thing.
第三,调试存储过程很困难。由于各种原因,它们经常导致错误的行为。这并不是说ORM生成的动态sql不会导致同样的问题,但少一个问题就是问题少一个。没有权限问题(尽管大多数在SQL 2005中解决),没有更多的多步骤开发。编写查询,测试查询,部署查询,将其绑定到代码中,测试代码,部署代码。查询是代码的一部分,我认为这是一件好事。
Fourth, you can still used stored procedures. Running some reports that take a long time? Stored procs are a better solution. Why? Query execution plans can be optimized by the database engine. I won't pretend to understand all the workings of the database, but I do know there are some limitations to optimizing dynamic sql currently and that is a trade off we make when going with an ORM. However, stored procs are not ruled out when using an ORM solution.
第四,您仍然可以使用存储过程。运行一些需要很长时间的报告?存储过程是更好的解决方案。为什么?查询执行计划可以由数据库引擎优化。我不会假装理解数据库的所有工作方式,但我知道当前优化动态sql存在一些限制,这是我们在使用ORM时所做的权衡。但是,使用ORM解决方案时不会排除存储过程。
Really the biggest reason I see people avoiding an ORM is that they simply don't have experience with one. There will be an obvious learning curve and ignorance stage. However, if it is going to improve development performance and hardly hinder (or in my case improve) performance. It is a trade off worth making.
真的,我认为人们避免使用ORM的最大原因是他们根本没有经验。将有一个明显的学习曲线和无知阶段。但是,如果要提高开发性能并且几乎不会阻碍(或者在我的情况下改进)性能。这是一个值得做出的折衷。
#9
1
I'm a big fan as well, using EF and Linq-to-SQL. My reasons are:
我也是使用EF和Linq-to-SQL的忠实粉丝。我的理由是:
Since LINQ is compiled and type safe, you don't get the problems of typos in "string-based" SQL. I don't know how many hours I've spent of my life tracking down an error in an SP or other SQL where a "tick" or some other keyword was in the wrong place.
由于LINQ是编译的并且类型安全,因此您不会在“基于字符串的”SQL中遇到拼写错误的问题。我不知道我花了多少时间追踪SP或其他SQL中的错误,其中“tick”或其他关键字位于错误的位置。
The above and other factors make development faster.
上述因素和其他因素使发展更快。
Though there certainly is overhead compared to "closer to the metal" methods of database querying, none of us would be using .NET at all, or even C++ if performance was our #1 concern. For most apps, I've been able to get excellent performance from Linq-To-SQL, even without using the stored proc approach (the client-based compiled queries is my usual approach).
虽然与“更接近金属”的数据库查询方法相比,肯定存在开销,但如果性能成为我们的首要考虑因素,我们根本就不会使用.NET,甚至不会使用C ++。对于大多数应用程序,我已经能够从Linq-To-SQL获得出色的性能,即使不使用存储过程方法(基于客户端的编译查询是我通常的方法)。
Certainly for some applications you still want to do things the old fashioned way though.
当然,对于某些应用程序,您仍然希望以旧式方式执行操作。
#10
1
I guess what I meant was, what is the innovation that ORMs provide over building your DAL using traditional ADO.NET, SQL and mapping them to objects in code?
我想我的意思是,ORM使用传统的ADO.NET,SQL并将它们映射到代码中的对象来构建DAL的创新是什么?
Here are the three major peices of my DAL and I am comparing with ORMs to see the benefits:
以下是我的DAL的三个主要方面,我正在与ORM进行比较以了解其中的好处:
-
You still have to have a query in an ORM = SQL (SQL is more powerful by far)
您仍然需要在ORM = SQL中进行查询(到目前为止SQL更强大)
-
Mapping code moves to configuration but still not eliminated, just shifts from one paradigm to another
映射代码移动到配置但仍未消除,只是从一个范例转移到另一个范例
-
Objects have to be defined and managed tightly relatedto your Data Schema unlike in the traditional approach which I can keep them decoupled.
对象必须与您的数据模式紧密相关地定义和管理,这与传统的方法不同,我可以将它们分离。
Am I missing something?
我错过了什么吗?
#11
0
I have been following Fluent-NHibernate very closely as it has some of the most potential I've ever seen in a project.
我一直非常密切关注Fluent-NHibernate,因为它有一些我在项目中见过的最有潜力。
#12
0
I am a big ORM guy, get the logic out of the database, use the database only for speed.
我是一个很大的ORM人,从数据库中获取逻辑,仅使用数据库来提高速度。
I love the speed you can develop an application. The biggest advantage, depending on the ORM, is you can change the back end without having to translate your business logic.
我喜欢你开发应用程序的速度。取决于ORM,最大的优势是您可以在不必翻译业务逻辑的情况下更改后端。
I switched to LINQ and have never looked back. DBLinq is awesome for doing other database than MSSQL. I have used it with MY SQL and it is GREAT.
我切换到LINQ并且从未回头。 DBLinq对于使用除MSSQL之外的其他数据库非常棒。我用它和MY SQL一起使用它很棒。
#13
0
not yet, still skeptical; like most microsoft products, i wait for SP2 or a year and a half before trusting them in a producton environment
还没有,仍然持怀疑态度;像大多数微软产品一样,我等待SP2或一年半之后再相信它们在产品环境中
and note that pretty much every new thing introduced by anyone, not just microsoft, is hailed as "lightweight, fast and easy" - take it with a block of salt. They do not advertise the problems/issues quite as loudly as the benefits/features! That's why we wait for early adopters to discover them.
并注意到几乎每个人都推出的新东西,不仅仅是微软,都被称为“轻巧,快速和简单” - 带上一块盐。他们不会像利益/功能那样大声宣传问题/问题!这就是我们等待早期采用者发现它们的原因。
This is not to disparage ORM or LINQ or anything like that; I'm reserving judgement until
这不是为了贬低ORM或LINQ或类似的东西;直到,我才会保留判断力
- I have time to evaluate them,
- some need arises that only they can satisfy,
- the technology appears stable and well-supported enough to risk in one of my clients' production environments, and/or
- a client requests it
我有时间评估它们,
有些需要只有他们才能满足,
该技术看起来稳定且受到良好支持,足以在我的客户的生产环境中冒险,和/或
客户要求它
Please note: I've done ORM before, manually, and it worked just fine, so I have high hopes for the newer ORM systems.
请注意:我之前已经手动完成了ORM,它运行得很好,所以我对新的ORM系统寄予厚望。
#14
0
If codesmith generates code based on your tables, aren't you still tightly coupled to your data schema? I would prefer a solution that decouples my objects from my database schema for mor flexibility in the architecture
如果codesmith根据您的表生成代码,您是否仍然与数据模式紧密耦合?我更喜欢一种解决方案,它将我的对象与我的数据库模式分离,以便在架构中实现灵活性
That's from one of your comments - It's true, CodeSmith tightly couples you to your tables.
这是你的一条评论 - 这是真的,CodeSmith将你与你的桌子紧密地联系在一起。
NHibernate on the other hand has allot of features that can help with that : you have Components so that in code you can have : Person with a property Address ,where Address is a separate class .
另一方面,NHibernate有许多功能可以帮助你:你有组件,所以在代码中你可以拥有:具有属性地址的人,其中地址是一个单独的类。
You also inheritance mapping. So it does a fair job of decoupling your schema from your domain.
你还继承映射。因此,它可以很好地将您的架构与您的域解耦。
#15
0
We still use a hand rolled, repetitive cut'n'paste DAL where I work. It's extremely painful, complex, and error prone, but it's something all developers can understand. Although it is working for us at the moment, I don't recommend it as it begins to break down quickly on large projects. If someone doesn't want to go to full blown ORM, I'd at least advocate some sort of DAL generation.
在我工作的地方,我们仍然使用手工卷制,重复切割'D'Paste DAL。它非常痛苦,复杂且容易出错,但它是所有开发人员都能理解的。虽然它目前对我们有用,但我不推荐它,因为它开始在大型项目上迅速崩溃。如果有人不想进行全面的ORM,我至少会提倡某种DAL生成。
#16
0
I'm actually working on writing an ORM tool in .NET as a side project to keep me entertained.
我正在努力在.NET中编写一个ORM工具作为一个让我受理的副项目。
SQL is dead to me. I hate writing it, especially having it in my code anywhere. Manually writing select/insert/update/delete queries for each object is a waste of time IMO. And don't even get me started on handling NULLs ("where col_1 = ?" vs "where col_1 is null") when dynamically generating queries. The ORM tools can handle that for me.
SQL对我来说是死的。我讨厌写它,特别是在我的代码中随处可见。手动为每个对象编写select / insert / update / delete查询是浪费时间IMO。在动态生成查询时,甚至不让我开始处理NULL(“where col_1 =?”vs“col_1 is null”)。 ORM工具可以为我处理。
Also, having 1 place that SQL may be dynamically generated would hopefully go a long was to eliminating SQL injection attacks.
此外,有一个地方可以动态生成SQL可能需要很长时间才能消除SQL注入攻击。
On the other hand, I've used Hibernate, and I absolutely hate it. In a real-word project, we ran into limitations, unimplemented bits, and bugs every few weeks.
另一方面,我使用过Hibernate,我绝对讨厌它。在一个真实的单词项目中,我们每隔几周就遇到限制,未实现的位和错误。
Keeping the query logic DB side (usually as a view or stored procedure) also has the benefit of being available for DBAs to tune. That was a constant battle at my last job, using Hibernate. DBAs: "give us all the possible queries so we can tune" Devs: "uh, I don't know because Hibernate will generate them on the fly. I can give you some HQL and an XML mapping though!" DBAs: "Don't make me punch you in the face!"
保持查询逻辑数据库端(通常作为视图或存储过程)也具有可供DBA调整的优点。使用Hibernate,这是我上一份工作的一场持续战斗。 DBA:“给我们所有可能的查询,以便我们可以调整”Devs:“呃,我不知道,因为Hibernate会动态生成它们。我可以给你一些HQL和XML映射!” DBA:“不要让我打你的脸!”
#17
0
I dislike the code generation used in most ORMs. In fact, code generation in general I find to be a weak tool that is usually indicative of using the wrong language in the first place.
我不喜欢大多数ORM中使用的代码生成。实际上,一般来说,代码生成我发现它是一个弱工具,通常表示首先使用错误的语言。
In particular with .Net reflection, I don't see any need for code gen for ORM purposes.
特别是.Net反射,我认为不需要代码生成用于ORM目的。
#18
#19
0
No, I dumped ORMs and switched to an smalltalk and an OODB: Gemstone. Better code, less code, faster development.
不,我倾倒了ORM并转而使用了一个小屁股和一个OODB:Gemstone。更好的代码,更少的代码,更快的开发。