Computer room cooperation---business logic where?

Source: Internet
Author: User
Tags boolean table

Preface: In the course of learning, do not feel ashamed because of the problems left behind, after the project, as long as the intentions, can solve the remaining problems.

In this article, I would like to record my rethinking of the logic of the UI, appearance, and the BLL layer.

Acceptance of the Machine room reconstruction, my UI layer, the appearance layer, the BLL layer code is written like this: (to log in this line for example)

First look at the BLL layer:

Public Class Loginmanager ' checks if the user name exists public Function isuserexists (ByVal user as Entity.userinfo) as Boolean D Im factory as New factory.loginfactory () Dim Iuser as Idal. Iuserinfo ' Call the ' Create user ' factory method Iuser = Factory. Createuserinfo () Dim table As DataTable Dim flag as Boolean Table = iuser.queryuser (user) If T Able. Rows.Count = 0 Then flag = False Else flag = True End If Return flag End Fun Ction ' checks if the password is correct public Function ispwdright (ByVal user as Entity.userinfo) as DataTable Dim factory as New Fac Tory. Loginfactory () Dim Iuser as Idal. Iuserinfo Dim table as DataTable ' intermediate variable for storing data Iuser = Factory for D-layer queries. Createuserinfo table = iuser.queryuser (user) Return table End Function ' Inserts a record public into the Worklog table funct Ion Insertworklog (ByVal user as Entity.userinfo) as Integer Dim factory As New Factory.worklogfactory Dim Iw Orklog as Idal.Iworklog Dim m as Integer Iworklog = Factory. Createworklog () m = iworklog.insertworklog (user) Return m End functionend Class

In addition to some of the fragmented's if else judgments in each method, in general, the logical judgment layer is "overhead", where is my business logic?

then look at the appearance:

Public Class Loginfacade    ' is used to check if the user has public    Function CheckUser (ByVal user as Entity.userinfo) as Boolean        ' Defines a B-layer to determine if the user ID exists for the object Dim isuserexists as New BLL. Loginmanager ()         Dim flag as Boolean        flag = isuserexists.isuserexists (user)        If flag = True then            Return True        Else            Return False        if    End Function    ' is used to check if the password is correct public    Function checkpwd (ByVal User as Entity.userinfo) as DataTable        Dim ispwd as New BLL. Loginmanager ()        Dim table as DataTable        table = ispwd.ispwdright (User)        Return table    End functionend Class

Loginfacade This class of two methods, with the current perspective again, is in the B layer outside and "paste a layer", no meaning, no wonder when the master said I, now I look at the real mess.

Finally, look at the UI layer:

Private Sub Cmdok_click (sender as Object, e as EventArgs) Handles Cmdok.click ' first to determine if the user name is empty and if the password is empty if Txtuser Id. Text = "Then MessageBox.Show (" Enter user name!)            "," Reminder ", MessageBoxButtons.OK, messageboxicon.exclamation) Txtuserid.text =" "Txtuserid.focus () Exit Sub ' exits the current program ElseIf txtPassword.Text = "Then MessageBox.Show (" Enter the password!             "," Reminder ", MessageBoxButtons.OK, messageboxicon.exclamation) txtPassword.Text =" "Txtpassword.focus ()        Exit Sub End If ' ****************************************************** ' defines an object for the skin layer ' Verification of password is more troublesome than user name validation with Dim facadelogin As New Facade.loginfacade Dim user As New Entity.userinfo ' validation User name is correct. UserName = Txtuserid.text user. Password = txtPassword.Text Dim strResult1 As Boolean ' to determine whether the user name is correct is to use a Boolean value to determine STRRESULT1 = Facadelogin.checkuse            R (user) If strResult1 = False ThenMsgBox ("There is no user, please re-enter!") ", vbexclamation + vbOKOnly," reminder ") Txtuserid.text =" "txtPassword.Text =" "txtuserid.se Lect () Txtuserid.focus () Exit Sub End If ' Verify that the password is correct Dim table as DataTable ' Judge secret The code is correctly used to return to the DataTable to determine table = facadelogin.checkpwd (user) if trim (txtpassword.text) = trim (table. Rows (0). Item (2)) Then ' MsgBox ("Login Successful! ") Charge.Entity.Holder.Holder = Txtuserid.text ' Entity.Model.UserName = Txtuserid.text set global variables that were originally used to Use Me.hide () frmmain.show () ' Change password ' to display the main form after successful login ("Password error, please re-MsgBox New input!        ", vbexclamation + vbOKOnly," Reminder ") txtpassword.focus () Exit Sub End If ' write to work record  Dim workuser As New entity.userinfo Workuser.userid = Txtuserid.text Dim m As Integer Dim Worklogui As New BLL. Loginmanager m = Worklogui.insertworklog (workuser) End Sub

A surface class used to display data and input data was filled with code, full of logic ...

The original BLL layer of logic code are here Ah, fortunately, now the cooperation, I have a new understanding.

Problems encountered in the BLL layer during refactoring:

When refactoring the BLL layer is divided according to "function", the problem is that some methods need to be reused, which makes the code redundant. For example, to determine if the card number exists this method needs to register, recharge, return card and everything for the card to be used in the operation of the query, the cost is that every time you need to write such a method.

When collaborating on the BLL layer idea:

firstly:

This time because I am responsible for writing the B-layer code, to solve this problem, at first my view is that the BLL layer according to the "data table" to divide the methods required for each table, and then in the appearance layer according to functional classification and the B-layer method of integration, but this kind of business logic is written to FA? The ADE layer, the benefit of which is the simplicity of the code, but added logic to the Fa?ade layer and continues to improve. )

Then :

If you want to write the business logic purely to the BLL layer, I think in the BLL layer in accordance with the table partition, and then according to the function of the division, the result is not to solve the reconfiguration process in the BLL layer code redundancy problem, but let the code more redundant ... (after analysis, the principle is actually to write two of the BLL layer, the code is not thin enough, but basically satisfied with the logic encapsulated in Layer B.) )

Finally:

What do we do?

Thinking back to the original idea, what's wrong with the original ideas?

Is it very redundant for my BLL layer code to be divided according to "function"? The answer is in fact negative.

:

Take the login as an example, the meaning of doing this is in the same way as the method of writing the BLL layer is again encapsulated, but the method differs from the previous one: 1, the code is relatively streamlined, 2, the logical understanding is more clear, in the time frame diagram is also more intuitive; 3. Refer some public methods to the COMMONBLL class, Much like the VB learning process using modules to encapsulate the effect of the method, making the whole more consistent with the idea of object-oriented.

Begin to experience cooperation, to solve the problem of the reconstruction process slowly, until I apprentice to go this way, I will stand in a higher perspective to see what they write and their ideas.

That's all.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Computer room cooperation---business logic where?

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.