非关系型数据库来了,CRL快速开发框架升级到版本4

时间:2023-03-08 18:01:47

*?,我很任性,我要造不一样的*,同时支持关系型和非关系型的框架有没有

新版数据查询作了些调整,抽象了LabmdaQueryy和DBExtend,升级到版本4,非关系数据库MongoDB被支持了!

最近又好多人发各种框架,谁的好,用谁的?

个人意见,想标准和稳定,EF,NH就行了,要个性,选一款好用的*吧

个人开发的框架始终是高级玩具,学习的好东西,不要较真,欢迎下载此高级玩具,猛击底部GITHUB地址

历史版本3介绍 CRL快速开发框架开源完全转到Github

当前版本结构更改如下

非关系型数据库来了,CRL快速开发框架升级到版本4

一句话描述,非关系型数据库Mongodb能用CRL进行统一管理了,不用再写各种各样的实现

此功能依赖MongoDB官方驱动MongoDB.Driver

简单示例

对象定义MongoDBModel.cs

public class MongoDBModel:CRL.IModel
{
public MongoDBModel()
{
//保持唯一
Id = new Guid();
}
public Guid Id
{
get;
set;
}
public string OrderId
{
get;
set;
}
public int Status
{
get;
set;
}
}

管理类实现MongoDBTestManage

    public class MongoDBTestManage : CRL.BaseProvider<MongoDBModel>
{
public static MongoDBTestManage Instance
{
get { return new MongoDBTestManage(); }
}
}

数据连接创建

CRL.SettingConfig.GetDbAccess = (dbLocation) =>
{
//可按type区分数据库
var type2 = dbLocation.ManageType;
if (type2 == typeof(Code.MongoDBTestManage))
{
//实现MongoDB连接
return new CoreHelper.MongoDBHelper("mongodb://localhost:27017", "test2");
}
return WebTest.Code.LocalSqlHelper.TestConnection;
};

创建访问对象

var instance = Code.MongoDBTestManage.Instance;

插入数据

instance.Add(new Code.MongoDBModel() { OrderId = "1212", Status = DateTime.Now.Second });

函数Count统计

int count = instance.Count(b => b.Status >= 0);

Group

var query = instance.GetLambdaQuery();
//group
query.GroupBy(b => b.OrderId).Select(b => new { count = b.Status.SUM(), count2 = b.Status.COUNT() });
var list = query.ToDynamic();
foreach (var item in list)
{
var a = item.count;
var key = item.OrderId;
}

标准查询

除了代表SQL特性的语法和函数不支持,其它都支持

            var query2 = instance.GetLambdaQuery();
query2.Select(b => new { aa = b.Id, bb = b.Status });
query2.Where(b=>b.Status.In(1,2,3,4)&&b.OrderId.StartsWith("123"));//支持扩展方法
var result = query2.ToDictionary<Guid, int>();//返回字典
var result2 = query2.ToDynamic();//返回动态对象
var result3 = query2.ToList();//返回List<MongoDBModel>

更新删除和之前调用方式保持一致

由于MongoDB的特性,以下不能实现,调用可能会抛出异常

  • 关联查询
  • 关联删除
  • 关联更新
  • SQL语句查询
  • 事务
  • 存储过程
  • 自动编译
  • 部份SQL函数

测试用例见开发文档 /page/MongoDB.aspx

void TestMongoDB()
{
//依赖官方驱动MongoDB.Driver
//MongoDBTest.Test();
//return;
var instance = Code.MongoDBTestManage.Instance;
//插入
instance.Add(new Code.MongoDBModel() { OrderId = "1212", Status = DateTime.Now.Second });
//函数Count
int count = instance.Count(b => b.Status >= 0);
var query = instance.GetLambdaQuery();
query.Where(b => b.Status > 10);
var result3 = query.ToList();//返回List<MongoDBModel>
//group
query.GroupBy(b => b.OrderId).Select(b => new { count = b.Status.SUM(), count2 = b.Status.COUNT() });
var list = query.ToDynamic();
foreach (var item in list)
{
var a = item.count;
var key = item.OrderId;
}
//标准查询
var query2 = instance.GetLambdaQuery();
query2.Select(b => new { aa = b.Id, bb = b.Status });
//query2.Where(b=>b.Status.In(1,2,3,4));
var result = query2.ToDictionary<Guid, int>();//返回字典
var result2 = query2.ToDynamic();//返回动态对象 //删除
instance.Delete(b => b.Status == 111);
//更新
var item2 = instance.QueryItem(b => b.Status > 0);
item2.Status = 123;
instance.Update(item2);
}

  

项目开源地址:https://github.com/hubro-xx/CRL3