ORM有什么好处?

时间:2023-01-15 20:27:06

So I'm having a head against the wall moment and hoping somebody can come help either remove the wall or stop my head from moving!!

所以我有一个头撞墙的时刻,并希望有人可以来帮助移除墙壁或阻止我的头移动!

Over the last 3/4 weeks I've been investigating ORM's in readyness for a new project. The ORM must map to an existing, large and ageing SQL database.

在过去的3/4周里,我一直在调查ORM为新项目做好准备。 ORM必须映射到现有的,大型且老化的SQL数据库。

So I tried Subsonic. I really liked v2 and v3 after modding to work nicely with VB and named schemas in SQL was running OK. However, its lack of flexibility of having separate entity properties names vs column names had me pulling my hair out (sorry Rob).

所以我尝试了亚音速。我非常喜欢mod2之后的v2和v3以便与VB很好地协作并且SQL中的命名模式运行正常。然而,由于缺乏具有单独的实体属性名称与列名称的灵活性,我把头发拉了出来(抱歉Rob)。

I tried Entity Framework but I found like others it lacking in certain areas.

我试过实体框架,但我发现其他地方缺乏某些领域。

So I bit the bullet and tried nHibernate but after a week or so getting it working how I liked (with help from Codesmith to generate classes/hbms for me) I'm frustrated with the time it takes to startup (build a config object), despite trying a number of tricks to reduce this time.

所以我咬了一口子并尝试了nHibernate,但是经过一周左右的时间让它工作我喜欢(在Codesmith的帮助下为我生成类/ hbms)我对启动时间(构建配置对象)感到沮丧尽管尝试了一些技巧来减少这个时间。

I'm essentially after building a DAL class that I can share between apps and websites. Am I barking up the wrong tree? For a legacy project with 100s of tables should I go back to ado.net and use DTOs? Aarrgh!

我基本上是在构建了一个可以在应用程序和网站之间共享的DAL类。我吠叫错了树吗?对于包含100个表的遗留项目,我应该回到ado.net并使用DTO吗? Aarrgh!

Sorry for the ranty style of question. I don't have much hair left and I'd like to keep what I have!!

对不起,这是一个大问题。我没有多少头发,我想保留我拥有的东西!

Thanks in advance, Ed

在此先感谢,Ed

PS. I should add that I know SQL very well and not scared of getting my hands dirty to write fast queries. If anything I don't need to be hid from SQL

PS。我应该补充一点,我非常了解SQL,并且不怕写脏快速查询。如果有什么我不需要隐藏SQL

4 个解决方案

#1


8  

ORM let's you:

ORM让你:

  1. To map table rows to objects, that are the the workable pieces of object oriented programming.
  2. 将表行映射到对象,这是面向对象编程的可行部分。

  3. To automatically navigate through object relationships
  4. 自动浏览对象关系

  5. To easily add, edit and remove table rows
  6. 轻松添加,编辑和删除表行

  7. To query the database in a more intuitive way as you don't have to think of joins (this one will depend on the ORM and the query method)
  8. 以更直观的方式查询数据库,因为您不必考虑连接(这将取决于ORM和查询方法)

  9. To transparently handle L1 and L2 cache.
  10. 透明地处理L1和L2缓存。

All of the above would have to be handled by hand if you werent using ORM.

如果您不使用ORM,则必须手动处理以上所有内容。

PS: I agree to Dmitry as to the startup time of NHibernate (see question comments). Besides, did you try Fluent NHibernate? Fluent NHibernate is impressively easy. I couldn't believe my eyes when I first mapped a database. It's even easier than proprietary ORMs like DevExpress XPO.

PS:我同意Dmitry关于NHibernate的启动时间(参见问题评论)。此外,你有没有试过Fluent NHibernate?流畅的NHibernate令人印象深刻。我第一次映射数据库时无法相信自己的眼睛。它比DevExpress XPO这样的专有ORM更容易。

#2


2  

The biggest benefit of an ORM tool is that it will help you layer your application correctly. Most project nowadays use a Data Layer to connect to the database. You start from the ORM tool to produce classes that correspond to your database objects. Then you define an interface using these methods. All persistence code uses the methods of this interface. This way the business logic layer is only coupled to this higher-layer interface and needs to know nothing about the database. In fact there should be no dependency on ADO.NET or even NHibernate.

ORM工具的最大好处是它可以帮助您正确分层应用程序。现在大多数项目使用数据层连接到数据库。您可以从ORM工具开始生成与数据库对象对应的类。然后使用这些方法定义接口。所有持久性代码都使用此接口的方法。这样,业务逻辑层只耦合到这个更高层的接口,并且不需要对数据库一无所知。实际上应该不依赖于ADO.NET甚至NHibernate。

Another advantage of ORM tools is that you de-couple your application from the database server. You could change the db engine and still use the same code. Also there isn't only the complexity of the SQL that the ORM hides from you. It can also help you with transactions logic and connection pooling.

ORM工具的另一个优点是您可以将应用程序与数据库服务器分离。您可以更改数据库引擎并仍然使用相同的代码。此外,不仅有ORM隐藏的SQL的复杂性。它还可以帮助您处理事务逻辑和连接池。

I'd say that for new projects an ORM tool is a necessity. For legacy projects it isn't so much beneficial, unless of course you have the time/money to start from scratch.

我想说,对于新项目,ORM工具是必需的。对于遗留项目来说,它并没有那么有用,除非你有时间/金钱从头开始。

#3


1  

In my experience, most ORMs end up being way more complex than SQL. Which defeats the entire purpose of using them.

根据我的经验,大多数ORM最终会比SQL更复杂。这违背了使用它们的全部目的。

One solution I'm enthusiastic about is LINQ2SQL. It excels as a thin layer about stored procedures or views. It's really easy to use and doesn't try to hide SQL.

我热衷的一个解决方案是LINQ2SQL。它擅长于存储过程或视图的薄层。它非常易于使用,并且不会尝试隐藏SQL。

#4


1  

There are basically two questions here:

这里基本上有两个问题:

What's great about ORMs? There are similar questions on *. See:

ORM有什么好处? *上有类似的问题。看到:

How can I improve NHibernate startup time? See:

如何改善NHibernate的启动时间?看到:

#1


8  

ORM let's you:

ORM让你:

  1. To map table rows to objects, that are the the workable pieces of object oriented programming.
  2. 将表行映射到对象,这是面向对象编程的可行部分。

  3. To automatically navigate through object relationships
  4. 自动浏览对象关系

  5. To easily add, edit and remove table rows
  6. 轻松添加,编辑和删除表行

  7. To query the database in a more intuitive way as you don't have to think of joins (this one will depend on the ORM and the query method)
  8. 以更直观的方式查询数据库,因为您不必考虑连接(这将取决于ORM和查询方法)

  9. To transparently handle L1 and L2 cache.
  10. 透明地处理L1和L2缓存。

All of the above would have to be handled by hand if you werent using ORM.

如果您不使用ORM,则必须手动处理以上所有内容。

PS: I agree to Dmitry as to the startup time of NHibernate (see question comments). Besides, did you try Fluent NHibernate? Fluent NHibernate is impressively easy. I couldn't believe my eyes when I first mapped a database. It's even easier than proprietary ORMs like DevExpress XPO.

PS:我同意Dmitry关于NHibernate的启动时间(参见问题评论)。此外,你有没有试过Fluent NHibernate?流畅的NHibernate令人印象深刻。我第一次映射数据库时无法相信自己的眼睛。它比DevExpress XPO这样的专有ORM更容易。

#2


2  

The biggest benefit of an ORM tool is that it will help you layer your application correctly. Most project nowadays use a Data Layer to connect to the database. You start from the ORM tool to produce classes that correspond to your database objects. Then you define an interface using these methods. All persistence code uses the methods of this interface. This way the business logic layer is only coupled to this higher-layer interface and needs to know nothing about the database. In fact there should be no dependency on ADO.NET or even NHibernate.

ORM工具的最大好处是它可以帮助您正确分层应用程序。现在大多数项目使用数据层连接到数据库。您可以从ORM工具开始生成与数据库对象对应的类。然后使用这些方法定义接口。所有持久性代码都使用此接口的方法。这样,业务逻辑层只耦合到这个更高层的接口,并且不需要对数据库一无所知。实际上应该不依赖于ADO.NET甚至NHibernate。

Another advantage of ORM tools is that you de-couple your application from the database server. You could change the db engine and still use the same code. Also there isn't only the complexity of the SQL that the ORM hides from you. It can also help you with transactions logic and connection pooling.

ORM工具的另一个优点是您可以将应用程序与数据库服务器分离。您可以更改数据库引擎并仍然使用相同的代码。此外,不仅有ORM隐藏的SQL的复杂性。它还可以帮助您处理事务逻辑和连接池。

I'd say that for new projects an ORM tool is a necessity. For legacy projects it isn't so much beneficial, unless of course you have the time/money to start from scratch.

我想说,对于新项目,ORM工具是必需的。对于遗留项目来说,它并没有那么有用,除非你有时间/金钱从头开始。

#3


1  

In my experience, most ORMs end up being way more complex than SQL. Which defeats the entire purpose of using them.

根据我的经验,大多数ORM最终会比SQL更复杂。这违背了使用它们的全部目的。

One solution I'm enthusiastic about is LINQ2SQL. It excels as a thin layer about stored procedures or views. It's really easy to use and doesn't try to hide SQL.

我热衷的一个解决方案是LINQ2SQL。它擅长于存储过程或视图的薄层。它非常易于使用,并且不会尝试隐藏SQL。

#4


1  

There are basically two questions here:

这里基本上有两个问题:

What's great about ORMs? There are similar questions on *. See:

ORM有什么好处? *上有类似的问题。看到:

How can I improve NHibernate startup time? See:

如何改善NHibernate的启动时间?看到: