继上次使用CRL实现大数据分库分表方案升级到2.4,时隔不久又升级到了大版本号3.0,主要是因为结构发生了一些更改
ORM和业务封装Package分开了,增加了实例项目演示代码CRLShoppingDemo,整体项目结构如下
3.0更新内容如下:
2015-09-26 升级改到3.0版,2.X版不再更新 2015-09-28 Model索引值类型改为dynamic 减少QueryList方法参数 2015-10-08 重载了BaseProvider 增加了in select查询 2015-10-10 增加Equals扩展方法解析 2015-10-15 优化了PackageTrans方法,去掉暴露在外面的事务控制方法 去掉了SetContext方法,使用TransactionScope实现整体事务 账户系统去掉SQL写法,改为对象 2015-10-20 修改了创建表时数据初始,改为在Model实现GetInitData 修改了缓存null判断 数据访问对象dbHelper改名为DBExtend 2015-10-23 更改字段别名返回和处理,不需专门传映射关系
QueryFromAllCache方法名 改为QueryFromCache 增加CRL演示项目
以上更新在3.0开发文档中都有体现
主要功能更新讲解
1.In select 创建一个等效的关联in 查询,也有相对的Not In
//等效为 product.UserId in(select UserId from order where product.SupplierId=10 and order.status=2) query.In<Code.Order>(b => b.UserId, b => b.UserId, (a, b) => a.SupplierId == "10" && b.Status == 2);
2.Equals 扩展方法,类型不同的字段和参数,能用此方法进行相等判断了,省去了转换方法
query.Where(b => b.UserId.Equals(Code.ProductChannel.其它));//按值等于,enum等于int
3.统一事务调用方法PackageTrans,不用再手动回滚了
public bool TransactionTest(out string error) { //简化了事务写法,自动提交回滚 return PackageTrans((out string ex) => { ex = ""; var product = new ProductData(); product.BarCode = "sdfsdf"; product.Number = 10; ProductDataManage.Instance.Add(product); //return false; //会回滚 product = new ProductData(); //product.BarCode = "sdfsdf2"; //product.Number = 12; ProductDataManage.Instance.Add(product);//不符合数据校验规则,将会抛出异常,会回滚 //return false; return true; }, out error); }
4.初始数据改在Model里重写GetInitData方法了,当对象表创建时,数据也会创建
/// <summary> /// 产品 /// </summary> public class Product : CRL.Package.Product.ProductBase { /// <summary> /// 初始默认数据 /// </summary> /// <returns></returns> public override System.Collections.IList GetInitData() { var list = new List<Product>(); for (int i = 1; i < 100; i++) { list.Add(new Product() { ProductName = "测试产品" + i, SettlementPrice = 80, SoldPrice = 100, SupplierId = 1, ProductStatus = CRL.Package.Product.ProductStatus.已上架 }); } return list; } }
演示项目CRLShoppingDemo
之前有人提到看不懂文档,希望有简单的例子,如你所愿,按基本在线销售系统写了一个Demo,以下为运行效果
此演示项目正如上面所说,采用了精简的写法实现了这些业务
继承业务封装,实现会员管理
public class MemberManage : CRL.Package.Person.PersonBusiness<MemberManage, Member>
商家和会员就都有了登录和验证方法,并实现Form认证
public ActionResult Login(Model.Member member) { string error; var a = MemberManage.Instance.CheckPass(member.AccountNo, member.PassWord, out error); if (!a) { ModelState.AddModelError("", error); return View(); } var u = MemberManage.Instance.QueryItem(b => b.AccountNo == member.AccountNo); if (u.Locked) { ModelState.AddModelError("", "账号已锁定"); return View(); } MemberManage.Instance.Login(u, "Member", false); string returnUrl = Request["returnUrl"]; if (string.IsNullOrEmpty(returnUrl)) { returnUrl = "/"; } return Redirect(returnUrl); }
页面上需要取当前用户,通过Form认证返回统一的Person对象
var user = MemberManage.Instance.CurrentUser;//CRL.Package.Person.Person
类似FindOne,FindList,分页方法不用去傻傻的写很多次了(一般三层结构会把这类方法写N次),已经自动转换好了
var item = ProductManage.Instance.QueryItemFromCache(b => b.Id == id);//从缓存查询 var item = ProductManage.Instance.QueryItem(b => b.Id == id);//从数据库查找
//分页
var query = ProductManage.Instance.GetLambdaQuery();
query.Where(b => b.SupplierId == CurrentUser.Id);
int count;
var result = ProductManage.Instance.Page(query, out count);
内置账户交易系统,解决多种类型支付管理问题
可以给N种角色设置N种货币类型,统一进行管理
比如以下是充值现金到会员
public bool Charge(Member member, decimal amount, string remark, TransactionType transactionType, out string error) { var account = Transaction.AccountManage.Instance.GetAccountId(member.Id, Model.AccountType.会员, transactionType); string orderId = DateTime.Now.ToString("yyMMddhhmmssff"); int tradeType = 10001; var trans = new List<CRL.Package.Account.Transaction>(); var ts = new CRL.Package.Account.Transaction() { AccountId = account, Amount = amount, OperateType = CRL.Package.Account.OperateType.收入, TradeType = tradeType, OutOrderId = orderId, Remark = remark }; trans.Add(ts); bool b = Transaction.TransactionManage.Instance.SubmitTransaction(out error,true, trans.ToArray());//提交流水 return b; }
确认订单时扣款,同时赠送积分
var accountUser = Transaction.AccountManage.Instance.GetAccount(order.UserId, Model.AccountType.会员, Model.TransactionType.现金); var accountUser2 = Transaction.AccountManage.Instance.GetAccount(order.UserId, Model.AccountType.会员, Model.TransactionType.积分); if (accountUser.AvailableBalance < order.TotalAmount) { error = "账户余额不足"; return false; } int tradeType = 1001; var amount = order.TotalAmount; var orderId = order.OrderId; var remark = "订单支付"; var trans = new List<CRL.Package.Account.Transaction>(); //生成会员交易流水 var ts = new CRL.Package.Account.Transaction() { AccountId = accountUser.Id, Amount = amount, OperateType = CRL.Package.Account.OperateType.支出, TradeType = tradeType, OutOrderId = orderId, Remark = remark }; trans.Add(ts); //赠送积分 var ts3 = new CRL.Package.Account.Transaction() { AccountId = accountUser2.Id, Amount = amount, OperateType = CRL.Package.Account.OperateType.收入, TradeType = tradeType, OutOrderId = orderId, Remark = "赠送积分" }; trans.Add(ts3); bool b = Transaction.TransactionManage.Instance.SubmitTransaction(out error, trans.ToArray());//提交流水 if(!b) { return false; }
更多详细写法请对照示例和开发文档
示例源码(不包含CRL源码) http://pan.baidu.com/s/1mg1YDqK
没什么人关注,为增加人气,CRL3.0完整源码请入群获取,谢谢支持