Research on the Application of metadata-driven model architecture in ASP. NET

Source: Internet
Author: User

Currently, there are many popular Asp.net architectures, including open-source projects such as Microsoft patterns & practices.

Factory, nhibernet, nbear Orm, petshop and other architectures. Next I will introduce another metadata-based (XML) architecture.

This architecture is rarely used by many IT companies. It features high flexibility, simplicity, efficiency, and convenient IOC dependency injection.

This solution features good coupling and high development efficiency. It can be combined with the Microsoft enterprise database for efficient storage. I have a good success story in Microsoft's interconnected Star project.

The general idea is to use XML (template) for permission control. refer:

First, set the global object in global. asax. After the system is started, the related Persistent object will be loaded into the memory to improve the system running speed:

RelatedCodeAs follows:

<% @ Application language = "C #" %>
<% @ Import namespace = "Microsoft. XXXXX. Advertising. systemframework" %>

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
Systemvm Vm = new systemvm ();
Application. Lock ();
Application ["VM"] = VM;
Application. Unlock ();

}

Void application_end (Object sender, eventargs E)
{
// In ApplicationProgramCode that runs when it is disabled

}

Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs

}

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts

}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.

}

</SCRIPT>

Second, establish the systemvm. CS Driver Model class, including some related attributes and load related template templates:

The logical process is as follows:

The simple code is as follows:
Public class systemvm
{
// Role Template
Private systemservice m_system = NULL;
Private orderservice m_order = NULL;
Private queryservice m_query = NULL;
Private foundationservice m_found = NULL;

/// <Summary>
///
/// </Summary>
Public xmlnode xmldimension
{
Get
{
If (httpcontext. Current. cache ["dimension"] = NULL)
{
Loadxmldimension ();
}
Return (xmlnode) httpcontext. Current. cache ["dimension"];
}
}
/// <Summary>
/// Query the Template
/// </Summary>
Public xmlnode xmlquerylist
{
Get
{
If (httpcontext. Current. cache ["querylist"] = NULL)
{
Loadxmlquerylist ();
}
Return (xmlnode) httpcontext. Current. cache ["querylist"];
}
}

/// <Summary>
/// JavaScript Template
/// </Summary>
Public xmlnode xmljavascript
{
Get
{
If (httpcontext. Current. cache ["JavaScript"] = NULL)
{
Loadxmljavascript ();
}
Return (xmlnode) httpcontext. Current. cache ["JavaScript"];
}
}
/// <Summary>
/// Province Template
/// </Summary>
Public xmlnode xmlprovince
{
Get
{
If (httpcontext. Current. cache ["Province"] = NULL)
{
Loadxmlprovince ();
}
Return (xmlnode) httpcontext. Current. cache ["Province"];
}
}
# Region multi-role Template
/// <Summary>
/// System role Template
/// </Summary>
Public xmlnode xmltemplate
{
Get
{
Xmlnode = NULL;
If (httpcontext. Current. Request. Cookies ["user"]! = NULL)
{
String swebsitetype = httpcontext. Current. Request. Cookies ["user"] ["websitetype"];

If (swebsitetype = "0 ")
{
Xmlnode = xmlcentertemplate;
}
If (swebsitetype = "1 ")
{
Xmlnode = xmlprovincetemplate;
}
If (swebsitetype = "2 ")
{
Xmlnode = xmlcitytemplate;
}

}
Return xmlnode;
}
}

Public xmlnode xmlcentertemplate
{
Get
{
If (httpcontext. Current. cache ["centertemplate"] = NULL)
{
Loadxmlcentertemplate ();
}
Return (xmlnode) httpcontext. Current. cache ["centertemplate"];
}
}
Public xmlnode xmlprovincetemplate
{
Get
{
If (httpcontext. Current. cache ["provincetemplate"] = NULL)
{
Loadxmlprovincetemplate ();
}
Return (xmlnode) httpcontext. Current. cache ["provincetemplate"];
}
}
Public xmlnode xmlcitytemplate
{
Get
{
If (httpcontext. Current. cache ["citytemplate"] = NULL)
{
Loadxmlcitytemplate ();
}
Return (xmlnode) httpcontext. Current. cache ["citytemplate"];
}
}
/// <Summary>
/// Add role permission Template
/// </Summary>
Private void loadxmlcentertemplate ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "centertemplate. config ";
Doc. Load (Spath );
Xmlnode = Doc. documentelement;
Httpcontext. Current. cache. insert ("centertemplate", Doc. documentelement,
New cachedependency (Spath ));
}
Private void loadxmlprovincetemplate ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "provincetemplate. config ";
Doc. Load (Spath );
Xmlnode = Doc. documentelement;
Httpcontext. Current. cache. insert ("provincetemplate", Doc. documentelement,
New cachedependency (Spath ));
}
Private void loadxmlcitytemplate ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "citycentertemplate. config ";
Doc. Load (Spath );
Xmlnode = Doc. documentelement;
Httpcontext. Current. cache. insert ("citytemplate", Doc. documentelement,
New cachedependency (Spath ));
}
# Endregion
/// <Summary>
/// System Service
/// </Summary>
Public systemservice
{
Get
{
Return m_system;
}
}
/// <Summary>
/// Query the service
/// </Summary>
Public queryservice
{
Get
{
Return m_query;
}
}
/// <Summary>
/// Basic Information
/// </Summary>
Public foundationservice
{
Get
{
Return m_found;
}
}
/// <Summary>
/// Order Management
/// </Summary>
Public orderservice
{
Get
{
Return m_order;
}
}
Public systemvm ()
{
Loadbusinessobj ();
}
/// <Summary>
/// Return the template type that can be selected by the system based on the template type.
/// </Summary>
/// <Param name = "Stype"> </param>
/// <Returns> </returns>
Public xmlnodelist getxmljavascriptbytype (string stype)
{
Xmlnodelist = xmljavascript. selectnodes ("// item [@ type = '" + stype + "']");
If (xmlnodelist. Count <0)
{
Throw new exception ("the node of Type [" + stype + "] cannot be found! ");
}
Return xmlnodelist;
}
/// <Summary>
/// Obtain the Javascript template based on the Template Name
/// </Summary>
/// <Param name = "sname"> </param>
/// <Returns> </returns>
Public String getxmljavascripttpl (string sname)
{
Stringbuilder sjstemplate = new stringbuilder ("");
Xmlnode = xmljavascript. selectsinglenode ("// item [@ name = '" + sname + "']");
If (xmlnode = NULL)
{
Throw new exception ("the node named [" + sname + "] cannot be found! ");
}
String Spath = toolfunc. getxmlstring (xmlnode, "@ template ");
Spath = appdomain. currentdomain. basedirectory + Spath;
Using (textreader sr = file. opentext (Spath ))
{
String line;
While (line = Sr. Readline ())! = NULL)
{
Sjstemplate. appendline (line );
}
}
Return sjstemplate. tostring ();
}
Public String geturlfromjavascript (string sname)
{
String Surl = "#";
Xmlnode = xmljavascript. selectsinglenode ("// item [@ name = '" + sname + "']");
If (xmlnode = NULL)
{
Throw new exception ("the node named [" + sname + "] cannot be found! ");
}
Xmlelement El = (xmlelement) xmlnode;
Surl = El. getattribute ("url ");
Return Surl;
}
Private void loadxmlprovince ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "province. config ";
Doc. Load (Spath );
Xmlnode = Doc. documentelement;
Httpcontext. Current. cache. insert ("Province", Doc. documentelement,
New cachedependency (Spath ));
}
Private void loadxmljavascript ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "javascript. config ";
Doc. Load (Spath );
Httpcontext. Current. cache. insert ("JavaScript", Doc. documentelement,
New cachedependency (Spath ));
}
Private void loadxmldimension ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "dimension. config ";
Doc. Load (Spath );
Httpcontext. Current. cache. insert ("dimension", Doc. documentelement,
New cachedependency (Spath ));
}
/// <Summary>
///
/// </Summary>
Private void loadxmlquerylist ()
{
String strbasepath = appdomain. currentdomain. basedirectory;
Xmldocument Doc = new xmldocument ();
String Spath = strbasepath + @ "template \" + "querylist. config ";
Doc. Load (Spath );
Httpcontext. Current. cache. insert ("querylist", Doc. documentelement,
New cachedependency (Spath ));
}

/// <Summary>
/// Load the Business Object
/// </Summary>
Private void loadbusinessobj ()
{
M_system = new systemservice (this );
M_order = new orderservice (this );
M_found = new foundationservice (this );
M_query = new queryservice (this );
}

In this case, you can use the metadata model to match the permissions with the template based on the role record of the database to achieve a great architecture design.

 

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.