/// <summary> /// 保存数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="type"></param> /// <param name="list"></param> private static void SaveChanges <T>(List <T> list) { StaticDao.SaveChanges(list, GlobalData, DataFilePath); //ThreadPool.QueueUserWorkItem((s) => //{ // StaticDao.SaveChanges(list, GlobalData, DataFilePath); // Thread.Sleep(1000 * 2); //}); }
/// <summary> /// 初始化 /// </summary> /// <param name="dataAssemblyName"></param> public static void Start(string dataAssemblyName) { if (!string.IsNullOrEmpty(dataAssemblyName)) { DataAssemblyName = dataAssemblyName; } var typeList = Util.Reflection.AssemblyUtil.GetTypeListOfImplementedInterface(DataAssemblyName, typeof(Interface.IDbModel)).ToArray(); if (GlobalData == null) { GlobalData = new Dictionary <Type, List <object> >(); } GlobalData = StaticDao.ReadData(GlobalData, typeList); }
/// <summary> /// 添加数据 /// </summary> /// <param name="dataAry"></param> public static void Add <T>(params T[] dataAry) { lock (locker) { var list = GetList <T>(); list.AddRange(dataAry); SaveChanges(list); } foreach (var item in dataAry) { Excute(() => { StaticDao.Add(item); }); } }
/// <summary> /// 读取数据 /// </summary> /// <param name="typeList"></param> private static void ReadData(params Type[] typeList) { foreach (var type in typeList) { if (!DataFilePath.ContainsKey(type)) { DataFilePath.Add(type, StaticDao.GetDataFilePath(type, DataDirPath)); } if (!GlobalData.ContainsKey(type)) { var data = StaticDao.ReadGlobalData(type, DataFilePath[type]); GlobalData.Add(type, data); } } }
/// <summary> /// 删除数据 /// </summary> /// <param name="dataAry"></param> public static void Remove <T>(params T[] dataAry) { lock (locker) { var list = GetList <T>(); foreach (var item in dataAry) { list.Remove(item); } SaveChanges(list); } foreach (var item in dataAry) { Excute(() => { StaticDao.Del(item); }); } }
/// <summary> /// 删除数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="where"></param> public static void Remove <T>(Func <T, bool> where) { T[] entityList = new T[] { }; lock (locker) { var list = GetList <T>(); entityList = list.Where(where).ToArray(); foreach (var item in entityList) { list.Remove(item); } SaveChanges(list); } foreach (var item in entityList) { Excute(() => { StaticDao.Del(item); }); } }
/// <summary> /// 更新数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="oldEntity"></param> /// <param name="newEntity"></param> public static void Update <T>(Func <T, bool> where, T newEntity) { T[] oldEntityList = new T[] { }; lock (locker) { var list = GetList <T>(); oldEntityList = list.Where(where).ToArray(); foreach (var item in oldEntityList) { list.Remove(item); list.Add(newEntity); } SaveChanges(list); } foreach (var item in oldEntityList) { Excute(() => { StaticDao.Update(item, newEntity); }); } }