petshop是C#實現的petstore,具體和技術無關的情況就不多介紹了。
現在基於.Net 2.0的PetShop4.0爲止,整個設計逐漸變得成熟而優雅,卻又很多可以借鑑之處。PetShop是一個小型的項目,系統架構與代碼都比較簡單,卻 也凸現了許多頗有價值的設計與開發理念。petShop4.0解剖
petshop3.0比petshop1和2都有了較大的改變,主要是設計方面的。看一下里面的8個工程和1個站點就知道它肯定分了不少層。
一.概況介紹。
Model:
模型層,封裝業務實體,一般和數據庫模式對應。
例如:
public class AccountInfo {
// Internal member variables
private string _userId;
private string _password;
private string _email;
private AddressInfo _address;
private string _language;
private string _category;
private bool _showFavorites;
private bool _showBanners;
。。。
}
// Internal member variables
private string _userId;
private string _password;
private string _email;
private AddressInfo _address;
private string _language;
private string _category;
private bool _showFavorites;
private bool _showBanners;
。。。
}
數據訪問接口層,主要是一些dao接口。
例如:
public interface IAccount
{
AccountInfo SignIn(string userId, string password);
AddressInfo GetAddress(string userId);
void Insert(AccountInfo account);
void Update(AccountInfo Account);
}
{
AccountInfo SignIn(string userId, string password);
AddressInfo GetAddress(string userId);
void Insert(AccountInfo account);
void Update(AccountInfo Account);
}
OracleDAL:
oracle實現的數據訪問層。
SQLServerDAL:
sql實現的數據訪問層。
OracleDAL和SQLServerDAL中的類都實現了IDAL中的接口。屬於dao實現。
DALFactory
負責確定是使用oracle實現還是mssql實現。通過在web.config中的配置確定使用哪一個dal實現(通過反射,動態生成訪問類是PetShop.SQLServerDAL還是PetShop.OracleDAL命名空間中的類)。
<add key="WebDAL" value="PetShop.SQLServerDAL"/>
<add key="OrdersDAL" value="PetShop.SQLServerDAL"/>
<add key="OrdersDAL" value="PetShop.SQLServerDAL"/>
public class Account
{
public static PetShop.IDAL.IAccount Create()
{
/// Look up the DAL implementation we should be using
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Account";
// Using the evidence given in the config file load the appropriate assembly and class
return (PetShop.IDAL.IAccount) Assembly.Load(path).CreateInstance(className);
}
}
{
public static PetShop.IDAL.IAccount Create()
{
/// Look up the DAL implementation we should be using
string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
string className = path + ".Account";
// Using the evidence given in the config file load the appropriate assembly and class
return (PetShop.IDAL.IAccount) Assembly.Load(path).CreateInstance(className);
}
}
BLL:
業務訪問層。通過DALFactory,讀取配置,決定使用何種dal實現。
public class Account {
public AccountInfo SignIn(string userId, string password) {
if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
return null;
// 通過DALFactory調用具體的dal實現。
IAccount dal = PetShop.DALFactory.Account.Create();
// Try to sign in with the given credentials
AccountInfo account = dal.SignIn(userId, password);
// Return the account
return account;
}
。。。
}
public AccountInfo SignIn(string userId, string password) {
if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
return null;
// 通過DALFactory調用具體的dal實現。
IAccount dal = PetShop.DALFactory.Account.Create();
// Try to sign in with the given credentials
AccountInfo account = dal.SignIn(userId, password);
// Return the account
return account;
}
。。。
}
表現層,主要包括了Web 頁面(aspx)和用戶控件(ascx)控件及自定義服務器控件SimplePager和ViewStatePager。
Utility:
公用模塊,一組幫助器類,其他業務層和數據訪問層可能會使用到的一些公用方法。