ITeye博客

用C# 3.0 写了个IoC类

nacu · 2007-07-02 22:32 · .net · 编程语言 · Markdown

虽然c# Meta programing 的能力不够
不过新特性还是比较爽的
用这些写了个IoC类
挺有意思

c# 代码

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. namespace Test
  7. {
  8. /// <summary></summary>
  9. /// service interface
  10. ///
  11. public interface IComponentService<t>  </t>
  12. {
  13. T GetComponent();
  14. }
  15. /// <summary></summary>
  16. /// normal service
  17. ///
  18. public class SingleComponentService<t>:IComponentService<t>  </t></t>
  19. {
  20. private Func
  21. private MicroContainer container;
  22. public SingleComponentService(MicroContainer container,Func
  23. {
  24. this.container = container;
  25. this.registerFunction = registerFunction;
  26. }
  27. #region IComponentService Members
  28. public T GetComponent()
  29. {
  30. return (T)this.registerFunction(this.container);
  31. }
  32. #endregion
  33. }
  34. /// <summary></summary>
  35. /// singleton service
  36. ///
  37. public class CachedComponentService<t>:IComponentService<t>  </t></t>
  38. {
  39. private IComponentService<t> realService;  </t>
  40. private T result = default(T);
  41. public CachedComponentService(IComponentService<t> realService)  </t>
  42. {
  43. this.realService = realService;
  44. }
  45. #region IComponentService Members
  46. public T GetComponent()
  47. {
  48. if (this.result == null)
  49. this.result = this.realService.GetComponent();
  50. return this.result;
  51. }
  52. #endregion
  53. }
  54. public class ServiceNotExistException : Exception
  55. {
  56. public ServiceNotExistException(string message):base(message)
  57. {
  58. }
  59. }
  60. /// <summary></summary>
  61. /// 小型Micro 容器
  62. /// todo: 1 父容器         -ok
  63. ///       2 service regist -ok
  64. ///       3 generic        -ok
  65. ///       4 singlton,lazy initial -ok
  66. ///       5 aop,attribute driver ,define to kernelSupport
  67. ///
  68. public class MicroContainer
  69. {
  70. private IDictionary<string, object> componets
  71. = new Dictionary<string, object>();
  72. private MicroContainer parentContainer;
  73. public MicroContainer():this(null)
  74. {
  75. }
  76. public MicroContainer(MicroContainer parentContainer)
  77. {
  78. this.parentContainer = parentContainer;
  79. }
  80. /// <summary></summary>
  81. /// 注册服务
  82. ///
  83. ///
  84. ///
  85. ///
  86. public void Register<t>(</t>string key, Func
  87. {
  88. this.componets.Add(key, new SingleComponentService<t>(</t>this,registerFunction));
  89. }
  90. /// <summary></summary>
  91. /// 注册singleton服务
  92. ///
  93. ///
  94. ///
  95. ///
  96. public void RegisterSingleton<t>(</t>string key, Func
  97. {
  98. this.componets.Add(key, new CachedComponentService<t>(</t>new SingleComponentService<t>(</t>this,registerFunction)));
  99. }
  100. /// <summary></summary>
  101. /// 注册服务
  102. ///
  103. ///
  104. ///
  105. ///
  106. public void RegisterService<t>(</t>string key,IComponentService<t> service)  </t>
  107. {
  108. this.componets.Add(key, service);
  109. }
  110. /// <summary></summary>
  111. /// 取得组件
  112. ///
  113. ///
  114. ///
  115. /// <returns>组件</returns>
  116. public T GetComponent<t>(</t>string key)
  117. {
  118. if (this.componets.ContainsKey(key))
  119. return ((IComponentService<t>)</t>this.componets[key]).GetComponent();
  120. if (this.parentContainer != null)
  121. return this.parentContainer.GetComponent<t>(key);  </t>
  122. else
  123. throw new ServiceNotExistException("component not exist:" + key);
  124. }
  125. }
  126. }

使用方法如下
测试类:

c# 代码

 

  1. public class MainClass
  2. {
  3. public string Name { get; set; }
  4. public int DoTimes { get; set; }
  5. public FirstInterface Sth { get; set; }
  6. public void Run()
  7. {
  8. for (int i = 0; i < this.DoTimes; i++)
  9. {
  10. this.Sth.Run(this.Name);
  11. }
  12. }
  13. public void RunForMetaCall()
  14. {
  15. Console.WriteLine("meta method call");
  16. }
  17. public string MethodMissing(string name)
  18. {
  19. return "hello" + name;
  20. }
  21. }
  22. public interface FirstInterface
  23. {
  24. void Run(string name);
  25. }
  26. public class FirstClass : FirstInterface
  27. {
  28. #region FirstInterface Members
  29. public void Run(string name)
  30. {
  31. Console.WriteLine("first hello {0}", name);
  32. }
  33. #endregion
  34. }
  35. public class SecondClass : FirstInterface
  36. {
  37. #region FirstInterface Members
  38. public void Run(string name)
  39. {
  40. Console.WriteLine("second hello {0}", name);
  41. }
  42. #endregion
  43. }

c# 代码

 

  1. var parentContainer = new MicroContainer();
  2. var container = new MicroContainer(parentContainer);
  3. container.Register<firstinterface>("FirstClass", c => new FirstClass());  </firstinterface>
  4. container.Register("Name", c => "xiao li");
  5. container.Register("Name2", c => "xiao zhang");
  6. container.Register("Times", c => 3);
  7. container.Register("Times2", c => 1);
  8. container.Register("MainClass",
  9. c => new MainClass
  10. {
  11. Name = c.GetComponent<string>("Name2"),
  12. DoTimes = c.GetComponent<int>("Times2"),
  13. Sth = c.GetComponent<firstinterface>("SecondClass")  </firstinterface>
  14. });
  15. parentContainer.Register<firstinterface>("SecondClass",  </firstinterface>
  16. c => new SecondClass());
  17. var mainClass = container.GetComponent<mainclass>("MainClass");  </mainclass>
  18. mainClass.Run();

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。