使用屬性和反射過渡從資料存取層到業務物件 - III

來源:互聯網
上載者:User
資料 簡介
本系列的最後一篇文章.第一部分如何描述,二部分如何取得描述.現在我們就要建立DAL庫來使我們的標題可行.

設計DAL庫
我想建立的類庫支援Sqlserver 和oledb.我把庫分成了以下的部分:

Utility classes
class DALQueryBuilder
產生sql語句更新物件.

class DALParameter
產生參數儲存在預存程序中.

class DALException
繼承於System.Exception , 資料庫有異常時將會提供更多的資訊.

Attribute classes
參見第一篇.

DAL 本身
class DALEngine
這個抽象的類用於資料庫操作,是資料庫程式更加簡單.它的虛擬和抽象的方法有不同的實施.

class DALSqlEngine


class DALOleDbEngine
耬一眼DALEngine 類
public abstract class DALEngine : IDisposable
{
//
// private data members
//
IDbConnection conn = null;
string connectionString = "";
ArrayList parameters = new ArrayList();
bool canClose = true;


// constructor
public DALEngine(string connectionString);

public bool CanClose;
public string ConnectionString;


protected IDbConnection Connection;
protected ArrayList Parameters;

public void Close();
public void Dispose();


//
// methods that must be override with a specific data provider
// implementation please see the implementation of DALSqlEngine
// or DALOleDbEngine
//
protected abstract IDbConnection GetConnection();
protected abstract IDbCommand CreateCommand(string spName);
public abstract void ExecSP_DataSet(string spName, DataSet dataSet,
string tableName);
public abstract void ExecQuery_DataSet(string query, DataSet dataSet,
string tableName);


//
// related to stored procedure parameters
//
public DALParameter GetParameter(string name);
void UpdateOutputParameters(IDbCommand cmd);
public void AddParameter(DALParameter param);
public void ClearParameters();


//
// for those that use stored procedures
//
public IDataReader ExecSP_DataReader(string spName);
public IDataReader ExecSP_DataReader(string spName,
CommandBehavior behavior);
public object ExecSP_Scalar(string spName);
public int ExecSP_NonQuery(string spName);


//
// methods for those that use plain SQL statements
//
public IDataReader ExecQuery_DataReader(string query,
CommandBehavior behavior);
public IDataReader ExecQuery_DataReader(string query);
public object ExecQuery_Scalar(string query);
public int ExecQuery_NonQuery(string query);




//
// Business objects methods
//
public static object CreateFromReader(IDataReader reader, Type objType);
public object RetrieveObject(object keyValue, Type objType);
public int RetrieveChildObjects(object foreignKeyValue, ArrayList objects,
Type childType);
void UpdateObjectSql(object o, DataTableAttribute dataTable);
void UpdateObjectStoredProcedure(object o, DataTableAttribute dataTable);
public void UpdateObject(object o);
public void UpdateObjects(IEnumerable enumObjects);
}

public class DAL : DALSqlEngine
{
const string CONN_STRING = "server=localhost;uid=sa;pwd=;database=pubs";

public DAL() : base(CONN_STRING)
{

}

public ArrayList GetCustomerDependents(Customer customer)
{
ArrayList result = new ArrayList();

RetrieveChildObjects(customer.Id, result, typeof(CustomerDependent));

return result;
}

public void UpdateCustomerDependents(Customer customer)
{
UpdateObjects(customer.Dependents);
}
}
看個例子:

public static void Main()
{

DAL dal = new DAL();

try
{

Contact contact = new Contact();
contact.Name = "Joao Cardoso";
contact.Age = 23;
contact.Address = "Av. Rio Branco, 202/121";
contact.Address2 = "Centro";
contact.PostalCode = "09029-901";
contact.City = "Sao Paulo";
contact.State = "SP";
contact.Country = "Brazil";

dal.UpdateObject(contact);
Console.WriteLine(contact);


Contact joaoCardoso = (Contact)dal.RetrieveObject(1, typeof(Contact));
joaoCardoso.Age++;
Console.WriteLine(joaoCardoso);
Console.WriteLine("");


Customer customer = new Customer();
customer.Name = "Paul Noyter";
customer.Age = 34;
customer.Address = "All St, 2202/2121";
customer.Address2 = "Downville";
customer.PostalCode = "90931";
customer.City = "Los Angeles";
customer.State = "CA";
customer.Country = "United States";
customer.TotalPurchased += 1900.87M;
customer.NumberOfPurchases++;

dal.UpdateObject(customer);


Customer paul = (Customer)dal.RetrieveObject(1, typeof(Customer));
Console.WriteLine(paul);

paul.TotalPurchased += 100M;
paul.NumberOfPurchases++;
dal.UpdateObject(paul);

if (paul.Dependents.Count == 0)
{
CustomerDependent dependent = paul.NewDependent();
dependent.Name = "Marie Noyter";
dependent.Age = 31;
paul.Dependents.Add(dependent);


dependent = paul.NewDependent();
dependent.Name = "Mark Noyter";
dependent.Age = 10;
paul.Dependents.Add(dependent);


dependent = paul.NewDependent();
dependent.Name = "Claudia Snorg";
dependent.Age = 32;
dependent.Relationship = CustomerRelationship.Friend;
paul.Dependents.Add(dependent);

dal.UpdateCustomerDependents(paul);
}
else
{
Console.WriteLine("Dependents of {0}", paul.Name);

foreach(CustomerDependent dependent in paul.Dependents)
{
Console.WriteLine("<Dependent>{0} - {1} [{2}]", dependent.Id,
dependent.Name, dependent.Relationship);
dependent.Relationship = CustomerRelationship.Family;
}

dal.UpdateCustomerDependents(paul);
}

}
finally
{
dal.Dispose();
}
}
Conclusion
有老多局限性,需要我們去進一步實施思考,但是你理解了這些以後,我們就可以進行nhibernate的理解研究和應用了,祝你好運.




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.