我应该使用什么ORM的.NET? [英] What ORM for .net should I use?

查看:213
本文介绍了我应该使用什么ORM的.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是比较新的.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.

修改我知道,一个选择是使用NHibernate的。这似乎为企业级应用的事实标准,而且似乎不是很有成效,因为其深厚的学习曲线。在其他的方式,我看了这里的一些其他职位,以便它不使用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的。这可以说是最好的行业标准,当涉及到商业和开源奥姆斯。它已经存在了很长一段时间才能成为真正稳定,被用在许多企业公司,是基于更好地称为休眠(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.

许多缺点和设置问题的汽化你开始使用功能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的工作变得轻而易举:只写为波苏斯您的实体,并加载它们完全自动地创建数据库,协会等(或不创建模式,如果它的存在的话)。用流利的语法配置数据库。一个非常简单的设置可以看看基本的内容:

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创建配置,并建立了数据库的模式,只要用户有足够的权限。流利的一个(和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 ),它可以把你的DB模式(S)从一个简单的ODBC或其他连接,并把它们变成正确的实体类,关联和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到一个新的水平。您也可以跳到追逐和潜入深海<一href="http://devlicio.us/blogs/billy%5Fmccafferty/archive/2008/04/21/asp-net-mvc-best-practices-with-nhibernate-and-spring-net.aspx">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和休眠不来这里短。许多书籍解释(N)从启动休眠专业,白皮书丰富,工具,文档同时相当出色的。

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一直进展顺利相结合,所有类型的的ICollection&LT;&GT; 这是用来在多对-X的映射和其他协会,但要求要检索的数据首先需要一个好的设计(缓存有助于在这里),否​​则它会表现不佳。这被认为是一个痛点的NH自从LINQ来的。

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.

幸运的是,现在有在城里有一个新来的小子:<一href="http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx">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的更好地满足您的需要,特别是如果你不打算使用它在复杂的多数据库,多数据库服务器或难以映射到面向对象的遗留情况。对我来说,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.

更新:添加code样品
更新:扩展code样品,固定错别字和措辞
更新:小章节,补充LINQ的一部分,加入声明的一部分

Update: added code sample
Update: expanded code sample, fixed typos and wording
Update: little chapters, added LINQ part, added Disclaimer part

这篇关于我应该使用什么ORM的.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆