Reconstruction version of the computer room fee system layer, interface, database connection, Reflection + factory (vb.net)

Source: Internet
Author: User


Layered

Layering is to reduce the layer-to-layer dependencies, add the readability of the program, and make the whole system structure clear. It can also greatly reduce maintenance costs, but there are some drawbacks to layering, and some can directly access the database layer, but through the database responsible for visiting the layer. In this way, there is a link in the Access database process, adding the overhead of the system, and sometimes adding a feature to the presentation layer. In order to reduce the coupling, we have to add the service class required for this function from top to bottom and add the development cost to each layer.

The more layered the better? The answer is no, quote a sentence "We must always remember: can not be blindly layered, should not be layered and layered should not pattern and pattern."

This is very important. Otherwise, you can only add the burden of development (better experience in future practice). "should be said to be based on the actual situation of layering, after all, is not absolute, because some systems do not layered score layer a lot of others."

The main layering is the three-tier architecture:

presentation Layer (UI)

mainly used for interaction with the user, responsible for conveying the user's instructions and data to the BLL layer. And the user needs to display the data, popular speaking is the user can see the interface, such as the window program.

Business Logic Layer (BLL)

The logical processing of data. For example, the data obtained from the Access database is converted into the data that the user sees and submitted to the presentation layer for display.

Data Access Layer (DAL)

Access to the database, to provide additions and deletions and other operations.

Here is my package diagram, where each package is a layer with the addition of a solid layer (Model), Interface layer (Idal), and a factory layer (Factory)



Solid Layer (Model)

is actually each table in the corresponding database. A table is built with a class. The property inside a class is the field in the corresponding table, for example, table Basedata_info


In the entity layer, you can create a class like this:

Public Class basedataentity Inherits Entity Private fixeduserhalfhourcost as single ' fixed user spends half an hour public Prop  Erty Fixduserhalfhourcost_ as single get Return fixeduserhalfhourcost End Get Set (value as Single) Fixeduserhalfhourcost = value End Set End Property Private Casualuseranhourcost as Singl         E ' Temporary user spends an hour on public property casualuseranhourcost_ as a single Get Return casualuseranhourcost End Get Set (value as single) Casualuseranhourcost = value End Set End Property Private I Ncreasingunittime as single unit increment time public property increasingunittime_ as single Get Return incre Asingunittime End Get Set (value as single) Increasingunittime = value End Set End Prope            Rty Private atleastonlinetime as single ' minimum online time public property atleastonlinetime_ as a single Get Return AtleastonliNetime End Get Set (value as single) Atleastonlinetime = value End Set End Property        Private Readytime as single ' Prep time public property readytime_ as single Get Return readytime  End Get Set (value as single) Readytime = value End Set End Property Private Atleastmoney        As single minimum Amount public property Atleastmoney_ as single get Return Atleastmoney End Get Set (value as single) Atleastmoney = value End Set End Property End Class


What's the use of it? When you need to register a user, you have to pass the data from the presentation layer (UI) to the data Access layer (DAL) used to access the database, but. You will not be able to register the user information: study number. Name, card number, registration date, registration time, class.

。。。。 And so on, pass the number of parameters to the function. Do you have to add rows?

    ' You don't really do that, do you? ' Was frightened by the    AddUser (studentid,studentname,cardid,registerdate,registertime,class.

。。。

。。 ) ' and suppose you encapsulate this data into a class. As above, you just need to pass a class AddUser (UserInfo)

Database connection

For access to the database, basically what language is inseparable from such a few steps:

1. Connect to the database

2. Run the SQL statement

3. Return the run structure of the SQL statement

and connected to the database string, you can participate in the database connection string Daquan

Running SQL statements can be a reference to SQL statement Daquan

Here's a simple example of a database interview:

ImportsSystem.Data.SqlClient   ' references sqlclientpublic Class selectdal public    Overloads Function Selectinfo (ByVal Table As String)     ' query entire table        Dim DataS As New DataSet        Try            Dim selectstr As String = "select * from" & Tabl E     ' query statement            Dim connectstr as String = "Data source=server name/server address; Initial catalog= database name; Userid=sa; password=123 "' Database connection string            Dim DBConnection as New SqlConnection (CONNECTSTR) ' Initialize database connection object             Dbconnection.open () ' Connect database            ' Dim dbcmd As New SqlCommand (selectstr,dbconnection)    ' run the query statement            Dim Adapter As New SqlDataAdapter ( selectstr,dbconnection)  ' Save the query results to the cache             Adapter.fill (DataS, table)      ' Save the table in the cache to the DataSet          as table name Catch ex as Exception            Throw New argumentoutofrangeexception ("" & Ex. Message)       End Try         Return DataS End Function

The object used to connect to the database is SqlConnection. The SqlDataAdapter object is used to run the query statement and return the results.

There are many ways to connect a database, such as local, remote, and so on.

Only to participate in the database connection string, according to their own circumstances to choose to use can achieve the goal.

Reflection + Factory, interface

for reflection, I was very dazed at first, after knocking out a sample in design mode. A lot of information was checked. I don't know how to apply it to the charging system. It's really stupid to get home.

Just, later saw the Tall Man's blog. It's going to be.

See:


If you have a BLL and DAL2 layer, you want a Class A in the BLL to use the Balancedal class in the DAL, you can do this by referencing the DAL, but it is said to reduce the coupling between the BLL and the DAL. So an interface layer was added between the BLL and the Dal called Idal.

For example with


you can see the Balancedal interface in the box above the DAL. This interface includes all the methods of the detailed class (Balancedal). So we just have to invoke the interface. This is equivalent to invoking the detailed class (Balancedal), but only if you have to connect the interface and the detail class. Like the TV and remote control, the remote control is the TV interface, to the remote control interface can be controlled. There should be a device in the TV that can receive and process the new infrared number, when you use the remote control. The remote control and the TV are Unicom, they passed the infrared link.

In fact, the relationship between the interface and the detailed class is simply a pipe created between the two. There is no detailed function in the interface, but there is a way to use the detailed class (like the button on the remote control), if the remote control and the TV is not connected, then you click the remote control, will not realize the power-on or FM functions. Therefore, to use the interface, you must let the interface and the detailed implementation of the class to connect, here are two steps to go. The first step is the reference, and the second step is to create the instance. See:



It is also equivalent to a bridge between the two layers by ticking the idal in the Dal reference. Next. We let the DAL implement this interface, that is, after creating the detail class in the DAL. Write keyword+ the interface you want to implement, and then enter. It will voluntarily list the methods you write in the interface, and then the method. You go to the detailed implementation of it.


Then, we need to add a pipe to the bridge, add the pipeline, that is, the interface method and the method of the detailed class to connect together. The method that allows me to invoke the interface inside the BLL layer. It's the equivalent of calling a detailed class, but I don't know how the detail class is going to be implemented in the BLL, it's said. This is to reduce the coupling between the BLL layer and the DAL layer, because I only care about the interface Idal. The following code, for example, looks at how the interface is used in the BLL

Imports system.reflection           ' reflected reference public CLASSBALANCEBLL    functionbalancequery (ByVal CardID asstring)        Ifcardid = "            then Thrownewargumentoutofrangeexception (" "," Enter card number: ")        EndIf dimibalance as         idal. Ibalance    ' Remaining Amount interface       ibalance = CType (Assembly.Load ("DAL"). CreateInstance ("DAL. Balancedal "), Idal. ibalance)   ' Get the remaining Amount class        Dimds as NewDataSet        Try            DS = ibalance.selectbalanceinfo (CardID) ' Query and return the dataset results            ifds.tables (0). Rows.Count < 1 then               Throw newargumentoutofrangeexception ("", "no remaining amount recorded for this card number")            EndIf        Catchex as Exception            thrownewargumentoutofrangeexception ("", ex. Message)        endtry        returnds    endfunction

Here, we first reference the reflection, then create the interface (ibalance), then the reflection. The instance of the Balancedal detail class in the DAL layer is reflected to the interface. Thus, we would be equivalent to a pipeline between the interface and the detail class, and then we would be able to use the method of the detailed class: Ibalance.selectbalanceinfo (CardID)' Query and return the dataset result

Later found. The class inside the DAL layer is reflected in the BLL layer. It is equivalent to producing the class of the DAL layer inside the BLL layer, and then reflecting the code of the detailed class. I then abstracted out a layer, called the factory layer. Only the BLL cites the factory and is able to return the detailed class through the factory.

This is the application of Reflection + factory.

It seems to be a little long written. There would have been more useful techniques to write about overloads, configuration files, exception handling, stored procedures, and so on. Forget it, put it in the next article. Hope to be of help to everyone.

Although object-oriented is more complex. But the whole feeling, it is the very many complex things classified, divided, layered, sub ... Then use the line to get them up, you don't have to remember all the pieces completely, but you can follow those drawings without knowing the details. Just get used to the way of thinking. It's not too hard to believe.


Reconstruction version of the computer room fee system layer, interface, database connection, Reflection + factory (vb.net)

Related Article

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.