HelloData之:数据库之逻辑层的生成。
上一节将model层创建完毕了,这一节就到了逻辑层与数据库对接的service。(请下载最新的源码,因为这段时间整理有些地方还在不断的完善,希望做到更好)。
首先我们看到HelloData.FrameWork的Data源码中包含了
IRepository为常用的数据操作,代码如下:
/// <summary> /// manage接口类 /// </summary> public interface IRepository<T> : IDisposable { /// <summary> /// 删除一个实体类 /// </summary> /// <param name="entity"></param> int Remove(T entity); /// <summary> /// 获取一个实体类 /// </summary> /// <param name="id"></param> /// <returns></returns> T FindById(object id); /// <summary> /// 插入一个实体类 /// </summary> /// <param name="entity"></param> /// <returns></returns> int Add(T entity); /// <summary> /// 更新一个实体类 /// </summary> /// <param name="entity"></param> int Save(T entity); /// <summary> /// 获取分页后代码 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="querys"></param> /// <returns></returns> PageList<T> FindList(int pageIndex, int pageSize, params QueryField[] querys);
BaseLogic 继承了 IRepository ,实现了接口中的所有的方法:
public abstract class BaseLogic<T> : IRepository<T>, IDisposable where T : new() { protected BaseLogic() { t = new T(); Entity = t as BaseEntity; } public BaseEntity Entity; /// <summary> /// 执行删除操作 /// </summary> /// <returns></returns> public virtual int Remove(T entity) { BaseEntity baseEntity = entity as BaseEntity; if (baseEntity != null) using (DeleteAction delete = new DeleteAction(baseEntity)) { delete.SqlWhere(Entity.KeyId, baseEntity.KeyIDValue); delete.Excute(); return delete.ReturnCode; } return 0; } /// <summary> /// 获取一个model /// </summary> /// <param name="keyvalue"></param> /// <returns></returns> public virtual T FindById(object keyvalue) { using (SelectAction select = new SelectAction(Entity)) { select.SqlWhere(Entity.KeyId, keyvalue); return select.QueryEntity<T>(); } } /// <summary> /// 保存(粗略的保存,保存实体类有值的数据) /// </summary> /// <param name="entity"></param> /// <returns></returns> public virtual int Add(T entity) { using (InserAction insert = new InserAction(entity as BaseEntity)) { return insert.Excute().ReturnCode; } } /// <summary> /// 更新 /// </summary> /// <param name="entity"></param> public virtual int Save(T entity) { BaseEntity baseEntity = entity as BaseEntity; if (baseEntity != null) using (UpdateAction update = new UpdateAction(baseEntity)) { update.SqlWhere(Entity.KeyId, baseEntity.KeyIDValue); update.Excute(); return update.ReturnCode; } return 0; } /// <summary> /// 获取分页数据 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="objects"></param> /// <returns></returns> public virtual PageList<T> FindList(int pageIndex, int pageSize, params QueryField[] querys) { using (SelectAction select = new SelectAction(Entity)) { if (querys.Length > 0) select.SqlWhere(querys); select.SqlPageParms(pageSize); return select.QueryPage<T>(pageIndex); } } /// <summary> /// 操作的基础model /// </summary> public T t { get; set; } public void Dispose() { Entity = null; }
}
BaseManager 有两种继承操作方式, 继承后为全局唯一实例,单例模式操作 ,其中一个已经加入了BaseLogic,泛型BaseManager<T, TU>,T为当前继承后的操作对象,TU为需要操作的Model对象(后期加入空对象)。
/// <summary> /// 全局唯一实例化,并自动创建部分代码 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TU"></typeparam> public class BaseManager<T, TU> : BaseLogic<TU>, IDisposable where T : BaseManager<T, TU>, new() where TU : BaseEntity, new() { private static T _sInstance = null; /// <summary> /// 获取当前业务逻辑对象的实例 /// </summary> public static T Instance { get { return _sInstance ?? (_sInstance = new T()); } } public void SetBaseEntity() { base.Entity = new TU(); } /// <summary> /// 创建结果 /// </summary> /// <param name="resultCode"></param> /// <param name="message"></param> /// <returns></returns> public string CreateResult(int resultCode, object message) { HandlerResponse result = new HandlerResponse { Result = resultCode, Message = message }; return result.ToString(); } public void Dispose() { } }
继承的demo如下:
public class cms_userManager : BaseManager<cms_userManager, cms_user> { internal void MulitTest(string ids) { string[] str = ids.Split(','); MultiAction mulut = new MultiAction(); DataBaseAction action = new DataBaseAction(); foreach (var item in str) { DeleteAction delete = new DeleteAction(Entity); /// delete.SqlWhere(cms_user.Columns.id, item); delete.Cast<cms_user>().Where(user => user.isactive == 1); mulut.AddAction(delete); action = delete; } try { mulut.Commit(); } catch { mulut.Rollback(); } } /// <summary> /// 自定义视图 /// </summary> public cms_user viewtestModel() { using (SelectAction action = new SelectAction("")) { action.SqlClomns = "_cms_user.*,_cms_manager.name as managername"; { //添加视图的关联关系 List<QueryField> field = new List<QueryField>(); field.Add(new QueryField() { FiledName = "mangerid", Condition = ConditionEnum.And, Value = "id" }); action.AddJoin(ViewJoinEnum.leftjoin, "cms_user", "cms_manager", field); } action.SqlWhere( new QueryField { FiledName = "", Value = "" }, new QueryField { FiledName = "", Value = "" } ); action.SqlWhere(cms_user.Columns.username, "admin"); action.SqlWhere(cms_user.Columns.password, "123456"); PageList<cms_user> lists = action.QueryPage<cms_user>(1); return null; } } }
调用方式为:
if (cms_userManager.Instance.UpdatePwd("1,2,3,4,5") > 0) Console.WriteLine("success"); cms_userManager.Instance.Update(new cms_user() { username = "test" + DateTime.Now.Millisecond, password = "123456", id = 12, createtime = null }); //自定义视图的操作 cms_user viewmodel = cms_userManager.Instance.viewtestModel(); if (viewmodel != null) Console.WriteLine("-->VIEWusername:" + viewmodel.username + " VIEWid:" + viewmodel.id);
下一段讲解系统参数的配置。