问题描述
我对 .NET 比较陌生,并且使用 Linq2Sql 已经快一年了,但它缺少我现在正在寻找的一些功能.
I'm relatively new to .NET and have being using Linq2Sql for a almost a year, but it lacks some of the features I'm looking for now.
我将开始一个新项目,我想在其中使用具有以下特征的 ORM:
I'm going to start a new project in which I want to use an ORM with the following characteristics:
- 它必须非常高效,我不想处理访问层来从数据库中保存或检索对象,但它应该允许我在实际将其提交到数据库之前轻松调整任何对象;它还应该允许我轻松地使用不断变化的数据库架构
- 它应该允许我扩展从数据库映射的对象,例如向它们添加虚拟属性(虚拟列到表)
- 它必须(至少几乎)与数据库无关,它应该允许我以透明的方式使用不同的数据库
- 它必须没有太多配置或必须基于约定才能使其工作
- 它应该允许我使用 Linq
那么,你知道我可以使用的任何 ORM 吗?感谢您的帮助.
So, do you know any ORM that I could use? Thank you for your help.
EDIT 我知道一个选项是使用 NHibernate.这似乎是企业级应用程序的事实标准,但由于其深度学习曲线,似乎效率不高.换句话说,我在 SO 中的其他一些帖子中读到了它与 Linq 的集成不佳.这都是真的吗?
EDIT I know that an option is to use NHibernate. This appears as the facto standard for enterprise level applications, but also it seems that is not very productive because its deep learning curve. In other way, I have read in some other post here in SO that it doesn't integrate well with Linq. Is all of that true?
推荐答案
也许您最好的选择是使用 NHibernate.它可以说是最好的行业标准".当涉及商业和开源 ORM 时.它变得真正稳定已经有很长一段时间了,被许多企业公司使用,基于更为人所知的 Hibernate (java),但已经完全重写以充分利用 .NET 功能.
Perhaps your best bet is using NHibernate. It's arguably the best "industry standard" when it comes to both commercial and open source ORMs. It has been around a long while to become really stable, is used in many enterprise companies, is based on the even better known Hibernate (java), but has fully been rewritten to make the best use of .NET features.
这听起来像是我是 NHibernate 的拥护者.也许我是.但是 NHibernate 有一个缺点:它有一个陡峭的学习曲线并且习惯于许多可能性并选择正确的或最好的".即使对于有经验的开发人员,针对您的情况进行实践也可能令人生畏.但这是为几乎可以做任何事情的企业级 ORM 支付的奖励.
This sounds like I'm an advocate of NHibernate. Perhaps I am. But NHibernate has a drawback: it has a steep learning curve and getting used to the many possibilities and choosing the right or "best" practice for your situation can be daunting, even for experienced developers. But that's the prize to pay for an enterprise-level ORM that's capable of virtually anything.
许多这些缺点和设置问题在您开始使用 Fluent Nhibernate 的那一刻就消失了,就我个人而言,我几乎不再需要它了因为它一次(几乎)消除了 NHibernate 的所有繁琐.
Many of these drawbacks and setup problems vaporize the minute you start using Fluent Nhibernate, personally, I hardly do without it anymore as it removes all the tediousness of NHibernate at once (almost).
它使使用 NHibernate 变得轻而易举:只需将您的实体编写为 POCO 并完全自动加载它们以创建您的数据库、关联等(或者如果已经存在,则不要创建模式).使用 Fluent 语法配置您的数据库.一个非常简单的设置看起来像这样基本:
It makes working with NHibernate a breeze: just write your entities as POCOs and load them fully automatically to create your database, the associations etc (or don't create the schema if it's there already). Configure your database using the Fluent syntax. A very simple setup can look as basic as this:
// part of a default abstract setup class I use
public ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c =>
c.Server(this.ServerName)
.Database(this.DatabaseName)
.Username(this.Username)
.Password(this.Password)
)
)
.Mappings(m =>
m.AutoMappings.Add(AutoMap.AssemblyOf<User>() // loads all POCOse
.Where(t => t.Namespace == this.Namespace))
// here go the associations and constraints,
// (or you can annotate them, or add them later)
)
.ExposeConfiguration(CreateOrUpdateSchema)
.BuildSessionFactory();
}
// example of an entity
// It _can_ be as simple as this, which generates the schema, the mappings ets
// but you still have the flexibility to expand and to map using more complex
// scenarios. It is not limited to just tables, you can map views, stored procedures
// create triggers, associations, unique keys, constraints etc.
// The Fluent docs help you step by step
public class User
{
public virtual int Id { get; private set; } // autogens PK
public virtual string Name { get; set; } // augogens Name col
public virtual byte[] Picture { get; set; } // autogens Picture BLOB col
public virtual List<UserSettings> Settings { get; set; } // autogens to many-to-one
}
public class UserSettings
{
public virtual int Id { get; private set: } // PK again
public virtual int UserId { get; set; } // autogens FK
public virtual User { get; set; } // autogens OO-mapping to User table
}
获取所有 POCO 实体并自动映射它们,为 ORM 创建配置并在数据库中构建架构,前提是用户具有足够的权限.Fluent 的一项非常强大的功能(以及 NH 的较小扩展)是在您进行任何更改时更新数据库架构.
which takes all POCO entities and automatically maps them, creates the configuration for the ORM and builds the schema in the database, provided the user has sufficient rights. One very powerful ability of Fluent (and NH to a lesser extend) is to update a database schema when you make any changes.
还有一个好处:存在许多自动生成工具(包括开源 MyGeneration),可以将您的来自简单 ODBC 或其他连接的 DB 模式,并将它们转换为正确的实体类、关联和 HBM 配置文件.其中许多工具(部分)是图形设计辅助工具.
Also on the upside: many auto generation tools exist (including the open source MyGeneration) that can take your DB schema(s) from a simple ODBC or other connection and turn them into the correct entity classes, associations and HBM configuration files. Many of these tools are (partially) graphical design aids.
请务必阅读 NHibernate 最佳实践.它使泛型和 DAO 更上一层楼.您还可以跳到追逐并深入使用 S#arp (下载),这是一个强加所有这些最佳实践并将 NUnit 添加到混合物中的框架.
Make sure to read NHibernate best practices. It brings generics and DAO to the next level. You can also skip to the chase and dive deep with S#arp (download), which is a framework that imposes all these best practices and adds NUnit to the mixture.
在我开始使用一项新技术之前,我通常希望它得到很好的覆盖.NHibernate 和 Hibernate 在这里并不缺.很多书籍从入门到专业讲解(N)Hibernate,白皮书丰富,工具文档也相当出色.
Before I start using a new technology I usually want it well covered. NHibernate and Hibernate don't come short here. Many books explain (N)Hibernate from starter to professional, white papers are abundant and tool documentation is meanwhile rather excellent.
LINQ 和 NHibernate 在多对 X 映射和其他关联中使用的所有类型的 ICollection<>
中一直配合得很好,但需要首先检索数据,需要一个好的设计(缓存在这里有帮助),否则它的性能会很差.自从LINQ出现以来,这一直被认为是NH的痛点.
LINQ and NHibernate have always gone well together through all types of ICollection<>
which are used in the many-to-X mappings and other associations, but requires the data to be retrieved first which requires a good design (the cache helps here), otherwise it'll perform badly. This has been considered a sore point of NH ever since LINQ came about.
幸运的是,现在镇上有一个新孩子:NHibernate-LINQ,它在提交之前将 LINQ 查询映射到 ICriteria
查询.ICriteria 查询得到了很好的缓存,并且与 LINQ 的这种组合非常强大且非常高效.NH-LINQ 现在是标准发行版的一部分.
Luckily, there's now a new kid in town: NHibernate-LINQ, which maps LINQ queries to ICriteria
queries prior to submitting. ICriteria queries are well cached and this combination with LINQ is both very powerful and very performant. NH-LINQ is now part of the standard distribution.
我已经使用 NHibernate 将近十年了(首先是 Java,后来是 .NET).我曾与其他 ORM 的商业和开源调情,但最终总是回到 NH(除非公司政策要求不同,但这种情况很少见).这个故事可能听起来有点偏颇,但这里的篇幅太短,无法详细介绍 NHibernate 与其他技术的比较.
I've used NHibernate for a almost decade (first Java, later .NET). I've flirted with other ORM's both commercial and open source, but in the end always returned to NH (unless company policy demanded different, but that was rare). This story may sound a bit biased but the space here is too short to go into excruciating detail about how NHibernate compares to other technologies.
其他 ORM 很可能更适合您的需求,尤其是如果您从未打算在复杂的多数据库、多数据库服务器或难以映射到 OO 的遗留情况下使用它.对我来说,NH 很出色,因为它不会以任何方式限制我并支持完整的往返工程,但是如果这里讨论的更轻的 ORM 的功能对您来说重量更重,那么您的选择可能会有所不同.
It's very well possible that other ORM's better fit your need, especially if you never plan to use it in complex multi-database, multi-db-server or hard-to-map-to-OO legacy situations. For me, NH shines because it doesn't limit me in any which way and supports full roundtrip engineering, but your choice might turn out different if features of lighter ORM's that are discussed here weigh heavier for you.
更新:添加了代码示例
更新:扩展了代码示例,修正了拼写错误和措辞
更新:小章节,添加LINQ部分,添加免责声明部分
Update: added code sample
Update: expanded code sample, fixed typos and wording
Update: little chapters, added LINQ part, added Disclaimer part
这篇关于我应该使用什么 ORM for .net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!