BLLFactory.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using ProjectBase.Util;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ProjectBase.Data.BaseDAL
  9. {
  10. /// <summary>
  11. /// 对业务类进行构造的工厂类
  12. /// </summary>
  13. /// <typeparam name="T">业务对象类型</typeparam>
  14. public class BLLFactory<T> where T : class
  15. {
  16. private static Hashtable objCache = new Hashtable();
  17. private static object syncRoot = new Object();
  18. /// <summary>
  19. /// 创建或者从缓存中获取对应业务类的实例
  20. /// </summary>
  21. public static T Instance
  22. {
  23. get
  24. {
  25. string CacheKey = typeof(T).FullName;
  26. T bll = (T)objCache[CacheKey];  //从缓存读取
  27. if (bll == null)
  28. {
  29. lock (syncRoot)
  30. {
  31. if (bll == null)
  32. {
  33. bll = Reflect<T>.Create(typeof(T).FullName, typeof(T).Assembly.GetName().Name); //反射创建,并缓存
  34. objCache.Add(typeof(T).FullName, bll);
  35. }
  36. }
  37. }
  38. return bll;
  39. }
  40. }
  41. }
  42. }