Application Architecture Guide 2.0 学习笔记(一)前言 - DataAccess Layer

来源:互联网 发布:linux 串口登陆 编辑:程序博客网 时间:2024/05/21 05:23

 Data Access Layer

The following guidelines will help you to understand the fundamental factors you need to onsider when designing the data layer for your application. Use these guidelines as a starting oint toward  understanding the key concerns, such as how to design your data access layer(如何设计数据访问层), how to manage connections(如何管理数据连接), the differences between stored procedures and dynamic SQL(动态SQL与存储过程之间的区别), how to think about data access performance(如何考虑数据访问性能), and how to effectively pass data through your application’s layers and tiers(如何在层之间高效传递数据).


• How to design your data access layer(如何设计数据访问层)


If you choose to access tables directly from your application without an intermediate data access layer, you may improve the performance of your application at the expense of maintainability(如果你选择在你的应用中直接存取数据表,你可能会得到一些性能上的提升,但是是以可维护性为代价). The data access logic layer provides a level of abstraction from the underlying data store(数据访问层为下层的数据仓库提供了一个抽象层). A well-designed data access layer exposes data and functionality based on how the data is used(一个设计很好的数据访问层 会 基于数据如何使用 公开数据和功能), and abstracts the underlying data store complexity. Do not arbitrarily map objects to tables and columns, and avoid deep object hierarchies(不要随意将对象映射到表和列,并要避免深的对象层次). For example, if you want to display a subset of data, and your design retrieves an entire object graph instead of the necessary portions, there is unnecessary object creation overhead(例如:如果你想要显示数据的一个子集,而你的设计却获取了整个数据表而不是必要的部分,这里就有不惜要的对象创建开销). Evaluate the data you need and how you want to use the data against your underlying data store().


For more information, see Chapter 12, “Data Access Layer Guidelines.”


• How to design your connection-management approach(如何设计链接管理)


Pool connections(使用连接池). Connections are an expensive and scarce resource(数据连接是一种昂贵且缺乏的资源), which should be shared between callers by using connection pooling(它们应该使用连接池以被调用者共享). Opening a connection for each caller limits scalability(为每一个调用者打开一个连接限制了可扩展性). Connect by using service accounts associated with a trusted subsystem security model(). Open database connections only when you need them(只有在需要的时候才打开数据库连接). Close the database connections as soon as you are finished—do not open them early, and do not hold them open across calls(一旦使用完毕立即关闭数据库连接,不要打开太早,也不要在整个调用中保持连接被打开).


For more information, see Chapter 12, “Data Access Layer Guidelines.”


• How to choose between stored procedures and dynamic SQL(如何选择存储过程与动态SQL)


When choosing between stored procedure and dynamic SQL, you need to consider the abstraction requirements(抽象需求), maintainability(可维护性), and any environment constraints(环境约束). Consider whether you need to implement abstraction in the database in the form of stored procedures or in the data layer in the form of dynamic SQL data access patterns or object/relational mapping (O/RM)(考虑是否需要以存储过程的形式实现数据库抽象,或者以动态SQL的形式). If you have a small application with limited business rules, consider using dynamic SQL as it is the simplest model with the least development overhead(如果你有一个小应用,只有有限的业务规则,考虑使用动态SQL,因为它是一种最简单的模型,只需要最小的开发开销). If you are building a larger application with maintainability requirements,consider using stored procedures since most changes to the database schema will have a minimal impact on application code(如果是大型应用,有可维护性要求,考虑使用存储过程,因为大多数对于数据库架构的变化 对于应用程序的代码只有很小的影响). For security considerations, you should always use typed parameters, either passed to a stored procedure or used when creating dynamic SQL(为安全考虑,在给存储过程或动态SQL任意一者传递参数的时候都要使用强类型参数。【减少SQL注入攻击】).


For an in-depth discussion on how to make an informed choice, see Chapter 12, “Data Access Layer Guidelines.”

 

• How to improve data access performance(如何提高数据访问性能)


Minimize processing on the server and at the client(最小化服务器与客户端的处理). Minimize the amount of data passed over the network(最小化在网络上传递的数据量). Use database connection pooling to share connections across requests(使用数据库连接池在数据请求间共享数据连接). Keep transactions as short as possible to minimize lock durations and to improve concurrency(保持事务尽可能的短,来最小化锁定时间并提高并发能力). However, do not make transactions so short that access to the database becomes too chatty(然而,也不要让事务太短,这样访问数据库会太频繁?。).


For more information, see Chapter 12, “Data Access Layer Guidelines.”


• How to pass data across layers and tiers(如何在层之间传递数据)


Consider scalar values when the consumer is interested only in the data and not the type or structure of the entity. Do not consider scalar values if your design is not capable of handing schema changes. Consider XML strings when you must support a variety of callers, including third-party clients. Consider custom objects when you must handle complex data, communicate with components that know about the object type, or require better performance through binary serialization. Consider using Data Transfer Objects (DTOs) that combine multiple data structures into a single structure in order to reduce round trips between layers and tiers. With the Microsoft .NET Framework, do not pass DataReader objects between layers because they require an open connection. Consider using DataSets for disconnected scenarios in simple CRUD (Create, Read, Update, Delete)-based applications. A DataSet contains schema information and maintains a change record, which means it can be modified and used to update the database without requiring additional code. However, it is important to understand that DataSets are expensive to create and serialize compared to custom objects.


For more information, see Chapter 12, “Data Access Layer Guidelines.”

 

原创粉丝点击