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();
}

C# 利用Unity 实现IOC+AOP的更多相关文章

  1. Unity 处理IOC AOP

    用Unity 可以做IOC(控制反转) AOP(切面)可以做统一的异常和日志处理,非常方便,项目中是用微软企业库中的Microsoft.Practices.Unity实现 1 定义接口与实现 //定义 ...

  2. Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)

    在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...

  3. spring ioc aop 原理

    spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...

  4. C#利用Emit反射实现AOP,以及平台化框架封装思路

    C#利用Emit反射实现AOP,以及平台化框架封装思路 这是前两天扒的一段动态代理AOP代码,用的Emit反射生成子类来实现代理模式,在这里做个小笔记,然后讨论一下AOP框架的实现思路. 首先是主函数 ...

  5. Unity容器中AOP应用示例程序

    转发请注明出处:https://www.cnblogs.com/zhiyong-ITNote/p/9127001.html 实在没有找到Unity容器的AOP应用程序示例的说明,在微软官网找到了教程( ...

  6. Spring中三个重要概念 IOC AOP Bean

    Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...

  7. Unity(IOC)学习笔记

    原文:Unity(IOC)学习笔记 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79432 ...

  8. 利用Unity制作“表”

    一枚小菜鸟   目前没发现在Unity有其他路径制作类似于c# WinForm中的表:但是利用Unity自带的UGUI,制作了一张"伪表",具体方案如下: 效果图如下: 步骤: 1 ...

  9. IOC AOP 设计模式

    IOC AOP 不是什么技术而是一种设计模式  学习 IOC AOP 其实是在学习一种思想. 1.IOC IOC其实是 将对象的创建和获取提取到外部.由外部IOC容器提供需要的组件. 看下面代码: p ...

随机推荐

  1. python基础一之while循环随机猜数字

    # Author:"Mamba" import random setNum = random.randint(1,10) #print(setNum) count = 0 whil ...

  2. Python_文件处理

    1.Python  文件处理 打开文件---->读取内容---->获得内容 读取文件方式:  r  只读文件  w 只写模式 a 追加模式 r+b 读写模式   w+b 写读模式  a+b ...

  3. YOLOv3训练自己的数据

    1.  下载预训练权重文件 YOLOv3使用在Imagenet上预训练好的模型参数(文件名称: darknet53.conv.74,大小76MB)基础上继续训练. darknet53.conv.74下 ...

  4. CMake 笔记

    1. configure_file configure_file()让你可以在代码文件中使用CMake中定义的变量. configure_file(<input> <output&g ...

  5. unsigned int reverse_bit(unsigned int value);

    /*编写函数 unsigned int reverse_bit(unsigned int value); 这个函数的返回值吧value的二进制位模式从左到右翻转后的值. 如在32位机器上25这个值包含 ...

  6. SpringBoot 部署到linux环境

    第一部分:Springboot项目部署 说明:工具使用的是IEDA 第一:项目打包 1.在pom文件中添加插件 <build> <plugins> <plugin> ...

  7. java标志性接口

    标识接口是没有任何方法和属性的接口.它仅仅表明它的类属于一个特定的类型,供其他代码来测试允许做一些事情.使用标记接口的唯一目的是使得可以用instanceof进行类型查询,例如:if(obj inst ...

  8. 设置table的每竖的宽度

  9. 高通平台获取secure boot,串号等状态

    adb shell下 运行./system/bin/r address 其中address对应各个flag参数的地址,具体如下: 无法打开/dev/mem节点(没有该节点),这时只需在内核配置中选上C ...

  10. day1||python

    测试题: 0. Python 是什么类型的语言? Python是一种面向对象.解释型.动态类型计算机程序设计语言解释型:程序无需编译成二进制代码,而是在执行时对语句一条一条编译动态类型:在程序执行过程 ...