简体   繁体   English

简单的数据访问层

[英]Simple Data Access Layer

Can anyone suggest a simple Data Access Layer (C# .NET)? 任何人都可以建议一个简单的数据访问层(C#.NET)? Not keen on using the Microsoft Application Data Access block, seems very bloated and overkill. 不热衷于使用Microsoft应用程序数据访问块,似乎非常臃肿和矫枉过正。 Also don't want to use LINQ to SQL for various reasons. 出于各种原因也不想使用LINQ to SQL。 I want to build upon this and create our own in-house ORM, again for various reasons. 我想以此为基础并再次出于各种原因创建我们自己的内部ORM。 In the past I've always had the Data Access Layer already built so was never involved in building.... 在过去,我总是已经建立了数据访问层,因此从未参与构建....

Other individuals and organizations have spent months or years developing their own ORMs and technologies -- many of which you listed -- and many are free to use. 其他个人和组织花了数月或数年时间开发自己的ORM和技术 - 其中许多是您列出的 - 并且许多可以免费使用。 You should devote your resources toward your core application logic instead of trying to build another whole beast called ORM. 您应该将资源投入到核心应用程序逻辑中,而不是尝试构建另一个名为ORM的整个野兽。 There are enough offerings out there to satisfy all kinds of applications. 有足够的产品来满足各种应用。

You said you've never been involved in building your down DAL. 你说你从来没有参与建立你的DAL。 It's a classic starting error to try to roll your own resulting ORM (as far as time and resources, not necessarily knowledge), for those reasons stated above. 由于上述原因,尝试推出自己产生的ORM(就时间和资源而言,不一定是知识)是一个典型的起始错误。 If you think some of the existing offerings seem overkill, just wait until you get into the intricacies of building your own. 如果您认为某些现有产品看起来有点过分,那就等到您进入构建自己的产品的复杂性之后。

However if you want to compete in the ORM market and that IS your product, then go right ahead. 但是,如果你想在ORM市场上竞争并且那是你的产品,那么就去吧。 That's my 2 cents. 那是我的2美分。

Edit: If you have concerns about being bound to a certain ORM product or offering in your project, you can simply hide away whatever software you choose behind an interface and swap in other ORMs as needed... 编辑:如果您担心在项目中绑定某个ORM产品或产品,您可以简单地隐藏您在界面后面选择的任何软件,并根据需要交换其他ORM ...

public interface IBusinessDataOperations {
   // hides any ORM of choice
}

And if someday you have some free cycles and want to build your own ORM, even after trying other products, you can just slip it in behind this interface too. 如果有一天你有一些自由周期并且想要建立自己的ORM,即使在尝试其他产品之后,你也可以将它放在这个界面后面。

Do you want to be able to use Linq? 你想能够使用Linq吗?

Do you want to use DataSets / DataTables? 您想使用DataSet / DataTables吗?

Do you want to use a code generator (of your own or someone else's)? 您想使用代码生成器(您自己或其他人的代码生成器)吗?

Do you want to use stored procedures? 你想使用存储过程吗?

Honestly, it's not that hard to grow your own DAL/ORM from scratch as long as you don't care for advanced features of any sort and you don't mind the mind numbing tedium of the process. 老实说,只要你不关心任何类型的高级功能并且你不介意过程令人麻木的麻烦,从头开始发展你自己的DAL / ORM就不那么难了。 You have to be a little crazy though. 你必须有点疯狂。 :) :)

I'm assuming you don't want to use anything like SubSonic, EntityFramework, or NHibernate as well, but correct me if that is a bad assumption. 我假设你不想使用像SubSonic,EntityFramework或NHibernate这样的东西,但如果这是一个不好的假设,请纠正我。

Here is a complete list: ORM tools ".Net" 这是一个完整的列表: ORM工具“.Net”

ADO.NET Entity Framework, Microsoft's ORM (released with .NET 3.5 SP1) ADO.NET实体框架,微软的ORM(随.NET 3.5 SP1发布)
Base One Foundation Component Library, free or commercial Base One基础组件库,免费或商业
BCSEi ORM Code Generator, free or commercial BCSEi ORM代码生成器,免费或商业
Business Logic Toolkit for .NET, open source 适用于.NET的Business Logic Toolkit,开源
Castle ActiveRecord, ActiveRecord for .NET, open source Castle ActiveRecord,ActiveRecord for .NET,开源
DataObjects.Net v4.0, open source, commercial DataObjects.Net v4.0,开源,商业
DevForce, commercial, N-Tier DevForce,商业,N-Tier
Developer Express, eXpress Persistent Objects (XPO) Developer Express,eXpress持久对象(XPO)
EntitySpaces, commercial EntitySpaces,商业
Euss, open source Euss,开源
Habanero, Free open source Habanero,免费开源
iBATIS, Free open source iBATIS,免费开源
Invist,free ORM and code generation tool Invist,免费的ORM和代码生成工具
LLBLGen, open source drivers, commercial LLBLGen,开源驱动程序,商业
LightSpeed, free or commercial LightSpeed,免费或商业
Neo, open source Neo,开源
NConstruct, commercial NConstruct,商业
NHibernate, open source NHibernate,开源
Opf3, free and commercial Opf3,免费和商业
ObjectMapper .NET, GPL and commercial license ObjectMapper .NET,GPL和商业许可证
OpenAccess, free or commercial TierDeveloper, free ORM and code generation tool OpenAccess,免费或商业TierDeveloper,免费ORM和代码生成工具
Persistor.NET, free or commercial Persistor.NET,免费或商业
Quick Objects, free or commercial 快速对象,免费或商业
Sooda, open source; Sooda,开源; BSD license BSD许可证
Subsonic, open source 亚音速,开源
Orasis, free trial or buy. Orasis,免费试用或购买。
Telerik, express or buy. Telerik,快递或购买。
CSLA.NET, free. CSLA.NET,免费。
ECO, free or commercial ECO,免费或商业
nhydrate, open source nhydrate,开源
.netTiers, open source .netTiers,开源
dapper-dot-net, open source dapper-dot-net,开源
codesmithtools plinqo, open source codesmithtools plinqo,开源

Have a look at SubSonic version 2.x if you need to avoid linq. 如果您需要避免使用linq,请查看SubSonic版本2.x. This is a simple/ lite ORM that you can build upon. 这是一个可以构建的简单/精简ORM。 Why roll your own when there is great code like this. 当有像这样的优秀代码时,为什么要自己动手。

Actually the simplest SQLHelper file that I've found has been the early 2000 era MS Data Access Application Block. 实际上,我发现的最简单的SQLHelper文件是早期的2000年代MS数据访问应用程序块。 After unpackaging and opening the solution you find that you have just one file called SQLHelper.cs which basically just wraps alot of the raw ADO that you would be writing with a home grown DAL. 在解压缩并打开解决方案之后,您会发现您只有一个名为SQLHelper.cs的文件,它基本上只包含了您将使用自行开发的DAL编写的大量原始ADO。 It is best practice code (at the time yes) and easily converts to .net 2.0 framework and above. 这是最佳实践代码(当时是)并且很容易转换为.net 2.0框架及以上。 After you place SQLHelper into your solution, then you can easily add in your own data access components to do your basic CRUD work, but you won't have to worry about the low level coding of opening a connection or a dataset or whatever. 将SQLHelper放入解决方案后,您可以轻松添加自己的数据访问组件来完成基本的CRUD工作,但您不必担心打开连接或数据集的低级编码等等。

I know what you're thinking, why suggest this because you said you didn't like the blocks. 我知道你在想什么,为什么要这样说,因为你说你不喜欢这些街区。 Well this one is the only i've found that doesn't use a provider factory and all of that extra code that you're saying is overkill. 那么这个是我发现的唯一一个没有使用提供商工厂的人,而你所说的所有额外代码都是矫枉过正的。 This block is only for SQL Server so do keep that in mind. 此块仅适用于SQL Server,因此请记住这一点。 But overall, you might find that this is a good starting point in your project. 但总的来说,您可能会发现这是您项目中的一个很好的起点。

Here is a download link to the block itself. 这是块本身的下载链接

Good luck, and hope this helps. 祝你好运,希望这能帮到你。

查看DbExtensions ,您可以使用它来简化DAL代码,或者您可以在顶部构建自己的ORM。

For very simple requirements (and I mean simple!) I'd create a DAL based on the repository pattern, where the sql querying is done in the repository methods and then simple POCO's ( Plain old CLR objects ) are created and returned. 对于非常简单的需求(我的意思是简单!)我将基于存储库模式创建一个DAL,其中sql查询在存储库方法中完成,然后创建并返回简单的POCO( 普通旧CLR对象 )。

You can call sprocs or parametized SQL if required. 如果需要,您可以调用sprocs或参数化SQL。 Then map the data to your poco (for this just use a standard SQLDataReader) 然后将数据映射到您的poco(为此只使用标准的SQLDataReader)

But to be honest the moment the queries get big or complex or there are lots of fields in your objects you're better off letting a proper DAL/ORM take the strain and concentrate on your application. 但说实话,当查询变得庞大或复杂或者对象中有很多字段时,最好让正确的DAL / ORM承担压力并专注于您的应用程序。

Before you can make your decisions there are some questions that you have to answer to yourself before begin, not just if you dont want linq or ms data access layer those are some of that questions: 在你做出决定之前,你有一些问题需要在开始之前回答自己,而不仅仅是你不想要linq或ms数据访问层那些问题:

  • Do you want to be able to modify the code (generator) or do you want a DAL that gives you the service of accesing the DB? 您是否希望能够修改代码(生成器),或者您是否希望DAL能够为您提供访问数据库的服务?
  • Do you want to deal with XML configuration files or do you prefer a DAL based on reflection? 您想要处理XML配置文件还是更喜欢基于反射的DAL?
  • Do you want support for multiple DBs or you can live with an specific one? 您是否希望支持多个数据库,或者您可以使用特定数据库?

Some years ago I had to build a DAL for the company I was working for and resulted that it was easier than I though, the result was an ORM based on reflection who supports many DBs at this time SqlServer, Oracle, PostgreSql and SqLite, I can share the code for you it may help you build your own or extend it to your needs. 几年前我不得不为我工作的公司建立一个DAL,结果是它比我更容易,结果是一个基于反射的ORM,支持许多数据库,此时SqlServer,Oracle,PostgreSql和SqLite,I可以为您分享代码,它可以帮助您建立自己的代码或扩展到您的需求。

If it helps you for anything build your own DAL (if you havee the time) its a great learning experience you will learn some Sql and several features of the language that you might never use again. 如果它可以帮助您构建自己的DAL(如果您有时间),那么您将学习一些Sql以及您可能永远不会再使用的语言的一些功能。

Good luck with that... 祝你好运......

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

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