BizComponentFactory.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MediII.Adapter.BizComponent.Base
  6. {
  7. public static class BizComponentFactory
  8. {
  9. static readonly string HosiptalName;
  10. static List<string> listNotExist = new List<string>();
  11. static BizComponentFactory()
  12. {
  13. HosiptalName = System.Configuration.ConfigurationManager.AppSettings["HosiptalName"];
  14. }
  15. public static IBizComponent GetBizComponent(string messageName)
  16. {
  17. string assemblyName = string.Format("MediII.Adapter.BizComponent.{0}", messageName);
  18. string typeName ;
  19. //if (messageName == "ORM")
  20. //{
  21. // int i = 10;
  22. //}
  23. if (string.IsNullOrEmpty(HosiptalName) || listNotExist.Contains(messageName))
  24. {
  25. typeName = string.Format("{0}.BizComponent_{1}", assemblyName, messageName);
  26. try
  27. {
  28. return Activator.CreateInstance(assemblyName, typeName).Unwrap() as IBizComponent;
  29. }
  30. catch(Exception ex)
  31. {
  32. throw new Exception(string.Format("创建消息处理对象{0}失败", typeName));
  33. }
  34. }
  35. else
  36. {
  37. typeName = string.Format("{0}.BizComponent_{1}_{2}", assemblyName, messageName, HosiptalName);
  38. try
  39. {
  40. return Activator.CreateInstance(assemblyName, typeName).Unwrap() as IBizComponent;
  41. }
  42. catch (TypeLoadException)
  43. {
  44. typeName = string.Format("{0}.BizComponent_{1}", assemblyName, messageName);
  45. var bizComponet = Activator.CreateInstance(assemblyName, typeName).Unwrap() as IBizComponent;
  46. lock (listNotExist)
  47. {
  48. listNotExist.Add(messageName);
  49. }
  50. return bizComponet;
  51. }
  52. catch
  53. {
  54. throw new Exception(string.Format("创建消息处理对象{0}失败", typeName));
  55. }
  56. }
  57. }
  58. }
  59. }