分享
 
 
 

使用属性和反射过渡从数据存取层到业务物件 - III

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

简介

本系列的最后一篇文章.第一部分如何描述,二部分如何取得描述.现在我们就要创建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的理解研究和应用了,祝你好运.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有