/// <summary> /// 导出实体数据到内存数据库。如果当前实体操作失败,请检查导出事件的异常参数对象。 /// </summary> /// <param name="funQ">获取导出数据的查询表达式委托方法,委托方法的参数为导出批次号;如果结果为空,导出实体全部数据</param> /// <param name="initBatchNumber">要初始化导出批次号的函数</param> public void Export(Func <int, T, OQL> funQ, Func <T, int> initBatchNumber) { Type entityType = typeof(T); try { //导出批次管理 string exportTableName = EntityFieldsCache.Item(entityType).TableName; List <ExportBatchInfo> batchList = MemDB.Get <ExportBatchInfo>(); ExportBatchInfo currBatch = batchList.FirstOrDefault(p => p.ExportTableName == exportTableName); if (currBatch == null) { currBatch = new ExportBatchInfo(); currBatch.BatchNumber = initBatchNumber == null?1: initBatchNumber(new T()); currBatch.ExportTableName = exportTableName; currBatch.LastExportDate = DateTime.Now; // batchList.Add(currBatch); MemDB.Add(currBatch); } else { currBatch.BatchNumber += 1; currBatch.LastExportDate = DateTime.Now; } MemDB.Save <ExportBatchInfo>(); //导出数据 OQL q = funQ(currBatch.BatchNumber, new T()); List <T> entityList = q != null?CurrDbContext.QueryList <T>(q) : CurrDbContext.QueryAllList <T>(); ExportEntityEventArgs <T> args = new ExportEntityEventArgs <T>(entityList, entityType, exportTableName); args.Succeed = true; args.OperationExcepiton = null; args.BatchNumber = currBatch.BatchNumber; if (OnExported != null) { OnExported(this, args); } if (!args.Cancel) { SaveEntity(entityList.ToArray(), args); } } catch (Exception ex) { ExportEntityEventArgs <T> args = new ExportEntityEventArgs <T>(null, entityType, EntityFieldsCache.Item(entityType).TableName); args.Succeed = false; args.OperationExcepiton = ex; if (OnExported != null) { OnExported(this, args); } } }
private void SaveEntity <T>(T[] entitys, ExportEntityEventArgs args) where T : EntityBase, new() { if (entitys.Length > 0) { args.Succeed = MemDB.SaveEntity <T>(entitys); if (OnSaved != null) { OnSaved(this, args); } } }
private void SaveEntity(T[] entitys, ExportEntityEventArgs <T> args) { if (entitys.Length > 0) { args.Succeed = MemDB.SaveEntity <T>(entitys); } else { MemDB.DropEntity <T>(); args.Succeed = true; } if (OnSaved != null) { OnSaved(this, args); } }