• 当你使用LINQ做底层时,最好设计一个工厂,不要把LINQ的动作暴露给业务层


      1: using System;
    
      2: using System.Collections.Generic;
    
      3: using System.Linq;
    
      4: using System.Text;
    
      5: using System.Data.Linq;
    
      6: using System.Threading;
    
      7: 
    
      8: namespace Data
    
      9: {
    
     10:     /// <summary>
    
     11:     /// LINQ数据库连接对象制造工厂
    
     12:     /// </summary>
    
     13:     static internal class DataContextFactory
    
     14:     {
    
     15: 
    
     16:         static System.Timers.Timer sysTimer = new System.Timers.Timer(10000);
    
     17: 
    
     18:         volatile static Dictionary<Thread, DataContext[]> divDataContext = new Dictionary<Thread, DataContext[]>();
    
     19: 
    
     20:         static public int DataContextCount { get { return divDataContext.Count; } }
    
     21: 
    
     22:         static DataContextFactory()
    
     23:         {
    
     24:             sysTimer.AutoReset = true;
    
     25:             sysTimer.Enabled = true;
    
     26:             sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
    
     27:             sysTimer.Start();
    
     28:         }
    
     29: 
    
     30:         static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    
     31:         {
    
     32:             List<Thread> list = new List<Thread>();
    
     33:             foreach (Thread item in divDataContext.Keys)
    
     34:             {
    
     35:                 if (item.ThreadState == ThreadState.Stopped)
    
     36:                 {
    
     37:                     list.Add(item);
    
     38:                 }
    
     39:             }
    
     40:             for (int index = 0; index < list.Count; index++)
    
     41:             {
    
     42:                 divDataContext.Remove(list[index]);
    
     43:                 list[index] = null;
    
     44:             }
    
     45:             list = null;
    
     46:         }
    
     47: 
    
     48:         /// <summary>
    
     49:         /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
    
     50:         /// </summary>
    
     51:         /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
    
     52:         /// <returns>LINQ数据库连接对象</returns>
    
     53:         public static DataContext Intance(string dbName)
    
     54:         {
    
     55:             return Intance(dbName, Thread.CurrentThread);
    
     56:         }
    
     57:         /// <summary>
    
     58:         /// 通过工厂的制造模式获取相应的LINQ数据库连接对象
    
     59:         /// </summary>
    
     60:         /// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
    
     61:         /// <param name="thread">当前线程引用的对象</param>
    
     62:         /// <returns>LINQ数据库连接对象</returns>
    
     63:         public static DataContext Intance(string dbName, Thread thread)
    
     64:         {
    
     65:             if (!divDataContext.Keys.Contains(thread))
    
     66:             {
    
     67:                 divDataContext.Add(thread, new DataContext[4]);
    
     68:             }
    
     69:             if (dbName.Equals("zzl"))
    
     70:             {
    
     71:                 if (divDataContext[thread][0] == null)
    
     72:                 {
    
     73:                     divDataContext[thread][0] = new Data.LinqzzlDataContext();
    
     74:                 }
    
     75:                 return divDataContext[thread][0];
    
     76:             }
    
     77:             else
    
     78:             {
    
     79:                 return null;
    
     80:             }
    
     81:         }
    
     82: 
    
     83:  
    
     84: 
    
     85:     }
    
     86: }
    
     87: 
  • 相关阅读:
    hdu 1395 2^x(mod n) = 1(C++)(欧拉定理 分解素因数)
    6. 数论准备知识
    hdu 2973 YAPTCHA(C++)(威尔逊定理)
    牛客小白月赛12——B.华华教月月做数学
    牛客小白月赛12——A.华华听月月唱歌
    5. 卡特兰数(Catalan)公式、证明、代码、典例.
    4.质数判定和质数筛法(埃拉托色尼筛选法,线性筛法/欧拉筛法)
    3.牛顿迭代法求解方程的根
    Codeforces刷题
    刷题计划
  • 原文地址:https://www.cnblogs.com/lori/p/2120424.html
Copyright © 2020-2023  润新知