C# 利用Unity 实现IOC+AOP
public interface INoticy { void Noticy(string msg); } public class SMSNoticy : INoticy { public void Noticy(string msg) { Console.WriteLine(msg); } } public class Alarm { [Dependency] public INoticy Noticy { get; set; } public void TriggerAlarm() { Noticy.Noticy("test"); } }
public class ComponentInterceptor : IInterceptionBehavior { public bool WillExecute { get { return true; } } public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine("Before the call"); var result = getNext()(input, getNext); Console.WriteLine("After the call"); return result; } }
public class TestMain { public void test() { var container = new UnityContainer(); container.AddNewExtension<Interception>(); var interceptor = new Interceptor<InterfaceInterceptor>(); var interceptionBehavior = new InterceptionBehavior<ComponentInterceptor>(); container.RegisterType<INoticy, SMSNoticy>( interceptor, interceptionBehavior); container.RegisterSingleton<Alarm>(); var alarm = container.Resolve<Alarm>(); alarm.TriggerAlarm(); } }
依赖注入最常见的有,构造函数注入,属性注入,接口注入
大型项目比较通用的做法是,将需要注入的内容,放在config中,让程序自动加载注入
在需要使用的地方,直接resolve想要的对象就行,大型项目通过IoC实现各种new对象的操作,IoC最底层是通过activator.createinstance 实现
依赖注入并不需要项目引用DLL,只用保证生成的目录中有DLL就行
附带一例 配置实现的例子
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" /> </configSections> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/> <alias alias="INoticy" type="ClassLibrary1.INoticy, ClassLibrary1"/> <alias alias="SMSNoticy" type="ClassLibrary2.SMSNoticy, ClassLibrary2"/> <!--<alias alias="SMSNoticy1" type="ClassLibrary2.SMSNoticy1, ClassLibrary2"/>--> <alias alias="IAlarm" type="ClassLibrary1.IAlarm, ClassLibrary1"/> <alias alias="Alarm" type="ClassLibrary3.Alarm, ClassLibrary3"/> <alias alias="Alarm" type="ClassLibrary3.Alarm, ClassLibrary3"/> <alias alias="ComponentInterceptor" type="WindowsFormsApplication1.ComponentInterceptor, WindowsFormsApplication1"/> <container name= "SMS"> <extension type="Interception" /> <register type= "INoticy" mapTo= "SMSNoticy"/> <register type= "IAlarm" mapTo= "Alarm"> <interceptor type="InterfaceInterceptor"/> <interceptionBehavior name="ComponentInterceptor" type="ComponentInterceptor" /> </register> </container> </unity> </configuration>
public void test() { var container = new UnityContainer(); var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "unity.config" }; //"Config/unity.config" var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var unitySection = (UnityConfigurationSection)configuration.GetSection("unity"); container.LoadConfiguration(unitySection,"SMS"); var alarm = container.Resolve<IAlarm>(); alarm.TriggerAlarm(); }