Implement data access logic components
The data access logic component is a stateless class that means that all message exchange is independently interpreted. There is no status in the call to be held. Data Acquisition logic component provides a single database single table or relational table, or in some cases, multiple databases are separated by a horizontal database. Typically, the method of data access logic components calls the stored procedure to perform their operations.
A key purpose of the data access logic component is to hide the format of the database characteristic in the call to the call. Data Acquisition logic components provide packaged data access services to applications. Especially need to be pointed out that the following details are implemented on the data access logic component processing:
· Management and packaging locks
· Appropriate processing security and authorization issues
· Appropriate handling transaction
· Execute data memory page scheduling
· Execute data dependencies in the case of the need
· Data without business, proper implementation of caching strategy
· Perform data stream and data serialization
The next chapter will study these problems more specific
Application scenario using data access logic components
Implement data access logic components
Data Acquisition logic components use ADO.NET to perform SQL statements or call stored procedures. For an example of a data access logic component, see how the appendix defines a data access logic component.
If your application contains multiple data access logic components, you can use data access Helper to simplify implementing data access logic components. This component can help manage database connections, perform SQL commands, and cache parameters. Data Acquisition Logic components still need to encapsulate logic to access specific business data, although data access Helper components focus on data access API and data connection configurations to reduce duplication code. Microsoft provides data access application blocks for .NET: http://msdn.microsoft.com/architecture/application/default.aspx? Pull = / library / en-us / dnbda / html / daab-rm.asp When you Using the Microsoft SQL Server database is that the tool can be used to generate data access Helper components. Figure 6 shows how to use data access Helper components to help implement data access logic components.
Figure 5 illustrates how a data access logic component is called by different application types, including Windows Forms Applications, ASP.NET Application, XML Web Services, and business processing. These calls may be local or remote, mainly dependent on how to configure your app. If this has multiple data access logic components, you can define a base class to integrate and extend.
The interface provided for different customer types when designing data access logic components. If the design of data access logic components and current requirements and potential business processing layers are compatible, they can reduce the number of interfaces, appearances, or mapping layers that must be implemented.
To support various business processes and applications, the following techniques are required to circulate data and data access logic component methods.
• Put the traffic of the business entity in the data access logic component method. You can circulate data in different format: serialized scalar values, XML strings, dataset, or custom business entity classes.
· Returns the business entity by the data access logic component method. Similarly You can return data in different format: You can circulate data in different format: serialized scalar, XML string, dataset, or custom business entity class or Data Reader.
Next chapter describes the advantages and disadvantages of each option. This information will help you make a choice based on your application scenario. By scheduling value as input and output
This choice has the following advantages
• Abstract caller must know the data defined to be a business entity, rather than the specified type or a specified business entity structure.
· Serialized scalar value of primary support serialization.
· Effective use of memory scalar values only transmit data in the case of need.
· Performance When processing instance data, the scalar value can have other performance.
Next is the disadvantage:
· Tight coupling and maintenance. SCHEMA needs to change after changing.
• Entity Collection Data Acquisition Logic Components Save or update multiple entities, and must be called by separation. This will cause major performance strikes in a distributed environment.
Support optimistic concurrency. To support optimism, the timestamp field must be defined in the database and as part of the data.
As input and output via XML string
This option is a bit:
· Song coupled caller must know the defined business entity, and schema provides metadata for business entities
· Integrated Accepting XML has a variety of ways, such as .NET applications, BizTalk rules, and third-party business rules.
· Business entity set an XML string can contain multiple business entities
· Serialized serialized support serialization
Disadvantages of this option
• Analysis of XML strings The XML string must be parsed when receiving. Very large XML string analysis can trigger performance costs.
· Memory uses inefficient XML strings to be lengthy, will trigger inefficient memory usage
· Support optimistic concurrency in order to support optimism, and the timestamp field must be defined in the database and as part of XML data.
As input and output via DataSets
The advantages are as follows:
· Native function supports complex data structure creation function to handle optimism. In addition, the data check is supported.
· Business entity collection is designed to handle collection and complex relationships, do not need to write yourself.
· Maintaining schema modifications do not affect the method identity. But if you use a strong name, you need to recompile when a new version.
The disadvantage is as follows:
· Performance instantiation and group DataSet triggers runtime cost
• A single business entity DataSet is designed to be used. If you use a single other way, it is better than using DataSet.
As input and output by custom business entity components
The advantages are as follows:
· Maintenance
· Business entity collection
The disadvantage is as follows:
· Support optimism concurrency
· Restriction integration
Returns Data Reader as an output
The advantages are as follows
· Performance When you need to quickly provide data, you can configure your data access logic components in the representation layer code.
Disadvantages
· Remote If you use a remote scene, it is a policy because there is a potential customer to take up a long time. Use stored procedures in data access logic components
You can use stored procedures to perform a lot of data access tasks through data access logic components.
advantage
· Store process generation results can improve performance because the database can optimize the data access plan through the process and cache schedule.
· In the database, the stored procedure can be relatively confined. Administrators can give user authorization to perform stored procedures.
• The stored procedure may be more easily maintained because the modified stored procedure is simpler than the hardcoded SQL statement that modifications in the configuration component.
• The stored procedure adds an additional abstraction level under the database Schema. The stored procedure is separated from the details of the implementation of the stored procedure.
· The stored procedure can reduce network blocking. The SQL statement can be executed in batches without the need to apply a large number of SQL requests.
Although there are so many advantages, there is also a place where it is not suitable or not recommended to use the stored procedure.
Disadvantage
• Apply Volume External Business Logic and Processing may increase the load on the server, especially in the stored procedure. For example, the processing type includes data migration, data round-trip movement, data transformation, and intrinsic calculation operations. You should put these to business processing or data access logic components, so more scalability.
• Do not put all business logic in the stored procedure. The flexibility of maintenance and application will become a problem when you need to modify the business logic in T-SQL. For example, a stand-alone software open provider can support multiple RDBMSs that do not maintain scattered stored procedures for each system.
· Writing and maintaining storage procedures usually requires special skills, which is not all developers owned. This situation may bring bottlenecks in the project development table.
Suggestions for using stored procedures in data access logic components
Considering the following suggestions when the data access logic component uses a stored procedure:
• Display stored procedure data access logic components should be unique to display components of database Schema information, such as stored procedures, parameters, tables, and fields. Your business entity should have no database SCHEMA information or does not depend on the database SCHEMA.
• Managing stored procedures with data access logic Components Each stored procedure can only be called and associated with data access logic components. For example, envisioning the customer's order giving a retailer. You should write a stored procedure to name OradInsert, create a order in the database. In your application, you must decide whether to call the stored procedure from the customer data access logic component or from the order data access logic component. Order Data Access Logic Components should be better, think it handles all orders related operations (customer data access logic components handle customer information, such as customer names and address)
· Naming Storage Procedure When you define a stored procedure for the data access logic component, the selected name is to emphasize the appropriate data access logic component. Such name habits can easily determine which stored procedure used. For example, CustomerInsert, CustomerUpdate, CustomergetBycustomerid
· Surface Safety Problem If you accept user entry execution dynamic query, do not use the parameters to create a string through the connection value. If you use sp_execute to perform a result string or you don't use the advantages of sp_execute SQL parameter support to avoid using a string connection during the stored procedure. Management lock and concurrency
Lock and concurrent everyone should be very familiar, I will describe myself simple description without complying with the original text. There are two ways to manage locks and concurrent. Pessimistic concurrency and optimistic concurrency. There are several ways to test optimistic concurrency.
· Use a distributed timestamp to add a timestamp in the field, get a timestamp, and the timestamp is compared when updated.
· Maintain a backup of a original data value first get a query original value, and the update is compared to the backup data.
· DataSet has the original value, the update is the use of Data Adapter to perform optimism and check
· Use a central timestamp to define a timestamp table to write a log for all updates. It can also provide audit function.
Implement optimism with Data Adapters and DataSets
For DataAdapter to write optimistic concurrently in DataAdapter.RowUpdated; when there is a large number of SQL statements during stored procedures, the SET NOCOUNT ON option is selected for performance reasons. This option does not return messages for each statement, reducing network blocking. But you can't check the RecordSaffected property because it is always -1. But you can return @@ rowcount, so you can specify @@ rowcount as the return value when updating. Implement business entity
Business entity has the following characteristics
· Business entity provides state-of-state capacity access business data
· Business entity can create from complex Schema, such as data that can be multiple associated tables
· Business entity can circulate as part of the I / O parameter during business processing
· The business entity can be serialized to maintain the state of the current entity. For example, an application can save entity data to a local hard drive, or if you apply an offline job from a desktop database or a message queue.
As mentioned before, there are many ways to represent business entities:
· Ordinary DataSet
· Typed DataSet
· Customized business entity components
· Customized business entity components containing CRUD behavior
Next, describe how to represent each business entity, to help you decide how to choose the most appropriate representation in your specific environment, how to describe how each business entity format performs the following tasks
· Organizational business entity collection
· Data Bind to User Interface Controls
· Serialized business entity data
· All levels of circulation business entity data
At the same time, non-functional needs will be considered, including performance, efficiency, scalability, and scalability.
Express business entity with XML
The following example uses XML to represent a simple business entity product
XML Version = "1.0"?>
10 BoxES X 20
Bags quantityperunit>
Product>
Consider the following guidelines when using XML to represent a business entity:
· Decide whether the XML document should include a business entity or business entity collection
· Use unique naming to identify the elements of XML to avoid conflicts
· Select the appropriate name for each element and attribute. It is naming to make sense
· Use the following way to obtain business entities
· If you use SQL Server2000, you can use for XML statements in your query or SP. Depending on performance test, use for XML to return to DataSet.
· Get DataSet and transform it or write it into the form of XML stream.
· Create an XML document by outputting parameters or using Data Reader. Data Reader is the fastest way to read multiple records from the database, but create an XML process reduces performance.
XML format as the advantages of business entity
· Standard support. W3C
· Flexible
· Interoperability, such as ASP.NET, Windows Form, DataSet
Disadvantages:
· Type distortion is just a string, of course you can use XSD Schema
• Check XML In order to verify, you need to resolve code and use XSD Schema. These speeds are quite slow
• Display XML can not automatically display XML data, you need to write XSLT format to translate to DataSet. And the style sheet is not easy to write.
· Analysis XML you need DOM or XMLReader, XmlReader only provides fast forward, read-only to read XML data. DOM is powerful, but the speed is slow.
· Sorting XML To use the following way
· Submit data according to encoding
· Dynamic sort data with XSLT style sheet
· Translate XML data to DataSet
· Use private fields you have no choice to hide information
Represents business entities using ordinary DataSet
advantage
· Flexible
· Binding
· Sort and filtering
· And XML conversion ability
· Available metadata
· Optimistic concurrency
· Scalability
Disadvantages:
· Performance
· Private field
Represents business entities using Typed DataSet
advantage:
Readability
· Compilation type check
Disadvantages:
· Configuration
· Support COM
· Scalability
· Instantiation
Inherit custom business entity components
With the following members
· Private field
· Common attribute
· Method and attribute
· Event
Consider the following suggestions
· Select StruCTS and CLASS
· Represents business entity status
· There are two representations of subsets
· Arraylist
· DataSet
· Data binding
· Enable business entities to serialize
· XML serialization
· Binary serialization
advantage:
Readability
· Package
· Complex system model
· Local verification
· Private field
Disadvantages:
· Business entity collection
· Serialization
· Expand complex relationships and levels
· Query and sort data
· Configuration
· Support COM
· Expansion issues
Customized business entity components contain crud (omitted)
Several suggestions
· If the application is mainly a collection, you need to sort, query, for the home DataSet, if the application uses instance data, the scalar value is better
· Application mainly uses instance data, custom business entities may be the best choice