HelloData 开源net Orm框架
写在新工作之前的一个下雨天。HelloData 开源net Orm框架。自创,希望大家喜欢。
HelloData 欢迎您。
hello world是我们新入软件这行必敲得一段字母,深得人心。HelloData是小型的ORM框架,想在开源的时间立一足,必须得到大家的认可才行。
现提供各种书写方法,大家有想法就说,文章末会提供源码的下载。
HelloData.FrameWork:为数据库底层框架,支持多种数据库操作,加入了BaseEntity与BaseLogic,BaseManager两个有关业务逻辑的继承方式。在数据库生成model的时候使用T4生成,
并且生成的数据库表对应的对象类为部分类(partial),如果需要扩展加入当前对象的多个部分类即可。这样做的好处是将数据库生成
的类与业务间的操作分离。BaseLogic的继承BaseLogic<T>,T为操作数据库表对象的泛型,里面包含的常用的新增,删除,修改,获取一个实体,获取实体list
,BaseManager<T, TU>,T为操作逻辑类,TU为操作逻辑对象类。继承后当前操作逻辑类为全局唯一实例,使用了单一模式,操作方法也是包含了那些常用的逻辑操作。
在gloab.cs或者program.cs里面加入全局的操作:
// 启动日志模块,支持log4net,nlog,console
Logger.Current.SetLogger = new ConsoleLog();
Logger.Current.IsOpenLog = true ;
Logger.CurrentLog.Info( " INSTALLING " );
// 设置数据库连接执行状况
AppCons.LogSqlExcu = true ;
// 设置第一个数据库
AppCons.SetDefaultConnect( new SQLliteHelper(), ConfigurationManager.AppSettings[ " ConnectionString1 " ]);
// 设置第二个数据库
// AppCons.SetSecondConnect(new MySqlHelper(), ConfigurationManager.AppSettings["ConnectionString1"]);
// 设置更多个数据库
// AppCons.SetMoreConnect(new SQLliteHelper(), ConfigurationManager.AppSettings["ConnectionString2"]);
// 是否需要数据库全局参数化
AppCons.IsParmes = false ;
// 是否数据库操作的缓存
AppCons.IsOpenCache = false ;
// 使用第三方的分布式缓存
// AppCons.CurrentCache =new RedisCache();
// 使用内置的webcache缓存
AppCons.CurrentCache = new WebCache();
新增的例子:
1 // demo 1:
2 cms_userManager.Instance.Save( new cms_user()
3 {
4 username = " test " + DateTime.Now.Millisecond,
5 password = " 123456 " ,
6 phone = "" ,
7 isadmin = true
8 });
9 // demo 2:
10 using (InserAction action = new InserAction( " cms_user " ))
11 {
12 action.SqlKeyValue(cms_user.Columns.username, model.username);
13 action.SqlKeyValue(cms_user.Columns.password, model.password);
14 return action.Excute().ReturnCode;
15 }
16 // demo 3:
17 using (InserAction action = new InserAction(model))
18 {
19 return action.Cast<cms_user> ()
20 .SqlInValues(user =>
21 new List<cms_user> ()
22 {
23 new cms_user {password = " 12345 " , phone = " 123456 " },
24 new cms_user {phone = " 123242343 " }
25 }
26 ).UnCast().Excute().ReturnCode;
27 }
修改的例子:
// demo 1:
using (UpdateAction update = new UpdateAction(Entity))
{
update.SqlKeyValue(cms_user.Columns.createtime, null );
update.SqlKeyValue(cms_user.Columns.password, " 123456123 " );
update.Cast <cms_user> ()
.SqlValue(user => new cms_user { password = " 123456 " , createtime = DateTime.Now })
.Where(user1 => user1.isadmin)
.UnCast().Excute();
return update.ReturnCode;
}
// demo 2:
cms_userManager.Instance.Update( new cms_user()
{
username = " test " + DateTime.Now.Millisecond,
password = " 123456 " ,
phone = "" ,
isadmin = true ,
// 主键一定要加入
id= 12
});
查找的例子:
// demo 1:
using (SelectAction select = new SelectAction(Entity))
{
select .SqlWhere(cms_user.Columns.username, " 1 " , " 2 " , ConditionEnum.And, RelationEnum.Between)
.SqlWhere(cms_user.Columns.password, password)
.SqlWhere(cms_user.Columns.isactive, true )
.SqlOrderBy(cms_user.Columns.createtime,OrderByEnum.Desc)
.SqlPageParms( 1 );
return select .QueryEntity<cms_user> ();
}
// demo 2:
using (SelectAction select = new SelectAction(Entity))
{
select .SqlWhere(cms_user.Columns.id, id, RelationEnum.LikeRight);
select .SqlWhere(cms_user.Columns.isactive, true );
select .SqlPageParms( 1 );
select .Cast<cms_user> ().
OrderBy(user => user.logintime, OrderByEnum.Asc)
.OrderBy(ui => ui.phone, OrderByEnum.Asc)
.GroupBy(u => new object [] { u.isadmin, u.logintime })
.Where(o => o.password == " 12321 " && o.logintime == DateTime.Now)
; // .UnCast();
return select .QueryEntity<cms_user> ();
}
未完待续。
HelloData之:数据库model的生成。
下载地址: https://github测试数据/xiaose1205/HelloData
标签: HelloData
作者: Leo_wl
出处: http://HdhCmsTestcnblogs测试数据/Leo_wl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
版权信息查看更多关于HelloData 开源net Orm框架的详细内容...