简体   繁体   English

数据访问层新手

[英]Data Access Layer newbie

Newbie to .NET data apps here, coming from a Visual Foxpro background. .NET 数据应用程序的新手,来自 Visual Foxpro 背景。

I'm planning on an ASP.NET and/or Silverlight UI, and maybe some WPF client stuff too on our LAN too, so I want to craft a data access layer that can support all these front ends.我也计划在我们的 LAN 上使用 ASP.NET 和/或 Silverlight UI,也许还有一些 WPF 客户端的东西,所以我想制作一个可以支持所有这些的数据访问层。

My data will be in SQL server.我的数据将在 SQL 服务器中。 I have already made a test run of pushing the data from Foxpro to SQL Server 2005. It went well.我已经进行了从 Foxpro 推送数据到 SQL Server 2005 的测试运行。它很顺利。 That gave me a SQL server data store to play with.这给了我一个 SQL 服务器数据存储来玩。

Now, here are the data tools I played with so far, trying to get familiar with .NET data access:现在,这里是我到目前为止玩过的数据工具,试图熟悉 .NET 数据访问:

  1. I've played with Linq-To-Sql, and made a test form using the typed objects and collections from L2S, and then populating some WPF listviews and other UI controls.我玩过 Linq-To-Sql,并使用 L2S 中的类型化对象和 collections 制作了一个测试表单,然后填充了一些 WPF 列表视图和其他 UI 控件。 That was cool.那很酷。 Linq is cool. Linq 很酷。 WPF is cool: See screenshot of form here: http://twitpic.com/26w26/full WPF 很酷:请在此处查看表格截图: http://twitpic.com/26w26/full

  2. I've played with what I guess you'd call classic ADO.Net DataSets.我玩过我猜你会称之为经典 ADO.Net 数据集的东西。 Man, DataSets seem like a lot of work... A SQLConncetion A SQLCommand A DataSet A TableAdapter伙计,数据集似乎需要做很多工作...... SQLConncetion 一个 SQLCommand 一个 DataSet 一个 TableAdapter

...and worst of all, I had to type SQL code within quotes, with absolutely no help from the IDE to to keep me from making typing errors, or writing just plain wrong SQL code, and I had to know the columns names from my data table every time. ...最糟糕的是,我不得不在引号内输入 SQL 代码,而 IDE 绝对没有帮助,以防止我输入错误,或者写完全错误的 Z9778840A0100CB30C5B30C98287674B我的数据表每次。 Lots of room for errors, And query parameters.有很多错误空间和查询参数。 yuck.呸。

So, let me ask.... does the .NET developer community really work with SQLCmds, DataSets, and DataTables to read and write data?那么,让我问一下.... .NET 开发者社区真的使用 SQLCmds、DataSets 和 DataTables 来读写数据吗? Is that how it works?它是这样工作的吗?

I know all about the O/RM battles out there, and EF too.我知道所有关于 O/RM 之争的事情,还有 EF。

It sure looks like you can hook up any of the UI controls from ASP.NET/Silverlight/WPF/and WinForms to either Object collections (via OR/M) or DataSets/DataTables, right?看起来您可以将任何 UI 控件从 ASP.NET/Silverlight/WPF/和 WinForms 连接到 Object collections(通过 OR/M)或 DataSets/DataTables,对吧? Is it always a choice between one of those two?它总是在这两者之一之间进行选择吗?

So, it's decision time for me, but, I don't know that to choose.所以,现在是我做决定的时候了,但是,我不知道如何选择。 They all seem to work, but that darn DataSet thing just seems scary to me, but, somehow, it also seems to be commonly used.它们似乎都可以工作,但是那个该死的 DataSet 东西对我来说似乎很可怕,但是,不知何故,它似乎也很常用。

First off, the only way to get data to SilverLight is via WCF web services.首先,将数据获取到 SilverLight 的唯一方法是通过 WCF web 服务。 Direct database access is not allowed in Silverlight (you are running in a browser after all) Silverlight 中不允许直接访问数据库(毕竟您是在浏览器中运行)

Second: (I'll take flack for this) DataSets are evil.第二:(我会对此表示反对)数据集是邪恶的。 Direct spawn of the devil.直接生成恶魔。 Avoid at all costs, unless someone is holding a gun to your head.不惜一切代价避免,除非有人拿着枪指着你的头。 Problem one is performance: there isn't any.问题之一是性能:没有。 Problem two is data consumption: 1k automatically turns into 3k.问题二是数据消耗:1k自动变成3k。 Not a big issue for a desktop application, but huge for web applications.对于桌面应用程序来说不是什么大问题,但对于 web 应用程序来说却是个大问题。

Third: learn what it mean to create a domain object.第三:了解创建域 object 的含义。 From there you can use EF or NHibernate.从那里您可以使用 EF 或 NHibernate。 I prefer NHibernate, but there is a learning curve.我更喜欢 NHibernate,但有一个学习曲线。 Once you have a good domain object, you can then pass that to any client you listed.一旦你有一个好的域 object,你就可以将它传递给你列出的任何客户端。

I have used the classic old ADO.NET for many years, and you just get used to it.经典的老ADO.NET我用了很多年了,习惯就好了。 For a larger application, you can invest some time in creating a data layer once, then re-use it with many other business objects.对于更大的应用程序,您可以花一些时间创建一次数据层,然后将其与许多其他业务对象一起使用。

A couple of other options:其他几个选项:

  1. Strongly Typed datasets.强类型数据集。 You can create a strongly typed dataset that will allow you to drag and drop database objects and will generate a lot of the plumbing code for you.您可以创建一个强类型数据集,该数据集允许您拖放数据库对象并为您生成大量管道代码。

2 linq To Sql designer, which also allows you to drag and drop objects into a designer, and generates a dbml file that you use to connect and manipulate your data. 2 linq 到 Sql 设计器,它还允许您将对象拖放到设计器中,并生成用于连接和操作数据的 dbml 文件。

I have recently gotten onto the Linq bandwagon and it does simplify your life when treating data as objects.我最近加入了 Linq 潮流,当将数据视为对象时,它确实简化了您的生活。

Good luck!祝你好运!

There is nothing wrong with ADO.NET "Classic" (the DataSet/DataTable API), and on at least one occasion, I decided to use it rather than an ORM. ADO.NET “Classic”(DataSet/DataTable API)没有任何问题,至少有一次,我决定使用它而不是 ORM。 That being said, the fact that ADO.NET "Classic" is more often used is largely an artifact of history.话虽如此,ADO.NET“经典”更常用的事实在很大程度上是历史的产物。 For a long time, that was the only viable option, especially if you wanted to stick with a Microsoft-only solution.长期以来,这是唯一可行的选择,尤其是如果您想坚持使用仅限 Microsoft 的解决方案。

People who use Datasets actually use the IDE to create strongly typed datasets.使用数据集的人实际上使用 IDE 来创建强类型数据集。

Using this you have a visual query builder similar to SQL Management Studio's query builder.使用它,您将拥有一个类似于 SQL Management Studio 的查询构建器的可视化查询构建器。 This generates function to load/save etc, with parameters.这会生成 function 以加载/保存等,并带有参数。

The fact that you see datasets everywhere is largely due to the fact that it was the only solution available up to now, short of hand coding SQL everywhere.随处可见数据集的事实很大程度上是因为它是迄今为止唯一可用的解决方案,没有随处可见的手动编码 SQL。

ORMs are all the rage these days, and they do provide a really nice abstraction layer.如今,ORM 风靡一时,它们确实提供了一个非常好的抽象层。

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

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