ASP.NET已經出來了有好幾年了,但是在開發模式上還是使用著傳統的Form模式,沒有成熟的MVC架構的支援。這種模式的缺點是很明顯的,代碼可重用性低,可擴性差,日後的修改和維護麻煩。
為了開發的需要,我設計了一個MVC模式,大家一起交流、探討,設計得不好的地方還請各位不吝指出。
MVC的模式結構如:
Code
using System;
using System.Text;
using System.Data;
namespace AccessHelper
{
public interface IHelper
{
DataTable ExecuteReader(string connectionString, CommandType cmdType, string cmdText);
int ExecuteNoneQuery(string connectionString, CommandType cmdType, string cmdText);
object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText);
void AddParameter(string key, object value);
}
}
DbHelper模組IHelper介面的定義如下:
IHelper用於直接執行SQL語句,它的三個主要的方法是根據Command對象的三個方法來劃分的。其中ExecuteReader方法返回的是DataTable類型,使用DataAdapter把資料Fill到DataTable中。這樣做的好處是儘早斷開資料庫的串連,釋放資源。
DbDAO模組根據具體的Model類來定義,它的作用是把從DbHelper得來的DataTable轉換成具體的類。DbDAO通過參數DataBaseType參數,使用DBHelperFactory工廠類執行個體化相應的IHelper類。
DbDAL模組用於處理各種的資料訪問邏輯,如登入,註冊等等。