构造器注入(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. iTool拷贝app到电脑上

    iTool拷贝app到电脑上 方法一. iTool找到你的app, 归档在桌面, 桌面就生成了ipa, 其实ipa是一个压缩包, 使用解压软件解压之后 生成Payload文件夹, 点开就可以看到Clo ...

  2. spring 部分配置内容备忘

    1.spring定时器简单配置: <bean name="taskJob" class="com.netcloud.mail.util.TaskJob"& ...

  3. U盘安装ubuntu server 14.04

    U盘安装ubuntu server 14.04 U盘安装ubuntu server 14.04 1.制作启动u盘 2.开始安装 1 将u盘插入主机,重启后从u盘启动 2 选择语言(随便挑,随便选),我 ...

  4. 服务器设置SSH 长连接

    1.echo $TMOUT 如果显示空白,表示没有设置, 等于使用默认值0, 一般情况下应该是不超时. 如果大于0, 可以在如/etc/profile之类文件中设置它为0. 2.修改/etc/ssh/ ...

  5. GIT文件的三种状态

    对于任何一个文件,在 Git 内都只有三种状态:已提交(committed),已修改(modified)和已暂存(staged).已提交表示该文件已经被安全地保存在本地数据库 中了:已修改表示修改了某 ...

  6. uniq命令注意事项,检查重复行的时候,只会检查相邻的行。

    今天在使用uniq命令统计数量时,uniq -c总是得不到想要的效果,相同的行没有合并,例如 后来在http://ju.outofmemory.cn/entry/78365才看到,原来uniq检查重复 ...

  7. keystone v3 相关介绍

    1) 涉及到如下几个概念:User.Tenant.Role.Token.http://www.ibm.com/developerworks/cn/cloud/library/1506_yuwz_key ...

  8. 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战

    转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...

  9. struts2 结果页面配置

    <result>标签: * 属性: * name:逻辑视图的名称 * type:结果页面类型. * dispatcher         :转发.默认值. * redirect       ...

  10. Linux下安装gcc和g++

    以CentOS为例,安装后是没有C语言和C++编译环境的,需要手动安装,最简单的是用yum的方式安装,过程如下: 1.安装gcc yum install gcc 询问是否,按y键回车即可,或者 yum ...