使用属性和反射过渡从数据存取层到业务物件 - III
2024-07-21 02:23:12
供稿:网友
简介
本系列的最后一篇文章.第一部分如何描述,二部分如何取得描述.现在我们就要创建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的理解研究和应用了,祝你好运.