Simple reality of open session in view

Source: Internet
Author: User

In actual ASP. NET programming, An ASPX page usually needs to display many types of data (such as basic user information, user order details, and website news ). These different data are often read in different classes and Methods. Each method opens a connection and closes the connection once. Despite the connection pool of ADO. net, frequent dispose connection objects also affect the performance. If an operation needs to execute multiple business methods, the transaction must be consistent. In this case, make sure that multiple methods share one connection object.

 

Open session in view can solve this problem well.

Solve the problem by using the OO encapsulation principle. Now the connection is changed, so we can "free it.
We define a singleton class connectionfacade to serve the Dal method. Dal no longer has the responsibility to "manage connections". They only need to get a connection object for use. The connection is managed in connectionfacade. It only needs to publish the getcurrentconnection method.

 

We all know that web and windowsProgramDifferent, the server will assign a thread to execute a request for the web programCodeWindows programs are always a thread in the memory. Therefore, connection management policies are different.

1. For the next Web request to create a connection, we can compile an httpmodule to enable the connection in the beginrequest event, and disable the connection in the endrequest event.
2. the next program in winforms has a connection, which is enabled at startup and closed at exit.

 

To make our code adapt to the web and winforms presentation layers, we encapsulate this "Change Point" again. Iconnectionstrategy is represented by one interface and processed by webconnectionstrategy and winconnectionstrategy.

 

Setstrategy is added to connectionfacade to set the connection management method we should use.

1. For webforms, register webconnectionstrategy in connectionfacade. setstrategy in the app_start event. The self-compiled httpmodule automatically opens/closes the connection.
2. For winfroms, register winconnectionstrategy in the main method connectionfacade. setstrategy. And openconnection. At the end, closeconnection.

 

The overall design adopts the strategy + appearance pattern reality.

 

Attach class digoal for your reference. You are also welcome to give suggestions.

 

When I have time, I will make some practical code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.