C#:除了数据集你还用什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: What Else Do You Use Besides DataSet
提问by saalon
I've found myself increasingly unsatisfied with the DataSet/DataTable/DataRow paradigm in .Net, mostly because it's often a couple of steps more complicated than what I really want to do. In cases where I'm binding to controls, DataSets are fine. But in other cases, there seems to be a fair amount of mental overhead.
我发现自己对 .Net 中的 DataSet/DataTable/DataRow 范式越来越不满意,主要是因为它通常比我真正想做的要复杂几个步骤。在我绑定到控件的情况下,DataSet 很好。但在其他情况下,似乎有相当数量的精神开销。
I've played a bit with SqlDataReader, and that seems to be good for simple jaunts through a select, but I feel like there may be some other models lurking in .Net that are useful to learn more about. I feel like all of the help I find on this just uses DataSet by default. Maybe that and DataReader really are the best options.
我玩过 SqlDataReader,这对于通过选择进行的简单短途旅行似乎很有用,但我觉得 .Net 中可能还有其他一些模型可以用来了解更多信息。我觉得我在这方面找到的所有帮助都默认使用 DataSet。也许那和 DataReader 真的是最好的选择。
I'm not looking for a best/worst breakdown, just curious what my options are and what experiences you've had with them. Thanks!
我不是在寻找最好/最差的故障,只是想知道我的选择是什么以及你对他们有什么经验。谢谢!
-Eric Sipple
-埃里克·西普尔
采纳答案by TheSmurf
Since .NET 3.5 came out, I've exclusively used LINQ. It's really that good; I don't see any reason to use any of those old crutches any more.
自从 .NET 3.5 出来后,我就专门使用 LINQ。真的那么好;我看不出有什么理由再使用那些旧拐杖了。
As great as LINQ is, though, I think any ORM system would allow you to do away with that dreck.
尽管 LINQ 很棒,但我认为任何 ORM 系统都可以让您摆脱这种麻烦。
回答by hometoast
We've moved away from datasets and built our own ORM objects loosely based on CSLA. You can get the same job done with either a DataSet or LINQ or ORM but re-using it is (we've found) a lot easier. 'Less code make more happy'.
我们已经摆脱了数据集,而是基于CSLA松散地构建了我们自己的 ORM 对象。您可以使用 DataSet 或 LINQ 或 ORM 完成相同的工作,但(我们发现)重用它要容易得多。'更少的代码让更多的快乐'。
回答by Eric Z Beard
I use them extensively but I don't make use of any of the "advanced" features that Microsoft was really pushing when the framework first came out. I'm basically just using them as Lists of Hashtables, which I find perfectly useful.
我广泛地使用它们,但我没有使用微软在框架首次出现时真正推动的任何“高级”功能。我基本上只是将它们用作哈希表列表,我觉得这非常有用。
I have not seen good results when people have tried to make complex typed DataSets, or tried to actually set up the foreign key relationships between tables with DataSets.
当人们尝试制作复杂类型的 DataSet 或尝试实际设置带有 DataSet 的表之间的外键关系时,我没有看到好的结果。
Of course, I am one of the weird ones that actually prefers a DataRow to an entity object instance.
当然,我是那些实际上更喜欢 DataRow 而不是实体对象实例的怪人之一。
回答by JamesSugrue
Pre linq I used DataReader to fill List of my own custom domain objects, but post linq I have been using L2S to fill L2S entities, or L2S to fill domain objects.
在 linq 之前,我使用 DataReader 来填充我自己的自定义域对象列表,但是在 linq 之后,我一直使用 L2S 来填充 L2S 实体,或使用 L2S 来填充域对象。
Once I get a bit more time to investigate I suspect that Entity Framework objects will be my new favourite solution!
一旦我有更多时间进行调查,我怀疑 Entity Framework 对象将成为我最喜欢的新解决方案!
回答by Keith
I was fed up with DataSets in .Net 1.1, at least they optimised it so that it doesn't slow as exponentially for large sets any more.
我厌倦了 .Net 1.1 中的 DataSets,至少他们优化了它,以便它不再像大集合那样以指数方式慢下来。
It was always a rather bloated model - I haven't seen many apps that use most of its features.
它一直是一个相当臃肿的模型——我没有看到很多应用程序使用了它的大部分功能。
SqlDataReader was good, but I used to wrap it in an IEnumerable<T>
where the T was some typed representation of my data row.
SqlDataReader 很好,但我曾经将它包装在一个IEnumerable<T>
T 是我的数据行的某种类型化表示中。
Linq is a far better replacement in my opinion.
在我看来,Linq 是一个更好的替代品。
回答by Mike
I just build my business objects from scratch, and almost never use the DataTable and especially not the DataSet anymore, except to initially populate the business objects. The advantages to building your own are testability, type safety and intellisense, extensibility (try adding to a DataSet) and readability (unless you enjoy reading things like Convert.ToDecimal(dt.Rows[i]["blah"].ToString())).
我只是从头开始构建我的业务对象,几乎不再使用 DataTable,尤其是不再使用 DataSet,除了最初填充业务对象。构建自己的优点是可测试性、类型安全和智能感知、可扩展性(尝试添加到数据集)和可读性(除非你喜欢阅读 Convert.ToDecimal(dt.Rows[i]["blah"].ToString() 之类的东西) ))。
If I were smarter I'd also use an ORM and 3rd party DI framework, but just haven't yet felt the need for those. I'm doing lots of smaller size projects or additions to larger projects.
如果我更聪明,我也会使用 ORM 和 3rd 方 DI 框架,但只是还没有感觉到需要这些。我正在做许多较小规模的项目或对较大项目的补充。
回答by Brian Leahy
DataSets are great for demos.
数据集非常适合演示。
I wouldn't know what to do with one if you made me use it.
如果你让我使用它,我不知道该怎么办。
I use ObservableCollection
我使用 ObservableCollection
Then again i'm in the client app space, WPF and Silverlight. So passing a dataset or datatable through a service is ... gross.
然后我再次进入客户端应用程序空间,WPF 和 Silverlight。因此,通过服务传递数据集或数据表是......很糟糕。
DataReaders are fast, since they are a forward only stream of the result set.
DataReader 速度很快,因为它们是结果集的前向流。
回答by bakasan
Selecting a modern, stable, and actively supported ORM tool has to be probably the single biggest boost to productivity just about any project of moderate size and complexity can get. If you're concluding that you absolutely, absolutely, absolutely have to write your own DAL and ORM, you're probably doing it wrong (or you're using the world's most obscure database).
选择一个现代的、稳定的、积极支持的 ORM 工具可能是对任何中等规模和复杂性的项目所能获得的生产力的最大提升。如果您得出结论,您绝对、绝对、绝对必须编写自己的 DAL 和 ORM,那么您可能做错了(或者您正在使用世界上最晦涩的数据库)。
If you're doing raw datasets and rows and what not, spend the day to try an ORM and you'll be amazed at how much more productive you can be w/o all the drudgery of mapping columns to fields or all the time filling Sql command objects and all the other hoop jumping we all once went through.
如果您正在处理原始数据集和行,而没有,请花一天时间尝试 ORM,您会惊讶地发现,如果没有将列映射到字段的所有苦差事或所有时间填充,您的工作效率会提高多少Sql 命令对象和我们都曾经经历过的所有其他跳环。
I love me some Subsonic, though for smaller scale projects along with demos/prototypes, I find Linq to Sql pretty damn useful too. I hate EF with a passion though. :P
我喜欢一些 Subsonic,但对于较小规模的项目以及演示/原型,我发现 Linq to Sql 也非常有用。不过,我满怀热情地讨厌 EF。:P
回答by Jesse Taber
I've been using the Data Transfer Objectspattern (originally from the Java world, I believe), with a SqDataReader to populate collections of DTOs from the data layer for use in other layers of the application. The DTOs themselves are very lightweight and simple classes composed of properties with gets/sets. They can be easily serialized/deserialized, and used for databinding, making them pretty well suited to most of my development needs.
我一直在使用数据传输对象模式(我相信最初来自 Java 世界)和 SqDataReader 来填充数据层的 DTO 集合,以便在应用程序的其他层中使用。DTO 本身是非常轻量级和简单的类,由带有获取/设置的属性组成。它们可以很容易地序列化/反序列化,并用于数据绑定,使它们非常适合我的大多数开发需求。
回答by fuzzbone
I NEVER use datasets. They are big heavyweight objects only usable (as someone pointed out here) for "demoware". There are lot's of great alternatives shown here.
我从不使用数据集。它们是仅可用于“演示软件”的大型重量级对象(正如有人在这里指出的那样)。这里展示了很多很棒的选择。