运用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 ...
随机推荐
- JSON APIs and Ajax
1. 通过jQuery来绑定点击事件. 函数 $(document).ready()这个函数中的代码只会在我们的页面加载时候运行一次,确保执行js之前页面所有的dom已经准备就绪. 在$(docume ...
- 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...
- [转]Web.xml配置详解之context-param
转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6759206 格式定义: [html] view plaincopy <co ...
- [转] javax.servlet.jar - jar not loaded问题解决
把那个jsp-api.jarservlet-api.jar删除即可! Details:把 webapps\maintenance\WEB-INF\lib\ 下面的 servlet-api.jar 删掉 ...
- css3背景属性 background-size 对背景图进行缩小放大
background-size需要两个值,它的类型可以是像素(px).百分比(%)或是auto,还可以是cover和contain.第一个值为背景图的width,另外一个值用于指定背景图上的heigh ...
- bcp功能
#include "MyBCP.h" #include "odbcss.h" //1,Allocate an environment handle and a ...
- 探索 vuex 2.0 以及使用 vuejs 2.0 + vuex 2.0 构建记事本应用23
前言 首先说明这并不是一个教程贴,而记事本应用是网上早有的案例,对于学习 vuex 非常有帮助.我的目的是探索 vuex 2.0 ,然后使用 vuejs 2.0 + vuex 2.0 重写这个应用,其 ...
- vue-router 2.0 改变的内容
2.x 版本的 vue-router 相比之前的0.7.x版本,有很多破坏性改变: 通用 API 的修改 The old router.go() is now router.push() . 新的 r ...
- node webkit (nw.js) 无法调试的结局方案之一
之前做过nw项目,当时主要内容是由别人做的!过后回到家中,自己研究了下这方面.结果发现我自己写的nw 客户端不可以调试!在网上各种找办法,没找到,深感绝望,突然看到 (https://github.c ...