In the previous article, "restructuring the data center billing system (3)", I mainly introduced my understanding about the Dal layer, the factory layer, the interface layer, and the entity layer, but a lot of readers ask me again. What about your code? I am here to explain that I just wrote my understanding about those parts and didn't write or post the code for your research, however, it is not a good article that does not conform to the hearts of the people. Therefore, in this article, I will post all the code in layer-7 of the fee logon in the IDC room for everyone to make a good decision, finally, let's talk about the BLL layer and the appearance layer.
First, the layer 7 is composed of the UI Layer, appearance layer, BLL layer, Dal layer, factory layer, entity layer, and interface layer.
To implement the login form, you must first check whether the user exists and then write the login information to the database. The two main steps are as follows.
First, let's take a look at the two tables used in my database:
This makes it easier for readers to understand the code below:
I. First, let's look at the code at the Entity layer.
(1) User table entity layer:
Public class mo_login private _ userid as string private _ level as string private _ password as string private _ username as string private _ computer as string public shared userhead as string 'sets the global variable public shared userlevel. as string public property userid () as string get return _ userid end get set (value as string) _ userid = value end set end property public property password () as string get return _ password end get set (value as string) _ password = value end set end property public property level () as string get return _ level end get set (value as string) _ Level = value end set end property public property username () as string get return _ username end get set (value as string) _ username = value end set end property public property computer () as string get return _ computer end get set (value as string) _ computer = value end set end propertyend class
(2) work record entity layer:
Public Class MO_Worklog Private _userID As String Private _level As String Private _loginDataTime As String Private _computer As String Private _status As String Public Shared Property Login_DataTime As String Public Property UserID() As String Get Return _userID End Get Set(value As String) _userID = value End Set End Property Public Property level() As String Get Return _level End Get Set(value As String) _level = value End Set End Property Public Property LoginDataTime() As String Get Return _loginDataTime End Get Set(value As String) _loginDataTime = value End Set End Property Public Property Computer() As String Get Return _computer End Get Set(value As String) _computer = value End Set End Property Public Property Status() As String Get Return _status End Get Set(value As String) _status = value End Set End PropertyEnd Class
Ii. Let's take a look at the UI Layer:
Public class frmlogin private sub btnok_click (sender as object, e as eventargs) handles btnok. click dim login as new model. mo_login dim falogin as new facade. fa_login 'dim strresult as string login during instantiation. userid = txtusername. login. password = txtpassword. text model. mo_login.userhead = txtusername. text dim worklog as new model. mo_worklog 'instantiate the worklog. logindatatime = date. now. tostring ("yyyy-mm-dd hh: mm: SS") 'worklog on time. status = "on duty" 'working status worklog. userid = model. mo_login.userhead worklog. computer = system. net. DNS. gethostname (). tostring () 'local computer strresult = falogin. flogin (login, worklog) 'select case strresult' during parameter return process judgment case "incorrect input" msgbox ("incorrect input, please input again ") case "Logon successful" msgbox ("Logon successful") model. mo_worklog.login_datatime = worklog. logindatatime me. hide () 'frmmain. show () end select end sub private sub btncancel_click (sender as object, e as eventargs) handles btncancel. click end subend class
Iii. Appearance Layer Code:
Imports bllpublic class fa_login public function flogin (byval user as model. mo_login, worklog as model. mo_worklog) as string dim userbll as new BLL. bl_login if userbll. isnotnull (user, worklog) = false then return "input error" else return "Logon successful" end if end functionend class
4. BLL Layer Code:
<PRE name = "code" class = "VB"> imports idalpublic class bl_login public function isnotnull (byval user as model. mo_login, byval worklog as model. mo_worklog) as Boolean dim iuser as idal. ilogin' note that the interface type cannot be instantiated. Dim factory as new factory. loginfactory 'instantiate the factory dim iworklog as idal. iworklog iuser = factory. createuserinfo () 'factory Creation Interface, Dal implementation interface, BLL calls factory iworklog = factory. createworklog () If iuser. user_login (user ). userid = "" then' if it is null return false 'boolean value is false else worklog. level = iuser. user_login (user ). level, send the queried user type to the worklog entity layer, and apply it in addition, deletion, modification, and so on. If iworklog. saveworklog (worklog) then 'If the user exists, the login information is written to the database. Return true end if return true end if end functionend class
5. Factory Layer Code:
Imports system. reflection 'Add a reflection reference imports system. configuration 'add configuration file reference imports idalpublic class loginfactory 'Using Reflection + configuration file + Abstract Factory dim strdb as string = system. configuration. configurationsettings. appsettings ("strsqlconnection") 'indicates to read the configuration file. If the configuration file contains an sqlserver database, it will be accessed. If it is another database, it will be accessed, you do not need to change the code 'dim strqueryworklog as string = system. configuration. configurationsettings. deleetask( "strsqlconnection") 'creates a user table factory public function createuserinfo () as ilogin return ctype (assembly. load ("Dal "). createinstance ("Dal "&". "&" da_login "), ilogin) end function public function createworklog () as iworklog return ctype (assembly. load ("Dal "). createinstance ("Dal "&". "&" da_worklog "), iworklog) end Function
End Class
6. Interface Layer Code:
(1) user query interface
Public Interface ILogin Function User_Login(ByVal user As Model.MO_Login) As Model.MO_LoginEnd Interface
(2) write operation records to the database code:
Public Interface IWorklog Function SaveWorkLog(ByVal worklog As Model.MO_Worklog) As BooleanEnd Interface
7. The code at the Dal layer is as follows:
(1) user query dal
Imports system. data. sqlclientimports system. dataimports idalpublic class da_login: Implements idal. ilogin public function user_login (user as model. mo_login) as model. mo_login implements idal. ilogin. user_login dim sqlparams as sqlparameter () = {New sqlparameter ("@ userid", user. userid), new sqlparameter ("@ password", user. password)} 'parameter passing process dim plain text as string = "select * From t_userinfo where user Name [email protected] and password [email protected] "'query database dim auser as new model. mo_login 'dim helper as new sqlhelper dim ready type as commandtype = new commandtype () Ready type = commandtype. text 'defines the command type as stored procedure dim table as able able' instantiate a process' here I want to explain one, because the function return parameters are the entity layer, however, the table defined here is a datable type, so the system cannot perform conversion. So I defined an auser for transit. Table = helper. executequery (plain text, plain type, sqlparams) 'defines the returned value if table. rows. count <> 0 then auser. userid = table. rows (0 ). item ("userid") auser. password = table. rows (0 ). item ("password") auser. level = table. rows (0 ). item ("level") auser. username = table. rows (0 ). item ("username") end if return auser end functionend class
(2) write operation records to the Code dal
Imports System.Data.SqlClientImports System.DataImports IDALPublic Class DA_Worklog : Implements IDAL.IWorklog Public Function SaveWorkLog(worklog As Model.MO_Worklog) As Boolean Implements IWorklog.SaveWorkLog Dim strText As String = "INSERT INTO T_WorkLog(UserID,level,LoginDateTime,Computer,Status)VALUES (@UserID,@level,@LoginDateTime,@Computer,@Status)" Dim sqlparams As SqlParameter() = {New SqlParameter("@UserID", worklog.UserID), New SqlParameter("@level", worklog.level), New SqlParameter("@LoginDateTime", worklog.LoginDataTime), New SqlParameter("@Computer", worklog.Computer), New SqlParameter("@Status", worklog.Status)} Dim cmdType As CommandType = CommandType.Text Dim helper As New SqlHelper Return helper.ExecuteNonQuery(strText, cmdType, sqlparams) End FunctionEnd Class
In fact, these codes are not complete, and the content about the configuration of factory reflection is missing, and there is sqlhelper. Sqlhelper, which is detailed in my "Data Center billing reconstruction (4. This will stop writing.
Finally, let's talk about BLL and the appearance layer. From the code perspective, it seems that the appearance layer has no function. Yes, there is no need to use the appearance layer for the implementation of the login form small function, but when you need to type a huge form in the future, we need to decouple the U layer and B layer, so the role of the appearance layer is highlighted. There is nothing to say about BLL and appearance. The most important thing is about data type conversion, because the process of passing parameters and return parameters between layers requires the same type of data, however, it is impossible to set the data types to able and string or object types. I hope you will pay attention to it in the future coding process.
There are a lot of code in this article. I hope you can make a correct decision ..