运用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 ...
随机推荐
- Bzoj4016/洛谷P2993 [FJOI2014] 最短路径树问题(最短路径问题+长链剖分/点分治)
题面 Bzoj 洛谷 题解 首先把最短路径树建出来(用\(Dijkstra\),没试过\(SPFA\)\(\leftarrow\)它死了),然后问题就变成了一个关于深度的问题,可以用长链剖分做,所以我 ...
- Python 头文件和常用函数
#coding=utf-8 """ @version: ?? @author: Donny @Mail: wdm666666@gmail.com @license: La ...
- CSU - 1334 -好老师(STL-map用法)
https://cn.vjudge.net/contest/157163#problem/E #include<map> #include<queue> #include< ...
- [BZOJ4859][BJOI2017]机动训练(DP)
4859: [BeiJing2017]机动训练 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 105 Solved: 63[Submit][Stat ...
- 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- 【动态规划+高精度】mr360-定长不下降子序列
[题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...
- 关于abstract class 和 interface
1.abstract class 在 Java 语言中表示的是一种继承关系,一个类只能使用一次继承关系.但是,一个类却可以实现多个interface. 2.在abstract class 中可以有自己 ...
- MYSQL复习笔记4-基本SQL语句
Date: 20140115Auth: Jin参考:http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select一:数据库操作1:显示数据 ...
- 版本控制SVN的使用笔记
安装 客户端和服务端下载地址,打开网址,根据自己的操作系统下载对应的版本,window用户服务端一般安装的是VisualSVN,客户端安装TortoiseSVN,在实际工作中,我们一般只需要安装Tor ...
- 第七章Openwrt安装服务器环境php+uhttpd+mysql
在前面的文章中刷openwrt.配置网络环境.挂载u盘都配置成功了之后,下面的操作就变得简单起来!!!! 1. putty连接到路由器 2. 安装php opkg install php5-fastc ...