設計模式的項目----權限管理系統

 
本系統是以B/S模式架構的基於角色控制的權限管理系統,系統由本人跟標仔兩人共同開發,開發工具爲vs.net2003,開發語言爲C#,可用於綜合性網站的統一權限管理。
基於解決方案的需要,主要用到了以下五種設計模式:
爲了支持對不同數據庫的訪問,用到抽象工廠設計模式
爲了產生單一工廠,用到單件設計模式
爲用戶提供統一的高層接口,封裝業務邏輯,使用外觀設計模式
爲了實現用戶對密碼加密的不同需求,用到策略模式
子系統使用該權限系統,不需要集成該系統,直接使用系統提供的WebService訪問,使用代理模式,實現鬆散的耦合。
 
類模型圖
 
數據庫模型圖
 
權限管理系統整體架構

 
本系統是基於經典的三層模式擴展的N層模
式,其中:
Web層即用戶界面層,跟用戶打交道的;
BLLFacade層即業務邏輯外觀層,定義了
一個高層接口, Web層只需要調用該層提
供的簡單接口;
BLL層即業務邏輯層,核心的業務邏輯代碼都在此模塊中;
IDAL層即數據訪問對象接口層,爲BLL層
即業務邏輯層提供一致的數據訪問接口;
OracleDAL層即Oracle數據庫訪問對象層,實現IDAL層的所有接口;
SQLServerDAL層即SQLServer數據庫訪問對象層,實現IDAL層的所有接口;
Model層即數據實體層;
Utility層即通用工具類層,相當於一個工具包;
 
下面組件圖顯示了各個項目的調用關係
  
在本系統中,要創建的數據訪問對象包括Users UserRoles Roles RolePowers Powers, Categories。在設計中,這些對象已經被抽象爲對應的接口,而其實現則根據數據庫的不同而有所不同。
 
 
                                                    .......
 
也就是說,創建的對象有多種類別,而每種類別又有不同的實現,這是典型的抽象工廠模式的應用場景。可以通過抽象工廠模式來解決。標準的抽象工廠模式類圖如下:
using System;
namespace PowerManage
{
 /// <summary>
 /// 抽象工廠設計模式,用到了單例設計模式
 /// </summary>
 public abstract  class DALFactory
 {
  private static   DALFactory Instance=null;
  private static  readonly string DBProvider = System.Configuration.ConfigurationSettings.AppSettings["DBProvider"];
     //構造函數爲保護函數,確保抽象工產只有一個實例
  protected   DALFactory()
  {
  }
  //獲取抽象工程的唯一實例
  public static DALFactory  GetInstance()
  {
   if(Instance==null)
   {
    switch(DBProvider)
    {
     case "SQLServerDB":
      Instance = new SQLServerDALFactory();
      break;
     case "OracleDB":
      Instance = new OracleDALFactory();
      break;
     default:
      Instance = new SQLServerDALFactory();
      break;
    }
   }
   return  Instance;
  }
  //創建Categories數據層接口
  public abstract  PowerManage.IDAL.ICategories CreateCategories();
  //創建Powers數據層接口
  public abstract  PowerManage.IDAL.IPowers CreatePowers();
  //創建RolePowers數據層接口
  public abstract  PowerManage.IDAL.IRolePowers CreateRolePowers();
  //創建Roles數據層接口
  public abstract  PowerManage.IDAL.IRoles CreateRoles();
  //創建UserRoles數據層接口
  public abstract  PowerManage.IDAL.IUserRoles CreateUserRoles();
  //創建Users數據層接口
  public abstract  PowerManage.IDAL.IUsers CreateUsers();
 }
}
  
Oracle數據庫訪問對象工廠
public class OracleDALFactory:DALFactory
{
      //創建Categories數據層接口
    public override PowerManage.IDAL.ICategories CreateCategories()
    {
           return new PowerManage.OracleDAL.Categories();
    }
       ………………
}
SQLServer數據庫訪問對象工廠
public class SQLServerDALFactory:DALFactory
{
       //創建Categories數據層接口
     public override PowerManage.IDAL.ICategories CreateCategories()
     {
            return new PowerManage.SQLServerDAL.Categories();
     }
              ………………
}
  例如,BLL層創建SQL Server數據庫的的Users對象如下:
DALFactory factory = new DALFactory.GetInstance();
private static readonly IUsers users = factory CreateUsers();
 
   權限管理系統部署好數據庫後,程序一旦執行,數據訪問工廠就必須確定,而Singleton設計模式是告訴您如何在你的應用程序創建一個唯一類實例的全局對象,也就是說,這個對象只能被實例化一次,這個對象同時提供一個訪問它的一個全局的訪問點。這裏的數據訪問工廠只能被實例化一次。
標準的單件模式類圖如下
策略模式
 /// <summary>
 /// 加密策略接口
 /// </summary>
 public interface EncryptStrategy
 {
  //對字符串進行密鑰加密
   string Encrypting(string Source, string Key);
  /// <summary>
  /// 對字符串進行密鑰解密
  /// </summary>
   string Decrypting(string Source, string Key) ;
 }
外觀模式
 
代理模式
  
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;

/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="UserSoap", Namespace="http://tempuri.org/")]
public class WebServiceUserProxy : System.Web.Services.Protocols.SoapHttpClientProtocol
{
   
 /// <remarks/>
 public WebServiceUserProxy()
 {
  this.Url = "http://localhost/Web/Service/User.asmx";
 }
   
 /// <remarks/>
 [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Login", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
 public string Login(string UserName, string Password, string Category)
 {
  object[] results = this.Invoke("Login", new object[] {
                 UserName,
                 Password,
                 Category});
  return ((string)(results[0]));
 }
   
 /// <remarks/>
 public System.IAsyncResult BeginLogin(string UserName, string Password, string Category, System.AsyncCallback callback, object asyncState)
 {
  return this.BeginInvoke("Login", new object[] {
                UserName,
                Password,
                Category}, callback, asyncState);
 }
   
 /// <remarks/>
 public string EndLogin(System.IAsyncResult asyncResult)
 {
  object[] results = this.EndInvoke(asyncResult);
  return ((string)(results[0]));
 }
   
 /// <remarks/>
 [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/CheckPower", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
 public string CheckPower(string UserName, string Password, string Category, string Power)
 {
  object[] results = this.Invoke("CheckPower", new object[] {
                   UserName,
                   Password,
                   Category,
                   Power});
  return ((string)(results[0]));
 }
   
 /// <remarks/>
 public System.IAsyncResult BeginCheckPower(string UserName, string Password, string Category, string Power, System.AsyncCallback callback, object asyncState)
 {
  return this.BeginInvoke("CheckPower", new object[] {
                  UserName,
                  Password,
                  Category,
                  Power}, callback, asyncState);
 }
   
 /// <remarks/>
 public string EndCheckPower(System.IAsyncResult asyncResult)
 {
  object[] results = this.EndInvoke(asyncResult);
  return ((string)(results[0]));
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章