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

发布于 2024-09-16 18:07:39 字数 537 浏览 7 评论 0原文

当我开始使用 ASP.NET 编写 Web 应用程序时,我从一些小项目开始,这些项目使用 Linq-To-SQL 映射器来访问 MSSQL Server 的数据库。
在获得一些经验后,我转向了经典的三层方法,即图形层、业务层和数据层。数据层的唯一功能是提供没有任何逻辑的插入/更新/删除方法和选择方法形式的逻辑。

随着时间的推移,我意识到最好不要向 GUI 提供数据库类(不幸的是,花了一些时间)。我转而使用BL中的业务类,这些业务类用于BL执行的所有操作,并通过GUI以从业务层获取List的形式显示。
一个很大的优点是我可以提供数据库本身未表示的附加属性。但是,我自己使用将相应的业务层类映射到数据库类的方法在业务层内部进行了映射。

我想这就是 O/R 映射器派上用场的地方?直到现在,我还没有意识到他们的目的,但我想我刚刚找到了。我最近尝试将新的实体框架与 .NET Framework 4 一起使用,但我只是将其用作 Linq-To-SQL DataContext。

有没有办法自动实现映射?如果是,这是新实体框架提供的东西还是我需要寻找像 NHibernate 这样的 O/R 映射器?

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.
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). 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.
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? 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

决绝 2024-09-23 18:07:39

我在我的项目中专门使用 NHibernate。我喜欢它给我的控制力和灵活性。有一个名为 Active Record 的“快捷方式”,它在幕后使用 NHibernate,但提供了一个非常好的NHibernate 的简单接口。

NHibernate 有一个陡峭的学习曲线,但是当你克服它时 - 它真的是一帆风顺。当(如果)您尝试使用 NHibernate 时,请查看 Ayende 以获取很酷的提示。

I use NHibernate exclusively in my projects. 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.

NHibernate has a steep learning curve, but when you get past that - it is really smooth sailing. When (and if) you venture the way of NHibernate, check out Ayende for cool tips.

氛圍 2024-09-23 18:07:39

(实体框架一个 O/R 映射器。)

如果您真的想亲自接触 ORM(但对该领域相对较新),我强烈推荐诸如 TekPub 的有关这些主题的视频之类的内容。您将能够从头开始看到这些工具的使用情况。这是对一些简单但现实世界的问题(如您提到的问题)的优雅介绍。

(Entity Framework is an O/R Mapper.)

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. 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.

_畞蕅 2024-09-23 18:07:39

LinqToSql 是一种 ORM,因此您已经在使用它了。取出 LinqToSql 并用 EntityFramework 或 NHibernate 替换它并不能解决您现在遇到的问题。

您应该了解以下一些内容,以帮助您获得更多上下文:

LinqToSql is an ORM, so you are already using one. Taking LinqToSql out and replacing it with EntityFramework or NHibernate won't solve the problems you appear to be having right now.

Here are some things you should learn more about to help give you additional context:

伴梦长久 2024-09-23 18:07:39

我使用 Entity Framework 4.0(+ CTP)度过了一段愉快的时光。我认为处理这样​​的 ORM 会容易得多。 EF4 提供了与 C#/.NET 中的 MSSQL 互操作所需的一切。您无需编写一行 SQL,并且它完全支持 LINQ(通过 ObjectQuery)。

I've had a great time using Entity Framework 4.0 (+ the CTP). I think you'd have a much easier time dealing with an ORM like that. EF4 provides everything you need to interoperate with MSSQL from C#/.NET. You won't have to write a single line of SQL, and it has full support for LINQ (through ObjectQuery).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文