用C# 3.0 写了个IoC类
虽然c# Meta programing 的能力不够
不过新特性还是比较爽的
用这些写了个IoC类
挺有意思
c# 代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
- namespace Test
- {
- /// <summary></summary>
- /// service interface
- ///
- public interface IComponentService<t> </t>
- {
- T GetComponent();
- }
- /// <summary></summary>
- /// normal service
- ///
- public class SingleComponentService<t>:IComponentService<t> </t></t>
- {
- private Func
- private MicroContainer container;
- public SingleComponentService(MicroContainer container,Func
- {
- this.container = container;
- this.registerFunction = registerFunction;
- }
- #region IComponentService Members
- public T GetComponent()
- {
- return (T)this.registerFunction(this.container);
- }
- #endregion
- }
- /// <summary></summary>
- /// singleton service
- ///
- public class CachedComponentService<t>:IComponentService<t> </t></t>
- {
- private IComponentService<t> realService; </t>
- private T result = default(T);
- public CachedComponentService(IComponentService<t> realService) </t>
- {
- this.realService = realService;
- }
- #region IComponentService Members
- public T GetComponent()
- {
- if (this.result == null)
- this.result = this.realService.GetComponent();
- return this.result;
- }
- #endregion
- }
- public class ServiceNotExistException : Exception
- {
- public ServiceNotExistException(string message):base(message)
- {
- }
- }
- /// <summary></summary>
- /// 小型Micro 容器
- /// todo: 1 父容器 -ok
- /// 2 service regist -ok
- /// 3 generic -ok
- /// 4 singlton,lazy initial -ok
- /// 5 aop,attribute driver ,define to kernelSupport
- ///
- public class MicroContainer
- {
- private IDictionary<string, object> componets
- = new Dictionary<string, object>();
- private MicroContainer parentContainer;
- public MicroContainer():this(null)
- {
- }
- public MicroContainer(MicroContainer parentContainer)
- {
- this.parentContainer = parentContainer;
- }
- /// <summary></summary>
- /// 注册服务
- ///
- ///
- ///
- ///
- public void Register<t>(</t>string key, Func
- {
- this.componets.Add(key, new SingleComponentService<t>(</t>this,registerFunction));
- }
- /// <summary></summary>
- /// 注册singleton服务
- ///
- ///
- ///
- ///
- public void RegisterSingleton<t>(</t>string key, Func
- {
- this.componets.Add(key, new CachedComponentService<t>(</t>new SingleComponentService<t>(</t>this,registerFunction)));
- }
- /// <summary></summary>
- /// 注册服务
- ///
- ///
- ///
- ///
- public void RegisterService<t>(</t>string key,IComponentService<t> service) </t>
- {
- this.componets.Add(key, service);
- }
- /// <summary></summary>
- /// 取得组件
- ///
- ///
- ///
- /// <returns>组件</returns>
- public T GetComponent<t>(</t>string key)
- {
- if (this.componets.ContainsKey(key))
- return ((IComponentService<t>)</t>this.componets[key]).GetComponent();
- if (this.parentContainer != null)
- return this.parentContainer.GetComponent<t>(key); </t>
- else
- throw new ServiceNotExistException("component not exist:" + key);
- }
- }
- }
使用方法如下
测试类:
c# 代码
- public class MainClass
- {
- public string Name { get; set; }
- public int DoTimes { get; set; }
- public FirstInterface Sth { get; set; }
- public void Run()
- {
- for (int i = 0; i < this.DoTimes; i++)
- {
- this.Sth.Run(this.Name);
- }
- }
- public void RunForMetaCall()
- {
- Console.WriteLine("meta method call");
- }
- public string MethodMissing(string name)
- {
- return "hello" + name;
- }
- }
- public interface FirstInterface
- {
- void Run(string name);
- }
- public class FirstClass : FirstInterface
- {
- #region FirstInterface Members
- public void Run(string name)
- {
- Console.WriteLine("first hello {0}", name);
- }
- #endregion
- }
- public class SecondClass : FirstInterface
- {
- #region FirstInterface Members
- public void Run(string name)
- {
- Console.WriteLine("second hello {0}", name);
- }
- #endregion
- }
c# 代码
- var parentContainer = new MicroContainer();
- var container = new MicroContainer(parentContainer);
- container.Register<firstinterface>("FirstClass", c => new FirstClass()); </firstinterface>
- container.Register("Name", c => "xiao li");
- container.Register("Name2", c => "xiao zhang");
- container.Register("Times", c => 3);
- container.Register("Times2", c => 1);
- container.Register("MainClass",
- c => new MainClass
- {
- Name = c.GetComponent<string>("Name2"),
- DoTimes = c.GetComponent<int>("Times2"),
- Sth = c.GetComponent<firstinterface>("SecondClass") </firstinterface>
- });
- parentContainer.Register<firstinterface>("SecondClass", </firstinterface>
- c => new SecondClass());
- var mainClass = container.GetComponent<mainclass>("MainClass"); </mainclass>
- mainClass.Run();