简体   繁体   English

.Net中的数据库应用程序最佳实践

[英]Database applications best practices in .Net

Back in 2003 I started making web applications that used databases in PHP. 早在2003年,我就开始制作使用PHP数据库的Web应用程序。 Of course, I started off just putting SQL queries directly in my code, not using stored procedures, prepared statements or any kind of ORM framework. 当然,我开始只是将SQL查询直接放入代码中,而不使用存储过程,准备好的语句或任何类型的ORM框架。 But back then it was a common practice, as is evidenced by the code I'm working on today (a VB.Net web application built around that time). 但是那时候这是一种普遍的做法,正如我今天使用的代码所证明的(那段时间构建的VB.Net Web应用程序)。

The application uses about a dozen stored procedures, some prepared statements and lots of SQL queries directly in the code. 该应用程序直接在代码中使用了大约十二个存储过程,一些准备好的语句和许多SQL查询。 Generally, there are redundant blocks of code that get the connection, run the SQL query (a string), then use a reader to extract data using hardcoded field names. 通常,有冗余的代码块可以建立连接,运行SQL查询(字符串),然后使用读取器使用硬编码的字段名称提取数据。 It's 2011 now, writing this kind of code was easy and therefore popular in 2003 but I'm guessing it's not the best practice. 现在是2011年,编写这种代码很容易,因此在2003年很流行,但我想这不是最佳实践。 It's not secure or easy to maintain, and I haven't touched the stored procedures. 它既不安全也不容易维护,而且我还没有涉及存储过程。

I want to move beyond hardcoding sql queries in applications and manually parsing data from responses. 我想超越对应用程序中的sql查询进行硬编码,并手动解析响应中的数据。 What are the current best practices and tools for creating database applications in .Net? 在.Net中创建数据库应用程序的当前最佳实践和工具是什么? I've heard of Linq2SQL, Entity Framework, NHibernate and some other technologies, but I don't know when they should be used or if they are still current. 我听说过Linq2SQL,实体框架,NHibernate和其他一些技术,但是我不知道什么时候应该使用它们或者它们是否仍然是最新的。

I'm using Visual Studio 2010, VB/C# and SQL Server 2008 Express 我正在使用Visual Studio 2010,VB / C#和SQL Server 2008 Express

Edit: It looks like most of you are discussing ORM software. 编辑:似乎大多数人都在讨论ORM软件。 This sounds like what I want; 这听起来像我想要的; we have several tables with a lot of information that could be accessed as an object. 我们有几个表,其中包含很多可以作为对象访问的信息。 When should I instead use a standard query or prepared statement? 什么时候应该改用标准查询或预备语句?

Entity Framework and NHibernate are both what are called Object / Relational Mappers. 实体框架和NHibernate都称为对象/关系映射器。 They both generate a layer that bridges the gap between objects and databases (sometimes called Object Relational Impedance Mismatch ). 它们都生成一个弥合对象与数据库之间的鸿沟的层(有时称为“ 对象关系阻抗不匹配” )。

I cannot recommend using an OR/M enough. 我不建议使用足够的OR / M。 Either one is good; 哪一个是好的? NHibernate has a bit of an edge in security, but it does seem to have a steeper learning curve. NHibernate在安全性方面有优势,但是它的学习曲线似乎更为陡峭。 I personally use Entity Framework, but it truly is just a matter of preference. 我个人使用实体框架,但这确实只是一个优先事项。

Both of the frameworks have a provision for when you just can't get done what you need to get done with the tool, and you can call back in and execute a stored procedure directly, so you're safe in that regard. 这两个框架都为您提供了何时无法完成使用该工具所需完成的准备工作,并且您可以回调并直接执行存储过程,因此在这方面是安全的。

LINQ2SQL is a more database oriented technology, but it qualifies as an OR/M. LINQ2SQL是一种面向数据库的技术,但它具有OR / M的资格。 I would skip straight to EF. 我会直接跳到EF。 EF was part of .Net 3.5 SP1, found here . EF是.Net 3.5 SP1的一部分,可在此处找到。

Here's an example of some code to retrieve a customer from a database using EF: 这是一些使用EF从数据库检索客户的代码示例:

using (var ctx = new DBEntities())
{
     var employee = (from e in ctx.Employees
            where e.User.UserName == userName
            select e).FirstOrDefault();
}

Another reason to use an OR/M, in my opinion, is that they tend to promote a design view of your code to be a first class artifact. 我认为,使用OR / M的另一个原因是,它们倾向于将代码的设计视图提升为一流的工件。 What I mean by this is that you end up with a nice graphical view of the relationships between your entities (because the editor is drag and drop) that you won't abandon when you end up short on time. 我的意思是,您最终得到了实体之间关系的漂亮图形视图(因为编辑器是拖放式的),而您在短时间结束时不会放弃。

Here's MS's link on getting started: http://msdn.microsoft.com/en-us/library/bb386876.aspx . 这是MS的入门链接: http : //msdn.microsoft.com/zh-cn/library/bb386876.aspx

HTH. HTH。

Try LLBLGen: http://www.llblgen.com/defaultgeneric.aspx 尝试LLBLGen: http ://www.llblgen.com/defaultgeneric.aspx

They have a simple UI that allows you to point to your DB and instantly have all of your DAL generated for you in minutes. 它们具有简单的UI,可让您指向数据库,并在几分钟之内立即为您生成所有DAL。 Setup and integration was far easier than any of the other ORM products I saw. 安装和集成比我看到的任何其他ORM产品都容易得多。 It is customizable as well, but I still haven't done much with customization yet. 它也是可定制的,但是我仍然没有做太多定制。 Need to find out more info there myself. 需要自己找到更多信息。

Your hunch to use object relational mapping (ORM) technologies is a good one; 您有使用对象关系映射(ORM)技术的直觉。 there are frameworks (as you've mentioned) which have support behind them which make interacting with data in a dynamic way (say, without stored procedures, query generation on the fly) and a non-dynamic way (stored procs, etc) easy. 有一些框架(如您所述)在其后方具有支持,这些框架使以动态方式(例如,无需存储过程,即时生成查询)和非动态方式(存储过程等)与数据交互变得容易。

You covered the current landscape of ORM technologies, here is a breakdown of some of the differences of each. 您介绍了ORM技术的当前情况,下面是每种技术的一些区别的分解。

NHibernate NHibernate的

An open source project which is based off the Java Hibernate project. 一个基于Java Hibernate项目的开源项目。 It is very active, and has great support for a number of scenarios. 它非常活跃,并且在许多情况下都具有强大的支持。

LINQ to SQL LINQ转SQL

While not officially dead, it's been publically stated that the focus for development will not be in LINQ-to-SQL. 尽管尚未正式死亡,但已公开声明开发重点不会放在LINQ-to-SQL中。 That effort is going to be directed at LINQ-to-Entities. 这项工作将针对LINQ到实体。 While it's generally a better idea to work with LINQ to Entities (if choosing between this and that), there are issues; 与LINQ to Entities合作通常是一个更好的主意(如果在两者之间进行选择),但存在一些问题。 model generation is not as clean as some would like, the metadata mapping model that they use doesn't allow you to use models from different designers easily , and other issues that NHibernate has probably overcome a long time ago. 模型生成并不像某些人想像的那样干净,他们使用的元数据映射模型不允许您轻松地使用来自不同设计师的模型 ,以及NHibernate很久以前就已经解决的其他问题。

LINQ to Entities LINQ到实体

Currently the preferred method of data access in .NET (as evidienced by not only designer support and integration in VS.NET, but in frameworks such as RIA), this is what MS is going to devote energy into when dealing with the ORM space. 目前,.NET中首选的数据访问方法(不仅是VS.NET中的设计人员支持和集成,而且在RIA等框架中也是如此),这就是MS在处理ORM空间时将投入的精力。 It's currently in its second major iteration (the first being found difficult to use by a number of people), the ease-of-use has definitely increased. 它目前处于第二次主要迭代中(第一次发现很难被许多人使用),易用性肯定有所提高。

I'd also look for future integrations with other MS technologies. 我还将寻找将来与其他MS技术的集成。


Regardless, all three technologies will allow you to expose your stored procedures in a way that will return object models to you instead of data sets that you have parse/use string lookups for. 无论如何,所有这三种技术都将允许您以一种将对象模型返回给您而不是为之解析/使用字符串查找的数据集的方式公开存储过程。

They will also handle the case where you have to round trip the data back into the database; 他们还将处理您必须将数据往返返回数据库的情况。 they just each go about it in different ways. 他们只是各自以不同的方式去做。

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

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