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();
}
C# 利用Unity 实现IOC+AOP的更多相关文章
- Unity 处理IOC AOP
用Unity 可以做IOC(控制反转) AOP(切面)可以做统一的异常和日志处理,非常方便,项目中是用微软企业库中的Microsoft.Practices.Unity实现 1 定义接口与实现 //定义 ...
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
- spring ioc aop 原理
spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...
- C#利用Emit反射实现AOP,以及平台化框架封装思路
C#利用Emit反射实现AOP,以及平台化框架封装思路 这是前两天扒的一段动态代理AOP代码,用的Emit反射生成子类来实现代理模式,在这里做个小笔记,然后讨论一下AOP框架的实现思路. 首先是主函数 ...
- Unity容器中AOP应用示例程序
转发请注明出处:https://www.cnblogs.com/zhiyong-ITNote/p/9127001.html 实在没有找到Unity容器的AOP应用程序示例的说明,在微软官网找到了教程( ...
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- Unity(IOC)学习笔记
原文:Unity(IOC)学习笔记 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79432 ...
- 利用Unity制作“表”
一枚小菜鸟 目前没发现在Unity有其他路径制作类似于c# WinForm中的表:但是利用Unity自带的UGUI,制作了一张"伪表",具体方案如下: 效果图如下: 步骤: 1 ...
- IOC AOP 设计模式
IOC AOP 不是什么技术而是一种设计模式 学习 IOC AOP 其实是在学习一种思想. 1.IOC IOC其实是 将对象的创建和获取提取到外部.由外部IOC容器提供需要的组件. 看下面代码: p ...
随机推荐
- 2019/02/09 对于KinectFusion 的理解
网上有很多关于Kinect Fusion 的详细介绍,包括各个部分的算法,思路,以及应用上的限制和优化. 在此就不多介绍了. KinectFusion 提供了非常基础的用RGB-D 相机实现的 Den ...
- linux 常见技巧
1.# :表示权限用户(如:root) $:表示普通用户 开机提示:login:输入用户名 password:输入口令 用户是系统注册用户成功登陆后, 可以进入相应的用户环境. 退出当前shell,输 ...
- kafka工作原理介绍
两张图读懂kafka应用: Kafka 中的术语 broker:中间的kafka cluster,存储消息,是由多个server组成的集群. topic:kafka给消息提供的分类方式.brok ...
- 将数据转换成树型层级的Json格式的String
有时候我们项目里面需要将树型关系的数据转换成Json格式的String字符串 假设数据库中的数据如下 需要转换后输出的字符串的结果如下(对应的层级的关系) [ {name:'爷爷',id:'1',co ...
- biaffineparser
代码: 一.pytorch,可以直接跑 https://github.com/chantera/biaffineparser python parser.py train --out model -- ...
- python 启动时报错无法正常启动(0xc000007b)请单击“确定”关闭应用程序的解决办法
这是一个自己非常傻逼的问题,但是还是想记录下来 晚上安装python,不管是命令提示符中运行还是python直接打开,都提示报错 各种百度,各种查找排除以后,皆不能解决错误 最后发现:特么64位系统下 ...
- 【转存】阿里云服务器下 LAMP 环境配置 —— 基于 CentOS 6.3
阿里云服务器下 LAMP 环境配置 —— 基于 CentOS 6.3 Posted on 2016年2月10日 by 学院君 1.Apache 配置 —————————————————– vi / ...
- Ubuntu16.04中如何启用floodlight的一种方式
在 floodlight文件夹下输入 java -jar target/floodlight.jar 启动floodlight控制器,在浏览器窗口输入 http://localhost:8080/ui ...
- Git 几个常用操作
git init -- 初始化仓库, git clone -- 从远端克隆仓库到本地 git status -- 查看git仓库的状态 git log ...
- 【puppeteer】前端自动化初探(一)
一.前提 windows环境的puppeteer环境配置要简单点,mac环境坑竟然有点多,这边稍微提下 二.开发环境 nodejs puppeteer mac 三.简单介绍下puppeteer Pup ...