简体   繁体   English

如何改进映射到数据库的业务层对象? 是时候使用 O/R 映射器了吗?

[英]How can I improve my business layer objects mapping into a database? Is it time for a O/R mapper?

As I began writing web applications with ASP.NET I started with small projects that used a Linq-To-SQL mapper for database access to a MSSQL Server.当我开始使用 ASP.NET 编写 Web 应用程序时,我从使用 Linq-To-SQL 映射器来访问 MSSQL 服务器的数据库的小项目开始。
After gaining some experience, I switched into a classic three-tiered approach with a graphic Layer, business Layer, and a data Layer.在积累了一些经验后,我转而采用经典的三层方法,即图形层、业务层和数据层。 The only function of the data layer was to provide insert/update/delete-methods without any logic and logic the form of selection methods.数据层的唯一功能是提供没有任何逻辑的插入/更新/删除方法和逻辑选择方法的形式。

Over the time I realized that it would be better not to provide the database classes up to the GUI (took some time, unfortunately).随着时间的推移,我意识到最好不要向 GUI 提供数据库类(不幸的是,花了一些时间)。 I switched to using business classes in the BL that are used for all operations performed by the BL and displayed by the GUI in the form of getting List from the business layer.我转而使用 BL 中的业务类,这些业务类用于 BL 执行的所有操作,并以从业务层获取 List 的形式由 GUI 显示。
A great advantage is that I can provide additional properties that are not represented by the database itself.一个很大的优势是我可以提供数据库本身没有表示的其他属性。 However, I did that mapping inside the business layer myself with methods that mapped the corresponding business layer class to the database class.但是,我自己在业务层内部使用将相应业务层类映射到数据库类的方法进行了映射。

I guess that's where O/R mapper come in handy?我想这就是 O/R 映射器派上用场的地方? Until now, I haven't realized their purpose, but I think I just found it.直到现在,我还没有意识到他们的目的,但我想我刚刚找到了。 I've recently tried out using the new Entity Framework with .NET Framework 4, but I'm only using it like the Linq-To-SQL DataContext.我最近尝试在 .NET Framework 4 中使用新的实体框架,但我只是像使用 Linq-To-SQL DataContext 一样使用它。

Is there a way to achieve the mapping automatically?有没有办法自动实现映射? If yes, is that something the new Entity Framework provides or do I need to look for a O/R Mapper like NHibernate?如果是,那是新实体框架提供的,还是我需要寻找像 NHibernate 这样的 O/R 映射器?

I use NHibernate exclusively in my projects.我只在我的项目中使用 NHibernate。 I like the control and flexibility it gives me.我喜欢它给我的控制力和灵活性。 There is a 'shortcut' called Active Record that uses NHibernate under the covers but provides a really nice an simple interface to NHibernate.有一个名为Active Record的“快捷方式”,它在幕后使用 NHibernate,但为 NHibernate 提供了一个非常好的简单界面。

NHibernate has a steep learning curve, but when you get past that - it is really smooth sailing. NHibernate 有一个陡峭的学习曲线,但是当你通过它时 - 它真的是一帆风顺。 When (and if) you venture the way of NHibernate, check out Ayende for cool tips.当(并且如果)您冒险使用 NHibernate 的方式时,请查看Ayende以获得很酷的提示。

(Entity Framework is an O/R Mapper.) (实体框架一个 O/R 映射器。)

If you're serious about getting your hands dirty with ORM (but relatively new to that area), I highly recommend something like TekPub's videos on these topics.如果您真的很想接触 ORM(但在该领域相对较新),我强烈推荐类似 TekPub 的关于这些主题的视频的内容。 You'll be able to see these tools in use starting from scratch.您将能够从头开始使用这些工具。 It is a graceful introduction to some simple, but real-world issues like the ones you mention.它优雅地介绍了一些简单但真实的问题,例如您提到的问题。

I've had a great time using Entity Framework 4.0 (+ the CTP).我在使用 Entity Framework 4.0(+ CTP)时玩得很开心。 I think you'd have a much easier time dealing with an ORM like that.我认为你会更轻松地处理这样的 ORM。 EF4 provides everything you need to interoperate with MSSQL from C#/.NET. EF4 提供了与 C#/.NET 中的 MSSQL 进行互操作所需的一切 You won't have to write a single line of SQL, and it has full support for LINQ (through ObjectQuery).您不必编写一行 SQL,并且它完全支持 LINQ(通过 ObjectQuery)。

LinqToSql is an ORM, so you are already using one. LinqToSql 是一个 ORM,所以您已经在使用它了。 Taking LinqToSql out and replacing it with EntityFramework or NHibernate won't solve the problems you appear to be having right now.取出 LinqToSql 并用 EntityFramework 或 NHibernate 替换它并不能解决您现在似乎遇到的问题。

Here are some things you should learn more about to help give you additional context:以下是一些您应该了解的更多信息,以帮助您提供更多背景信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM