简体   繁体   English

C#:除了DataSet之外你还能使用什么?

[英]C#: What Else Do You Use Besides DataSet

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. 我发现自己对.Net中的DataSet / DataTable / DataRow范例越来越不满意,主要是因为它通常比我真正想做的事情复杂几步。 In cases where I'm binding to controls, DataSets are fine. 在我绑定到控件的情况下,DataSet也没问题。 But in other cases, there seems to be a fair amount of mental overhead. 但在其他情况下,似乎有相当数量的心理开销。

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. 我已经玩了一些SqlDataReader,这对于通过选择的简单短途旅行似乎很好,但我觉得可能有一些潜伏在.Net中的其他模型对于了解更多信息很有用。 I feel like all of the help I find on this just uses DataSet by default. 我觉得我发现的所有帮助都默认使用DataSet。 Maybe that and DataReader really are the best options. 也许这和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 -Eric Sipple

Since .NET 3.5 came out, I've exclusively used LINQ. 自.NET 3.5问世以来,我一直使用LINQ。 It's really that good; 这真的很棒; I don't see any reason to use any of those old crutches any more. 我认为没有任何理由再使用这些旧拐杖了。

As great as LINQ is, though, I think any ORM system would allow you to do away with that dreck. 然而,就像LINQ一样,我认为任何ORM系统都可以让你消除那个残骸。

We've moved away from datasets and built our own ORM objects loosely based on CSLA . 我们已经远离数据集并基于CSLA松散地构建了我们自己的ORM对象。 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. 您可以使用DataSet或LINQ或ORM完成相同的工作,但重新使用它(我们已经发现)更容易。 'Less code make more happy'. “减少代码让人更开心”。

I'm a huge fan of SubSonic . 我是SubSonic的忠实粉丝。 A well-written batch/CMD file can generate an entire object model for your database in minutes; 一个编写良好的批处理/ CMD文件可以在几分钟内为您的数据库生成一个完整的对象模型; you can compile it into its own DLL and use it as needed. 您可以将其编译为自己的DLL并根据需要使用它。 Wonderful model, wonderful tool. 精彩的模型,精彩的工具。 The site makes it sound like an ASP.NET deal, but generally speaking it works wonderfully just about anywhere if you're not trying to use its UI framework (which I'm moderately disappointed in) or its application-level auto-generation tools. 该网站使它听起来像是一个ASP.NET交易,但一般来说,如果你不试图使用它的UI框架(我感到非常失望)或它的应用程序级自动生成工具,它几乎可以在任何地方工作。 。

For the record, here is a version of the command I use to work with it (so that you don't have to fight it too hard initially): 为了记录,这是我用来处理它的命令的一个版本(这样你最初不必太努力):

sonic.exe generate /server [servername] /db [dbname] /out [outputPathForCSfiles] /generatedNamespace [myNamespace] /useSPs true /removeUnderscores true

That does it every time ... Then build the DLL off that directory -- this is part of an NAnt project, fired off by CruiseControl.NET -- and away we go. 每次都这样做...然后在该目录下构建DLL - 这是一个NAnt项目的一部分,由CruiseControl.NET发射 - 然后我们走了。 I'm using that in WinForms, ASP.NET, even some command-line utils. 我在WinForms,ASP.NET,甚至一些命令行工具中使用它。 This generates the fewest dependencies and the greatest "portability" (between related projects, EG). 这产生了最少的依赖性和最大的“可移植性”(在相关项目之间,EG)。

Note 注意

The above is now well over a year old. 以上现在已经超过一年了。 While I still hold great fondness in my heart for SubSonic, I have moved on to LINQ-to-SQL when I have the luxury of working in .NET 3.5. 虽然我仍然非常喜欢SubSonic,但是当我在.NET 3.5中工作时,我已经转向使用LINQ-to-SQL。 In .NET 2.0, I still use SubSonic. 在.NET 2.0中,我仍然使用SubSonic。 So my new official advice is platform version-dependent. 所以我的新官方建议依赖于平台版本。 In case of .NET 3+, go with the accepted answer. 对于.NET 3+,请使用已接受的答案。 In case of .NET 2.0, go with SubSonic. 对于.NET 2.0,请使用SubSonic。

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中的DataSet,至少他们对它进行了优化,使得它不再以大型集合的指数速度减速。

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是一个更好的替代品。

I've been using the Data Transfer Objects pattern (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. 我一直在使用数据传输对象模式(最初来自Java世界,我相信),使用SqDataReader从数据层填充DTO集合,以便在应用程序的其他层中使用。 The DTOs themselves are very lightweight and simple classes composed of properties with gets/sets. DTO本身是非常轻量级的简单类,由带有gets / sets的属性组成。 They can be easily serialized/deserialized, and used for databinding, making them pretty well suited to most of my development needs. 它们可以很容易地序列化/反序列化,并用于数据绑定,使它们非常适合我的大多数开发需求。

DataSets are great for demos. DataSet非常适合演示。

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. 然后我再次进入客户端应用空间,WPF和Silverlight。 So passing a dataset or datatable through a service is ... gross. 因此,通过服务传递数据集或数据表是......粗略的。

DataReaders are fast, since they are a forward only stream of the result set. DataReader很快,因为它们只是结果集的前向流。

I have used typed and untyped DataSets, DataViewManagers, DataViews, DataTables, DataRows, DataRowViews, and just about anything you can do with the stack since it firsts came out in multiple enterprise projects. 我使用了类型化和非类型化的DataSet,DataViewManagers,DataViews,DataTables,DataRows,DataRowViews,以及几乎可以对堆栈做的任何事情,因为它在多个企业项目中首次出现。 It took me awhile to get used to how allow of it worked. 我花了一段时间才习惯它是如何工作的。 I have written custom components that leverage the stack as ADO.NETdid not quite give me what I really needed. 我编写了利用堆栈的自定义组件,因为ADO.NETdid并不能完全满足我的需求。 One such component compares DataSets and then updates backend stores. 一个这样的组件比较DataSet,然后更新后端存储。 I really know how all of these items work well and those that have seen what I have done are very impressed that I managed to get beyond there feel that it was only useful for demo use. 我真的知道所有这些项目是如何运作良好的,那些看到我所做的事情给我留下了深刻的印象,我设法超越那里感觉它只对演示使用有用。

I use ADO.NET binding in Winforms and I also use the code in console apps. 我在Winforms中使用ADO.NET绑定,我也在控制台应用程序中使用该代码。 I most recently have teamed with another developer to create a custom ORM that we used against a crazy datamodel that we were given from contractors that looked nothing like our normal data stores. 我最近与另一个开发人员合作创建了一个自定义ORM,我们用它来对付一个疯狂的数据模型,我们从承包商那里看到的东西看起来就像我们的普通数据存储。

I searched today for replacement to ADO.NET and I do not see anything that I should seriously try to learn to replace what I currently use. 我今天搜索了替换ADO.NET,我没有看到任何我应该认真学习替换我目前使用的东西。

I've used typed DataSets for several projects. 我已经为几个项目使用了类型化的DataSet。 They model the database well, enforce constraints on the client side, and in general are a solid data access technology, especially with the changes in .NET 2.0 with TableAdapters. 他们很好地建模数据库,在客户端强制执行约束,并且通常是一种可靠的数据访问技术,特别是使用TableAdapter的.NET 2.0中的更改。

Typed DataSets get a bad rap from people who like to use emotive words like "bloated" to describe them. 类型化的数据集从那些喜欢用“臃肿”这样的情感词来描述它们的人那里得到了糟糕的说唱。 I'll grant that I like using a good O/R mapper more than using DataSets; 我会批准我喜欢使用好的O / R映射器而不是使用DataSet; it just "feels" better to use objects and collections instead of typed DataTables, DataRows, etc. But what I've found is that if for whatever reason you can't or don't want to use an O/R mapper, typed DataSets are a good solid choice that are easy enough to use and will get you 90% of the benefits of an O/R mapper. 它只是“感觉”更好地使用对象和集合而不是类型化DataTables,DataRows等。但我发现,如果由于某种原因你不能或不想使用O / R映射器,键入DataSet是一个很好的可靠选择,易于使用,可以获得O / R映射器的90%的好处。

EDIT: 编辑:

Some here suggest that DataReaders are the "fast" alternative. 有人建议DataReader是“快速”替代品。 But if you use Reflector to look at the internals of a DataAdapter (which DataTables are filled by), you'll see that it uses...a DataReader. 但是如果你使用Reflector来查看DataAdapter的内部(DataTables被填充),你会发现它使用了......一个DataReader。 Typed DataSets may have a larger memory footprint than other options, but I've yet to see the application where this makes a tangible difference. 类型化的DataSet 可能比其他选项具有更大的内存占用,但我还没有看到应用程序在哪里产生了实质性的差异。

Use the best tool for the job. 使用最好的工具来完成工作。 Don't make your decision on the basis of emotive words like "gross" or "bloated" which have no factual basis. 不要根据没有事实基础的“粗暴”或“臃肿”等情绪化词语作出决定。

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. 我基本上只是将它们用作Hashtables列表,我发现它非常有用。

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实际设置表之间的外键关系时,我没有看到好的结果。

Of course, I am one of the weird ones that actually prefers a DataRow to an entity object instance. 当然,我是一个奇怪的人,实际上更喜欢DataRow到实体对象实例。

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. Pre 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对象将成为我最喜欢的解决方案!

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. 选择一个现代的,稳定的,积极支持的ORM工具可能是生产力的最大提升,几乎任何中等规模和复杂性的项目都可以获得。 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). 如果你的结论是绝对的,绝对必须编写自己的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. 我爱我一些Subsonic,虽然对于小型项目以及演示/原型,我发现Linq对Sql非常有用。 I hate EF with a passion though. 虽然我很讨厌EF。 :P :P

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. 我只是从头开始构建我的业务对象,几乎从不使用DataTable,特别是不再使用DataSet,除了最初填充业务对象。 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())). 构建自己的优点是可测试性,类型安全性和智能感知,可扩展性(尝试添加到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. 如果我更聪明,我也会使用ORM和第三方DI框架,但还没有感觉到需要。 I'm doing lots of smaller size projects or additions to larger projects. 我正在做大量规模较小的项目或大型项目的补充。

I NEVER use datasets. 我永远不会使用数据集。 They are big heavyweight objects only usable (as someone pointed out here) for "demoware". 它们是大型重量级物体,只能用于“demoware”(如此处有人指出)。 There are lot's of great alternatives shown here. 这里展示了很多很好的选择。

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

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