开源SmallData轻量数据访问组件
这个组件是从NClay.Data基础功能分离出来,主要功能以下功能:一致的数据库访问方式,基于接口的数据库提供者扩展,基于线程的连接对象句柄可以简单实现跨方法的数据库事务处理能力;组件配备6个数据库连接设置通过DBContext的不同版本的Region方法来进行不同数据库操作。
组件才用Apache License 2.0协议。
辅助工具:迟下将会提供一个Code Generator用于实现简单描述XML生成基于组件的Entity数据操作方式,在代码例程用使用该Code Generator的简单测试(由于这东西并不是组件必须,所以暂时不提供;不过有需要的朋友可以联系我)
组件结构和处理图
普通代码操作方式
DBContext.Region(e => {
Command cmd = new Command("select * from employees");
Console.WriteLine("Select Employee:");
using (IDataReader reader = e.ExecuteReader(cmd))
{
while (reader.Read())
{
Console.WriteLine("\t"+reader["firstname"]);
}
}
Console.WriteLine("Select Orders:");
cmd = new Command("select * from orders");
using (IDataReader reader = e.ExecuteReader(cmd))
{
while (reader.Read())
{
Console.WriteLine("\t" + reader["orderid"] +"\t" + reader["orderdate"]);
}
}
cmd = new Command("select * from orders where orderdate >=@p1 and orderdate<@p2");
cmd.AddParameter("p1", "1996-1-1");
cmd.AddParameter("p2", "1997-1-1");
using (IDataReader reader = e.ExecuteReader(cmd))
{
while (reader.Read())
{
Console.WriteLine("\t" + reader["orderid"] + "\t" + reader["orderdate"]);
}
}
});
基于Code Generator的操作方式
DBContext.Region(e =>
{
int id;
WhereBody exp;
Employees emp = Employees.Load(e, 3);
Console.WriteLine(emp.FirstName +"\t" + emp.LastName);
emp.LastName = "henry";
emp.Save(e);
emp = Employees.Load(e, 3);
Console.WriteLine(emp.FirstName + "\t" + emp.LastName);
emp = new Employees();
emp.FirstName = "fan";
emp.LastName = "henry";
emp.Save(e);
id = emp.EmployeeID;
emp = Employees.Load(e,id);
Console.WriteLine(emp.FirstName + "\t" + emp.LastName);
emp.Delete(e);
emp = Employees.Load(e, id);
Console.WriteLine(emp == null);
IList<CountOrderByMonth> items = CountOrderByMonth.List(e, null, null, null);
foreach(CountOrderByMonth item in items)
{
Console.WriteLine(item.Year.ToString("0000")+item.Month.ToString("00") + ":\t" + item.Count);
}
exp = new WhereBody();
exp.AddExpression("Employeeid =@p1");
exp.AddParameter("p1", 3);
items = CountOrderByMonth.List(e, exp, null, null);
foreach (CountOrderByMonth item in items)
{
Console.WriteLine(item.Year.ToString("0000") + item.Month.ToString("00") + ":\t" + item.Count);
}
});