簡單的容器盒子

作一個公用,全局共享的容器盒子,全局單利使用。數據庫

不想將數據存放在數據庫,數據更新頻率很高,而且只作臨時存放點,測試

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Reflection;  5 using System.Text;  6 using System.Threading.Tasks;  7 /*********************************************************************  8 惟一標識:339980d6-46f1-450f-85ff-d6cfda251f20  9 版本號:v1.0.0.0  10 CLR版本:4.0.30319.42000  11 ======================================================================  12 建立人: liusg  13 建立時間:2018/7/5 17:05:04  14 **********************************************************************/
 15 namespace MTDemoRun  16 {  17     public class DBContainer  18  {  19         private List<PaCrt> list = new List<PaCrt>();  20         private static DBContainer _siCrt;  21         public static readonly object _Locked = new object();  22         public static DBContainer GetSingle()  23  {  24             if(_siCrt==null)  25  {  26                 lock(_Locked)  27  {  28                     if(_siCrt==null)  29  {  30                         _siCrt = new DBContainer();  31  }  32  }  33  }  34             return _siCrt;  35  }  36         private DBContainer()  37  {  38             list = ReadeDB();//初次加載,初始化數據
 39  }  40         
 41         
 42         public bool Add(PaCrt model)  43  {  44  list.Add(model);  45             return true;  46  }  47         /// <summary>
 48         /// 可添加,可修改  49         /// 存在時候修改,不存在時添加  50         /// </summary>
 51         /// <param name="mode"></param>
 52         /// <returns></returns>
 53         public bool UpdatOrAdd(PaCrt mode)  54  {  55             try
 56  {  57                 var t = mode.GetType();  58                 var propert = t.GetProperties().Where(p => p.IsDefined(typeof(KeyCrtAttributers), true))  59  .FirstOrDefault();  60 
 61                 if (propert == null) { return false; }  62 
 63                 var d = list.Find(p => p.GetType()  64  .GetRuntimeProperty(propert.Name)  65                   .GetValue(p) == mode.GetType()  66  .GetRuntimeProperty(propert.Name)  67  .GetValue(mode));  68 
 69                 if (d != null)  70  {  71  list.Remove(d);  72  }  73  list.Add(mode);  74                 return true;  75  }  76             catch (Exception e)  77  {  78 
 79                 return fasle;  80  }  81  }  82         public PaCrt GetModel(Func<PaCrt,bool> where)  83  {  84             return Clone().Where(where).FirstOrDefault();  85  }  86         public PaCrt Find(Func<PaCrt, bool> where)  87  {  88             return Clone().Where(where).FirstOrDefault();  89  }  90         public bool Delete(PaCrt model)  91  {  92             if (model == null) return false;  93             return list.Remove(model);  94  }  95         public List<PaCrt> GetList(Func<PaCrt, bool> where)  96  {  97             return Clone().Where(where).ToList();  98  }  99         public List<PaCrt> GetList( ) 100  { 101             return Clone().Where(p=>true).ToList(); 102  } 103         /// <summary>
104         /// 取數據時候,取副表的數據 105         /// </summary>
106         /// <returns></returns>
107         private List<PaCrt> Clone() 108  { 109             List<PaCrt> newList = new List<PaCrt>(); 110  newList.AddRange(list); 111             return newList; 112  } 113        /// <summary>
114        /// 寫入數據 115        /// </summary>
116        /// <param name="dblist"></param>
117         private void WriteDB(List<PaCrt> dblist) 118  { 119       //可寫入數據庫,也能夠存入臨時文件中
120  } 121         /// <summary>
122         /// 讀取數據 123         /// 
124         /// 
125         /// </summary>
126         /// <returns></returns>
127         private List<PaCrt> ReadeDB() 128  { //可從數據庫中讀取,也能夠從臨時文件中 129             return new List<PaCrt>(); 130  } 131  } 132     /// <summary>
133     /// Model 134     /// </summary>
135     public class PaCrt 136  { 137  [KeyCrtAttributers] 138         public string Name { get; set; } 139         public int Value { get; set; } 140  } 141     public class KeyCrtAttributers : Attribute 142  { 143         /// <summary>
144         /// 做爲惟一標識,主鍵, 145         /// 出現多個主鍵時候,只取第一個 146         /// </summary>
147         public bool MakeKey { get; set; } 148         public KeyCrtAttributers() 149  { 150             MakeKey = true; 151  } 152  } 153 
154 }

測試的狀況以下:

測試代碼:ui

 1 using Common;  2 using ManagerAPI;  3 using MetaTrader4.Manager.Contracts;  4 using MetaTrader4.Manager.Contracts.Configuration;  5 using System;  6 using System.Collections.Generic;  7 using System.Linq;  8 using System.Reflection;  9 using System.Text; 10 using System.Threading; 11 using System.Threading.Tasks; 12 
13 namespace MTDemoRun 14 { 15     class Program 16  { 17         static void Main(string[] args) 18  { 19             Console.Title = "Liusg"; 20             string[] list = new string[] 21  { 22                 "ASD", 23                 "WSD", 24                 "AQW", 25                 "TGH", 26                 "FGB", 27                 "JHD", 28                 "OLK", 29                 "POU", 30                 "EFT", 31                 "SRD", 32  }; 33              
34              
35             var t=DBContainer.GetSingle(); 36             Task.Factory.StartNew(() =>
37  { 38                 while (true) 39  { 40                     var td = Guid.NewGuid().ToString(); 41                     t.UpdatOrAdd(new PaCrt 42  { 43                         Name = list[td[0] % 10], 44                         Value = td[0] 45                     }); t.UpdatOrAdd(new PaCrt 46  { 47                         Name = list[td[1] % 10], 48                         Value = td[1] 49                     }); t.UpdatOrAdd(new PaCrt 50  { 51                         Name = list[td[2] % 10], 52                         Value = td[2] 53  }); 54                     Thread.Sleep(800); 55  } 56  }); 57 
58             Task.Factory.StartNew(() =>
59  { 60                 while (true) 61  { 62                     t.GetList().OrderBy(p=>p.Name).ToList().ForEach(p => Console.Write("{0}:{1},",p.Name,p.Value)); 63                     Console.WriteLine(""); 64                     Thread.Sleep(1000); 65  } 66  }); 67             do
68  { 69                 Console.Write("輸入t退出:"); 70 
71             } while (Console.ReadLine() == "t"); 72  } 73 
74        
75  } 76 }
相關文章
相關標籤/搜索