MVC Framework example

Source: Internet
Author: User

Framework originated from petshop3.0
Divided into the following layers
Web
Bll
Idal
Model
Dalfactory
Nhdal

According to the framework of petshop3.0, the web layer only depends on BLL and model. That is to say, the web layer calls the data defined by the model layer returned by the BLL layer method. The bll layer depends on idal, model, this layer actually combines the methods in idal into a business and processes the models returned by the idal layer. idal defines all the underlying methods and leaves interfaces for the Dal layer. Model defines object entities, only attributes have no method, and can be basically defined as abstract classes. Dalfactory is used to Dynamically Retrieve the instance of the data access layer object based on the settings in the configuration file. In this way, the actual dal is separated from the BLL layer, to switch to another Dal, you only need to modify this layer.

This project uses NH (nhib.pdf) as the Dal, so it is named nhdal. Nhdal depends on model, inherits the definition of attributes in model, and adds the attribute tool required by NH. At the same time, nhdal implements the methods defined in idal.

The following are examples of login verification and log storage
Let's take a look at the definitions in idal and model.

CodeSection 1 idal Interface Definition

Using system;

Namespace cmanager. idal. Log
{
/** // <Summary>
/// Summary of iloginlog.
/// </Summary>
Public interface isignlog
{
Void logsignin (string userid, string sessionid, string clientip, datetime logindate );

Bool checkpassword (string userid, string password );
}
}

Code snippet 2 model object definition

Using system;

Namespace cmanager. model. Log
{
/** // <Summary>
/// Logininfo summary.
/// </Summary>
Public class signloginfo
{
Public int signlogid
{
Get {return _ signlogid ;}
}
Public String userid
{
Get {return _ userid ;}
}
Public String sessionid
{
Get {return _ sessionid ;}
}
Public String clientip
{
Get {return _ clientip ;}
}
Public datetime signindate
{
Get {return _ signindate ;}
}
Public datetime signoutdate
{
Get {return _ signoutdate ;}
}

Protected int _ signlogid;
Protected string _ userid;
Protected string _ sessionid;
Protected string _ clientip;
Protected datetime _ signindate;
Protected datetime _ signoutdate;
}
}

Then it is called in BLL. Because BLL does not rely on any Dal, it creates a dal instance through the dalfactory in the middle layer and returns it to BLL through the interface defined by idal.

Code snippet 3 BLL Method

Using system;
Using system. collections;
Using system. Web;

Using cmanager. model. log;
Using cmanager. model. collections;
Using cmanager. dalfactory. log;
Using cmanager. idal. log;

Namespace cmanager. BLL. Log
{
Public class log
{

// Unfinished. The module. log. Account should be returned
Public static bool login (string userid, string password, httpcontext context)
{
Isignlog log = logfactory. createsignlog ();
If (log. checkpassword (userid, password ))
{
Log. logsignin (userid, context. session. sessionid, context. Request. userhostaddress, datetime. Now );
Return true;
}
Else
{
Return false;
}
}
}
}

Code snippet 4 dalfactory Method

Using system;
Using cmanager. idal. log;
Using system. reflection;

Namespace cmanager. dalfactory. Log
{
Public class logfactory
{
Public static isignlog createsignlog ()
{
// Obtain the Dal settings in Web. config.
String Path = system. configuration. configurationsettings. receivettings ["webdal"];
// Combine the class names
String classname = path + ". log. signlog ";
// Create a class instance at runtime and instantiate the class.
Return (isignlog) Assembly. Load (PATH). createinstance (classname );
}
}
}
Code snippet 5 web. config

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<! -- Everyone is familiar with this section -->
<Configsections>
<Section name = "Nhibernate" type = "system. configuration. namevaluesectionhandler, system, version = 1.0.5000.0, culture = neutral, publickeytoken = b77a5c561934e089"/>
</Configsections>
<Nhib.pdf>
<Add key = "hibernate. Connection. provider" value = "nhib.pdf. Connection. driverconnectionprovider"/>
<Add key = "hibernate. Connection. driver_class" value = "nhib.pdf. Driver. sqlclientdriver"/>
<Add key = "hibernate. Connection. connection_string" value = "Server = 127.0.0.1; database = cmanager; uid = sa; Pwd =;"/>
<Add key = "hibernate. Connection. Isolation" value = "readcommitted"/>
<Add key = "hibernate. dialect" value = "nhib.pdf. dialect. mssql2000dialect"/>
</Nhib.pdf>
<Deleetask>
<! -- Displays the number of records per page by default -->
<Add key = "defaultrecordcount" value = "20"/>
<! -- * Namespace where the database layer is located * the actual used dal is set here -->
<Add key = "webdal" value = "cmanager. nhdal"/>
</Appsettings>
<System. Web>
<! -- This section is pressed and does not occupy the page -->
</System. Web>

</Configuration>

Theoretically, the web layer does not know the atomic method defined by the idal layer. It only uses the method given by the BLL layer. This is the so-called separation between the business layer and the presentation layer. The web layer only accepts requests sent by users, sends processing parameters to the BLL layer, and judges the results returned by the BLL layer.

Code snippet 6 web layer call

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Using cmanager. BLL. log;
Using cmanager. model. log;

namespace bttech. cmanager. Web
{< br>/** ////


/// Summary of login.
//
public class login: pagebase
{< br> protected system. web. UI. webcontrols. textbox username;
protected system. web. UI. webcontrols. textbox password;
protected system. web. UI. htmlcontrols. htmlinputimage imagebutton1;
private void page_load (Object sender, system. eventargs e)
{< br>
}

Private void imagebutton1_serverclick (Object sender, system. Web. UI. imageclickeventargs E)
{
If (log. login (username. Text, password. text, this. Context ))
Response. Redirect ("", true );
Else
Alert ("incorrect user name or password ");
}
}
}

The final part of this article is nhdal, which is the final performer of all practical operations.

Code segment 7 implements idal

 

Using system;
Using nhib.pdf. cfg;
Using nhib.pdf;

Namespace cmanager. nhdal. Log
{
/** // <Summary>
/// Abstract description of signlog.
/// </Summary>
Public class signlog: nhobject, cmanager. idal. log. isignlog
{
Public signlog ()
{
}

Public void logsignin (string userid, string sessionid, string clientip, datetime logindate)
{
Isession session = createsession ();
Session. Save (New signloginfo (userid, sessionid, clientip, logindate ));
}

Public bool checkpassword (string userid, string password)
{// Unfinished
Return true;
}
}
}

Code Segment 8 inherits model

Using system;
Using cmanager. model;

Namespace cmanager. nhdal. Log
{
/** // <Summary>
/// Abstract description of signlog.
/// </Summary>
Public class signloginfo: cmanager. model. log. signloginfo
{
Private int ID
{
Get {return this. _ signlogid ;}
Set {This. _ signlogid = value ;}
}
Private string nh_userid
{
Get {return _ userid ;}
Set {_ userid = value ;}
}
Private string nh_sessionid
{
Get {return _ sessionid ;}
Set {_ sessionid = value ;}
}
Private string nh_clientip
{
Get {return _ clientip ;}
Set {_ clientip = value ;}
}
Private datetime nh_signindate
{
Get {return _ signindate ;}
Set {_ signindate = value ;}
}
Private datetime nh_signoutdate
{
Get {return _ signoutdate ;}
Set {_ signoutdate = value ;}
}

Private signloginfo ()
{

}

Internal signloginfo (string userid, string sessionid, string clientip, datetime signdate)
{
This. _ userid = userid;
This. _ sessionid = sessionid;
This. _ clientip = clientip;
This. _ signindate = signdate;
This. _ signoutdate = signdate;
}
}
}

Code snippet 9: shadow file signloginfo. HBM. xml used hereArticleMethods mentioned in "Do not let the NH attribute machine destroy encapsulation"

<? XML version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.0">
<Class name = "cmanager. nhdal. log. signloginfo, cmanager. nhdal" table = "cm_signlog">
<ID name = "ID" type = "int32" unsaved-value = "0">
<Column name = "signlogid" SQL-type = "int" not-null = "true" unique = "true" Index = "pk1_cm _ signlog _ 79a81403"/>
<Generator class = "native"/>
</ID>
<Property name = "nh_userid" type = "string">
<Column name = "userid" length = "50" SQL-type = "nvarchar" not-null = "true"/>
</Property>
<Property name = "nh_sessionid" type = "string">
<Column name = "sessionid" length = "24" SQL-type = "nvarchar" not-null = "true"/>
</Property>
<Property name = "nh_clientip" type = "string">
<Column name = "clientip" length = "20" SQL-type = "nvarchar" not-null = "true"/>
</Property>
<Property name = "nh_signindate" type = "datetime">
<Column name = "signindate" SQL-type = "datetime" not-null = "true"/>
</Property>
<Property name = "nh_signoutdate" type = "datetime">
<Column name = "signoutdate" SQL-type = "datetime" not-null = "false"/>
</Property>
</Class>
</Hibernate-mapping>
10 tips for code segments, nhobject base class
Defines a base class of nhobject. Because any actual operation object is a nhobject, It inherits this class.
The advantage of this is that when we need to change the method of loading the connection string (such as decryption), we only need to make modifications here, and we can also change the usage of NH in a large area.

Using system;
Using nhib.pdf;
Using nhib.pdf. cfg;

Namespace cmanager. nhdal
{
/** // <Summary>
/// Summary of nhconfigbase.
/// </Summary>
Public class nhobject
{
Private Static configuration Config = new configuration (). addassembly (system. reflection. Assembly. Load ("cmanager. nhdal "));
Private Static isessionfactory sessionfactory = config. buildsessionfactory ();
Protected static Nhibernate. isession createsession ()
{
Return sessionfactory. opensession ();
}
}
}

Such a complete framework is successfully built and run. :)

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.