SQL 数据库表中的 C# 类

发布于 2024-07-06 20:19:39 字数 290 浏览 8 评论 0原文

遇到这个:

http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp

并且想知道这是否是正确的解决方案,因为我不太热衷于为每个存储过程创建一个类,或者我是否将 Enterprise Library 用于 ASP.net 2.0 项目。

Came across this:

http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp

And wondering if this is the right solution as I am not that big of a fan of creating a class for every stored procedure or do I use Enterprise Library for ASP.net 2.0 project.

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

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

发布评论

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

评论(4

困倦 2024-07-13 20:19:39

您绝对不应该为每个存储过程创建一个类。 您可以采用多种方法来处理数据库交互。 您应该仔细研究现有的主要框架,然后决定哪一个最适合您。 Castle 项目解决方案很棒,并且依赖于 nHibernate (nHibernate)。 LINQ 是 Mircrosoft 提供的类似产品(LINQ 项目)。 这两个解决方案都是完整的 ORM 框架(对象关系映射),并将生成动态 SQL 以将对象保留在数据库中。 每个都有自己的怪癖,并且喜欢您以特定的方式构建对象。 如果您不想管理系统使用的 SQL,我肯定会推荐其中一种方法。

我有数据库背景,更喜欢对 SQL 有更多的控制。 我特别喜欢通过存储过程来处理交互。 我发现这使我能够更好地控制 SQL 进行优化,同时帮助我以更友好的方式管理数据库安全性。 为了适应这种方法,我推荐像 iBatis (iBatis) 之类的东西。 iBatis 不是一个完整的 ORM,而是一个简单的 SQL 映射器。 我的方法的缺点是您需要编写更多代码(SQL),但我不介意这种权衡。

You definitely shouldn't be creating a class for every stored procedure. There are a number of approaches you can take to handling your database interactions. You should have a good look at the major frameworks out there and decide which one best suits you. The Castle Project solution is great, and relies on nHibernate (nHibernate). LINQ is a similar offering by Mircrosoft (LINQ Project). Both of these solutions are full ORM frameworks (Object Relational Mapping) and will generate dynamic SQL to persist your objects in the database. Each also has it's own quirks and likes you to structure your objects in particular ways. If you don't want to manage the SQL your system uses, I would definitely recommend one of these approaches.

I come from a database background, and prefer a bit more control over my SQL. In particular I like to have my interractions handled by stored procedures. I find this enables me to control both the SQL better for optimisation, but helps me manage database security in a more friendly manner. To accommodate this approach, I recommend something like iBatis (iBatis). iBatis isn't a full ORM, but rather a simple SQL mapper. The downside to my approach is that you need to write a lot more code (SQL), but I don't mind the trade-off.

几味少女 2024-07-13 20:19:39

是否有可能升级到framework 3.5? 如果是这样,请查看 LINQ to SQL 和实体框架,因为这将为您完成很多工作。

如果没有,那么只要它生成不将您绑定到第 3 方库的标准代码,那么您当然可以使用它。 在我的工作场所,我们有自己的与此类似的生成器,尽管我们很快就会转向 LINQ to SQL,但它运行良好。

Is there any possibility of upgrading to framework 3.5? if so take a look at LINQ to SQL and Entity Framework as this will accomplish alot of this for you.

If not then as long as it generates standard code that doesnt tie you into 3rd party libraries then you could certainly use it. At my workplace we have our own generator similar to this and it works well although we will shortly be moving to LINQ to SQL.

一个人练习一个人 2024-07-13 20:19:39

在 C# 类中包装数据库表的方法有很多种; 在您链接到的框架和实体框架之间进行选择之前,您可能需要研究一些替代方案。

有一种称为“活动记录模式”的软件模式,它准确地描述了这种方法 - 每个表一个 C# 类,具有加载/保存方法,如 Customer.GetById()、Customer.Save() 等。

对于 ASP.NET 2.0,请查看 Castle 项目的 ActiveRecord 实现 和第三方名为 ActiveWriter 的 Visual Studio 插件工具,可让您使用拖放界面为表生成类包装器。

There are many ways of wrapping a database table in a C# class; you probably want to investigate a few alternatives before choosing between the one you've linked to and the Entity Framework.

There's a software pattern called the "active record pattern" which describes exactly this approach - one C# class for each table, with load/save methods like Customer.GetById(), Customer.Save(), and so on.

For ASP.NET 2.0, check out the Castle Project's ActiveRecord implementation and a third-party Visual Studio plugin tool called ActiveWriter that lets you generate class wrappers for your tables using a drag'n'drop interface.

苯莒 2024-07-13 20:19:39

您需要确定在什么时候需要由表组成的数据集,以及您是否希望 SQL 使用存储过程生成这些数据,或者您的业务逻辑层是否将处理这些数据。 正如 Dr8k 所说,nHibernate 将为您创建 SQL,但是 nHibernate 有一个学习曲线。 ORM 将控制您获取数据的方式,并且根据您的环境和 DBA 的舒适程度,您可能还需要克服其他问题。

如果您更熟悉 SQL,那么还有另一个名为 SubSonic 的工具,它将为您创建 Active Record 等包装器同时还为您提供使用存储过程的能力。 如果您无法使用 LINQ,还有一个界面流畅的不错的查询工具,您可以使用它。

You will need to determine at what point you need sets of data that are composed from your tables, and whether you want SQL to produce these with stored procedures or if your business logic layer will handle these. As Dr8k says, nHibernate will create SQL for you, but there is a learning curve with nHibernate. The ORM will be in control of how you are getting the data and depending on your environment and DBA's conmfort level you may other issues to overcome.

If you more comfortable with SQL, then there is another tool called SubSonic that will create wrappers ala Active Record for you while offering you the ability to use stored procedures as well. There is also a nice query tool with a fluent interface that you can use if you are not able to use LINQ.

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