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学习第二次笔记

    python学习第二次记录 1.格式化输出 name = input('请输入姓名') age = input('请输入年龄') height = input('请输入身高') msg = " ...

  2. c# 在.NET使用Newtonsoft.Json转换,读取,写入json

    转自:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html  首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https:/ ...

  3. oracle 修改服务端字符集编码

    进入服务端的sqlplus命令界面 SELECT * FROM V$NLS_PARAMETERS; 可以查看参数的值. 解决字符集编码 NLS_CHARACTERSET 办法: UPDATE PROP ...

  4. 2019 Power BI最Top50面试题,助你面试脱颖而出系列<中>

    敲黑板啦!!! 来来来 大家双眼看黑板 开始划重点啦 这篇大部分是"考试"必考题 你们一定要好好的牢记在心 一分都不要放过 刷题中... Power BI面试题目-DAX 9)什么 ...

  5. light sdk

    //请求ajax var request = function (url,method,params,cb) { var d = ajax({ url:url, type:method, data:p ...

  6. python 各种开源库

    测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...

  7. 实验十 ZStack 网状网络实验

    实验十 ZStack 网状网络实验[实验目的]1. 了解 ZigBee 网状网络结构2. 掌握构建网状网络的方法[实验设备]1. 装有 IAR 开发工具的 PC 机一台2. 实验箱一台3. CCDeb ...

  8. SSH配置

    什么是SSH: SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH ...

  9. advanceskeleton插件分身体和表情单独绑定的时候合并表情步骤

    advanceskeleton插件分身体和表情单独绑定的时候合并表情使用的代码以及合并步骤 1.身体单独绑定 2.表情单独绑定 3.合并步骤 ①原有adv表情文件删掉除了curve组以外所有东西 删除 ...

  10. JavaScript 查找图中连接两点的所有路径算法

    1.把图看成以起点为根节点的树 2.使用深度遍历算法遍历路径 3.遍历到节点为目标节点时,保存这条路径 find2PointsPath(sourceId, targetId) { const { no ...