运用Unity实现依赖注入[有参构造注入]
上一篇章讲到关于使用Unity实现依赖注入的简单功能,针对有博友提出关于有参构造注入的问题;

本文同样通过一个实例来讲解如何实现此功能,文中一些分层讲解可以看上一文章(运用Unity实现依赖注入[结合简单三层实例]),本文就不在重复;
1:首先我们在IAopBLL层新建一个IPropertyBLL类,我们增加的两个属性:name跟age
namespace IAopBLL
{
public interface IPropertyBLL
{
string name { set; get; } int age { set; get; } void ShowInfo(); void OutShowName();
}
}
2:逻辑实现接口的代码如下
using IAopDAL;
using IAopBLL;
using Command;
namespace AopBLL
{
public class PropertyBLL:IPropertyBLL
{
IReadData bllServer = new UnityContainerHelp().GetServer<IReadData>(); public string name { set; get; } public int age { get; set; } public PropertyBLL(string Name,int Age)
{
this.name = Name;
this.age = Age;
} public void ShowInfo()
{
Console.WriteLine(bllServer.ReadDataStr(name));
} public void OutShowName()
{
Console.WriteLine("我是从构结函数得到Name的值:{0} 而Age的值:{1}",name,age);
}
}
}
*这边有个要注意,因为Unity会自动使用参数最多的构造函数来进行创建对象,假如在这个类中有多个构造函数时,而我们要指定其中一个作为Unity进行创建对象则必需用到[InjectionConstructor],它是在Microsoft.Practices.Unity下面,要对DLL进行引用;比如下面我们使用到一个无参的构造函数让Unity进行创建对象:
using IAopDAL;
using IAopBLL;
using Command;
using Microsoft.Practices.Unity;
namespace AopBLL
{
public class PropertyBLL:IPropertyBLL
{
IReadData bllServer = new UnityContainerHelp().GetServer<IReadData>(); public string name { set; get; } public int age { get; set; } public PropertyBLL(string Name,int Age)
{
this.name = Name;
this.age = Age;
} [InjectionConstructor]
public PropertyBLL()
{
} public void ShowInfo()
{
Console.WriteLine(bllServer.ReadDataStr(name));
} public void OutShowName()
{
Console.WriteLine("我是从构结函数得到Name的值:{0} 而Age的值:{1}",name,age);
}
}
}
3:接着我们修改公共层里的助手类,增加的方法public T GetServer<T>(Dictionary<string,object> parameterList)其中parameterList是我们用来定义构造函数集合;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity.InterceptionExtension.Configuration;
using System.Configuration;
using System.Reflection; namespace Command
{
public class UnityContainerHelp
{
private IUnityContainer container;
public UnityContainerHelp()
{
container = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
container.LoadConfiguration(section, "FirstClass");
} public T GetServer<T>()
{
return container.Resolve<T>();
} /// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ConfigName">配置文件中指定的文字</param>
/// <returns></returns>
public T GetServer<T>(string ConfigName)
{
return container.Resolve<T>(ConfigName);
} /// <summary>
/// 返回构结函数带参数
/// </summary>
/// <typeparam name="T">依赖对象</typeparam>
/// <param name="ConfigName">配置文件中指定的文字(没写会报异常)</param>
/// <param name="parameterList">参数集合(参数名,参数值)</param>
/// <returns></returns>
public T GetServer<T>(Dictionary<string,object> parameterList)
{
var list = new ParameterOverrides();
foreach (KeyValuePair<string, object> item in parameterList)
{
list.Add(item.Key, item.Value);
}
return container.Resolve<T>(list);
}
}
}
4:配置文章里我们增加一个注册依赖对象的节点
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practces/2010/unity">
<container name="FirstClass">
<register type="IAopBLL.IReadDataBLL,IAopBLL" mapTo="AopBLL.ReadDataBLL,AopBLL">
</register>
<register type="IAopBLL.IPropertyBLL,IAopBLL" mapTo="AopBLL.PropertyBLL,AopBLL"></register>
<register type="IAopDAL.IReadData,IAopDAL" mapTo="AopOracelDAL.ReadDataDAL,AopOracelDAL"/>
</container>
</unity>
</configuration>
5:接着看一下主程序代码,其中Dictionary<string, object>存储参数的名称跟其对应的值,因为我们的值有可能是不同类型的所以使用object
using IAopBLL;
using Command;
namespace AopUnity
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> parameterList = new Dictionary<string, object>();
parameterList.Add("Name", "踏浪帅2");
parameterList.Add("Age", );
IPropertyBLL bllProperty = new UnityContainerHelp().GetServer<IPropertyBLL>(parameterList);
Console.WriteLine("--------运行方法ShowInfo()---------");
bllProperty.ShowInfo(); Console.WriteLine("--------运行方法OutShowName()---------");
bllProperty.OutShowName();
Console.WriteLine("-----------------------------------");
}
}
}
6:运行效果:

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。 因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读(因为源代码现在我正接着写Unity实现AOP的功能,所以将在实现功能后一起贴出)
运用Unity实现依赖注入[有参构造注入]的更多相关文章
- bean的装配方式(注入方式,构造注入,setter属性注入)
bean的装配方式有两种,构造注入和setter属性注入. public class User { private String username; private String password; ...
- IoC容器-Bean管理XML方式(创建对象和set注入属性,有参构造注入属性)
Ioc操作Bean管理 1,什么是Bean管理 (0)Bean管理指的是两个操作 (1)Spring创建对象 (2)Spring注入属性 2,Bean管理操作有两种方式 (1)基于xml配置文件方式实 ...
- Spring 设值注入 构造注入 p命名空间注入
注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...
- 7.28.1 Spring构造注入还是设置注入
1. 构造方法注入代码如下:public UserManagerImpl(UserDao userDao) { ...
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码]
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码] Unity 2.x依赖注入(控制反转)IOC,对 ...
- Asp.Net MVC4 使用Unity 实现依赖注入
项目创建参考 上一篇 <<Asp.Net MVC5 使用Unity 实现依赖注入>>, 不同的是这里是 Unity.MVC4 装好后会出现 然后示例说在这里写对应关系 ...
- Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)
构造注入 语法: <constructor-arg> <ref bean="bean的id"/> </constructor-arg> 1 ...
- IOC使用Unity 实现依赖注入
转自:http://www.cnblogs.com/techborther/archive/2012/01/06/2313498.html http://www.cnblogs.com/xishuai ...
随机推荐
- 【最短路径】 SPFA算法
上一期介绍到了SPFA算法,只是一笔带过,这一期让我们详细的介绍一下SPFA. 1 SPFA原理介绍 SPFA算法和dijkstra算法特别像,总感觉自己讲的不行,同学说我的博客很辣鸡,推荐一个视频讲 ...
- [BZOJ 2668] 交换棋子
Link: BZOJ 2668 传送门 Solution: 重点在于对于每条转移路径:首尾算一次,中间节点算两次 可以一点拆三点,将原流量拆成入流量和出流量 但其实也可以就拆两点,分前后是否是一首尾点 ...
- 【数论】【莫比乌斯反演】【线性筛】hdu6134 Battlestation Operational
看这个题解吧:http://blog.csdn.net/wubaizhe/article/details/77338332 代码里顺便把几个常用的线性筛附上了. Key:1.gcd(i,j)==1利用 ...
- 【KMP】BZOJ3942-[Usaco2015 Feb] Censoring
[题目大意] 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程.输出最后的S串. [思路]三 ...
- lightoj 1229 - Treblecross 博弈论
思路:SG函数 枚举先手的每一个位置是否有必胜. 1)如果出现了XXX则必胜: 2)如果出现了XX或X.X则必败: 3)否则计算后手的sg值和. 代码如下: #include<iostream& ...
- LeetCode 771. 宝石与石头(java)
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字符都是字母 ...
- RSA加密破解
1Linux 下安装gmpy2 https://www.cnblogs.com/ESHLkangi/p/8576113.html 2.yafu安装使用方法 https://www.cnblogs.co ...
- 【shiro】使用shiro,点击页面请求总是302状态码
解决方法: 配置shiro中,将要求放过的地址后面加上后缀,这里是.htmls 因为web.xml中配置所有的页面都是放过的
- The file “Info.plist” couldn’t be opened because there is no such file
修改了Info.plist的实际文件位置(项目和单元测试对应不同的Info.plist),报错 (null): could not read data from '/Users/xxxxx/Deskt ...
- python2和python3输入输出相关
python3: #coding=utf-8 a = input("请输入你的名字:") print("%s"%a) #输出没有一点问题,a就是字符串(或者数字 ...