构造器注入(Constructor Injection):IoC容器会智能地选择选择和调用适合的构造函数以创建依赖的对象。如果被选择的构造函数具有相应的参数,IoC容器在调用构造函数之前会自定义创建相应参数对象;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//1.创建一个UnityContainer对象
//2.通过UnityContainer对象的RegisterType方法来注册对象与对象之间的关系
//3.通过UnityContainer对象的Resolve方法来获取指定对象关联的对象
UnityContainer container = new UnityContainer();//创建容器
container.RegisterType<IWaterTool, PressWater>();//注册依赖对象 默认注册(无命名),如果后面还有默认注册会覆盖前面的
container.RegisterType<IWaterTool, PressWater>("PressWater1"); //命名注册
IWaterTool water = container.Resolve<IWaterTool>();
IWaterTool water1 = container.Resolve<IWaterTool>("PressWater1");
Console.WriteLine(water.returnWater());
Console.WriteLine(water1.returnWater());
Console.Read();
}
} /// <summary>
/// 压水井
/// </summary>
public class PressWater : IWaterTool
{
public string returnWater()
{
return "地下水好甜啊!!!";
}
} /// <summary>
/// 获取水方式接口
/// </summary>
public interface IWaterTool
{
string returnWater();
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//1.在配置文件<configSections> 配置节下注册名为unity的section
//2.在<configuration> 配置节下添加Unity配置信息
//3.在代码中读取配置信息,并将配置载入到UnityContainer中
IUnityContainer container = new UnityContainer();
container.LoadConfiguration("MyContainer");
UnityConfigurationSection section
= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");//获取指定名称的配置节
section.Configure(container, "MyContainer");//获取特定配置节下已命名的配置节<container name='MyContainer'>下的配置信息 IWaterTool water = container.Resolve<IWaterTool>("PressWater");
Console.WriteLine(water.returnWater());
Console.Read();
}
} /// <summary>
/// 压水井
/// </summary>
public class PressWater : IWaterTool
{
public string returnWater()
{
return "地下水好甜啊!!!";
}
} /// <summary>
/// 获取水方式接口
/// </summary>
public interface IWaterTool
{
string returnWater();
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration> <configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections> <unity>
<!--定义类型别名-->
<aliases>
<add alias="IWaterTool" type="ConsoleApplication1.IWaterTool,ConsoleApplication1" />
<add alias="PressWater" type="ConsoleApplication1.PressWater,ConsoleApplication1" />
</aliases>
<!--容器-->
<container name="MyContainer">
<!--映射关系-->
<register type="IWaterTool" mapTo="PressWater" name="PressWater"></register>
</container>
</unity> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
</configuration>

属性注入(Property Injection):如果需要使用到被依赖对象的某个属性,在被依赖对象被创建之后,IoC容器会自动初始化该属性;

    class Program
{
static void Main(string[] args)
{
UnityContainer container = new UnityContainer();
container.RegisterType<IPeople, VillagePeople>();
container.RegisterType<IWaterTool, PressWater>();
IPeople people = container.Resolve<IPeople>();
Console.WriteLine(people._pw.returnWater());
people.DrinkWater();
Console.Read();
}
} /// <summary>
/// 压水井
/// </summary>
public class PressWater : IWaterTool
{
public string returnWater()
{
return "地下水好甜啊!!!";
}
} /// <summary>
/// 获取水方式接口
/// </summary>
public interface IWaterTool
{
string returnWater();
} /// <summary>
/// 人接口
/// </summary>
public interface IPeople
{
IWaterTool _pw { get; set; }
void DrinkWater();
} /// <summary>
/// 村民
/// </summary>
public class VillagePeople : IPeople
{
[Dependency]
public IWaterTool _pw { get; set; }
public VillagePeople(IWaterTool pw)
{
_pw = pw;
}
public void DrinkWater()
{
Console.WriteLine(_pw.returnWater());
}
}

方法注入(Method Injection):如果被依赖对象需要调用某个方法进行相应的初始化,在该对象创建之后,IoC容器会自动调用该方法。

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
UnityContainer container = new UnityContainer();
container.RegisterType<IPeople, VillagePeople>();
container.RegisterType<IWaterTool, PressWater>();
IPeople people = container.Resolve<IPeople>();
Console.WriteLine(people._pw.returnWater());
people.DrinkWater();
Console.Read();
}
} /// <summary>
/// 压水井
/// </summary>
public class PressWater : IWaterTool
{
public string returnWater()
{
return "地下水好甜啊!!!";
}
} /// <summary>
/// 获取水方式接口
/// </summary>
public interface IWaterTool
{
string returnWater();
} /// <summary>
/// 人接口
/// </summary>
public interface IPeople
{
IWaterTool _pw { get; set; }
void DrinkWater();
void ObjectInit();
} /// <summary>
/// 村民
/// </summary>
public class VillagePeople : IPeople
{
[Dependency]
public IWaterTool _pw { get; set; }
public VillagePeople(IWaterTool pw)
{
_pw = pw;
}
public void DrinkWater()
{
Console.WriteLine(_pw.returnWater());
}
[InjectionMethod]
public void ObjectInit()
{
Console.WriteLine("方法注入");
}
}
}

Unity依赖注入使用的更多相关文章

  1. Unity 依赖注入之二

    1. 构造子注入 1.1 构造子注入初级代码 container.RegisterType<IMyWork, MyWork>(new InjectionConstructor(new Bo ...

  2. Unity依赖注入使用详解

    写在前面 构造器注入 Dependency属性注入 InjectionMethod方法注入 非泛型注入 标识键 ContainerControlledLifetimeManager单例 Unity注册 ...

  3. WPF PRISM开发入门二(Unity依赖注入容器使用)

    这篇博客将通过一个控制台程序简单了解下PRISM下Unity依赖注入容器的使用.我已经创建了一个例子,通过一个控制台程序进行加减乘除运算,项目当中将输入输出等都用接口封装后,结构如下: 当前代码可以点 ...

  4. C# Unity依赖注入

    简介: 控制反转:我们向IOC容器发出获取一个对象实例的一个请求,IOC容器便把这个对象实例“注入”到我们的手中,在这个过程中你不是一个控制者而是一个请求者,依赖于容器提供给你的资源,控制权落到了容器 ...

  5. Unity 依赖注入

    关于Ioc的框架有很多,比如astle Windsor.Unity.Spring.NET.StructureMap,我们这边使用微软提供的Unity做示例,你可以使用Nuget添加Unity,也可以引 ...

  6. c# Unity依赖注入WebService

    1.IOC与DI简介 IOC全称是Inversion Of Control(控制反转),不是一种技术,只是一种思想,一个重要的面相对象编程的法则,它能知道我们如何设计出松耦合,更优良的程序.传统应用程 ...

  7. 使用Microsoft.Practices.Unity 依赖注入

    Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...

  8. 使用Microsoft.Practices.Unity 依赖注入 转载https://www.cnblogs.com/slardar1978/p/4205394.html

    Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...

  9. ASP.NET MVC5+EF6+EasyUI 后台管理系统(6)-Unity 依赖注入

    系列目录 前言 为了符合后面更新后的重构系统,文章于2016-11-1日重写 本节重构一下代码,采用IOC控制反转,也就是依赖注入 您可以访问http://unity.codeplex.com/rel ...

  10. Unity 依赖注入知识点

    三种依赖注入方法,构造器注入.属性注入.方法注入 可以配置Config文件,来实现不用修改代码.需要先将接口与实体关联,然后使用时会自动加载对应实体. namespace WeChatConsole ...

随机推荐

  1. [BZOJ1833][ZJOI2010]count 数字计数

    [BZOJ1833][ZJOI2010]count 数字计数 试题描述 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 输入 输入文件中仅包含一行两个整数a ...

  2. phpcms常用标签

    http://v9.help.phpcms.cn/html/pc_tag/modules/ 9帮助中心 {template "content","header" ...

  3. SMT 的基本流程?SMT的工艺流程?SMT的设备操作?

    一.SMT工艺流程------单面组装工艺来料检测 --> 丝印焊膏(点贴片胶)--> 贴片 --> 烘干(固化) --> 回流焊接 --> 清洗 --> 检测 - ...

  4. Android 简易XML解析

    首先创建在Android工程中创建一个Assets文件夹 app/src/main/assets 在这里添加一个名为 data.xml的文件,然后编辑这个文件,加入如下XML格式内容 <?xml ...

  5. 百度地图api 常用demo

    功能一:获取map地图窗口的可视区域: var map = new BMap.Map("allmap");            // 创建Map实例 map.centerAndZ ...

  6. 一个按比特位拷贝数据的函数copybits

    一个按比特位拷贝数据的函数 没有进行特别的优化.其实还可以在拷贝源开始位置和目标开始位置是2的整数倍位置的时候进行优化. 说明 这个函数用于从src数组首地址跳过sbb个字节,又跳过ssb个比特位,拷 ...

  7. 6 HandlerDescriptor 处理程序描述类——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...

  8. PHP 常量

    1.__NAMESPACE__           http://php.net/manual/zh/language.namespaces.nsconstants.php

  9. java mail api 使用

    所需要的jar包: http://pan.baidu.com/s/1qWGZRJm 如果遇到这个错误:在windows防火墙允许 javaw.exe访问网络.或者关闭防火墙 FATAL ERROR i ...

  10. ffmpeg-20160517-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...