unity让对象作为参数_运用Unity实现依赖注入[有参构造注入]

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

本文同样通过一个实例来讲解如何实现此功能,文中一些分层讲解可以看上一文章(运用Unity实现依赖注入[结合简单三层实例]),本文就不在重复;

1:首先我们在IAopBLL层新建一个IPropertyBLL类,我们增加的两个属性:name跟age

namespaceIAopBLL

{public interfaceIPropertyBLL

{string name { set; get; }int age { set; get; }voidShowInfo();voidOutShowName();

}

}

2:逻辑实现接口的代码如下

usingIAopDAL;usingIAopBLL;usingCommand;namespaceAopBLL

{public classPropertyBLL:IPropertyBLL

{

IReadData bllServer= new UnityContainerHelp().GetServer();public string name { set; get; }public int age { get; set; }public PropertyBLL(string Name,intAge)

{this.name =Name;this.age =Age;

}public voidShowInfo()

{

Console.WriteLine(bllServer.ReadDataStr(name));

}public voidOutShowName()

{

Console.WriteLine("我是从构结函数得到Name的值:{0} 而Age的值:{1}",name,age);

}

}

}

*这边有个要注意,因为Unity会自动使用参数最多的构造函数来进行创建对象,假如在这个类中有多个构造函数时,而我们要指定其中一个作为Unity进行创建对象则必需用到[InjectionConstructor],它是在Microsoft.Practices.Unity下面,要对DLL进行引用;比如下面我们使用到一个无参的构造函数让Unity进行创建对象:

usingIAopDAL;usingIAopBLL;usingCommand;usingMicrosoft.Practices.Unity;namespaceAopBLL

{public classPropertyBLL:IPropertyBLL

{

IReadData bllServer= new UnityContainerHelp().GetServer();public string name { set; get; }public int age { get; set; }public PropertyBLL(string Name,intAge)

{this.name =Name;this.age =Age;

}

[InjectionConstructor]publicPropertyBLL()

{

}public voidShowInfo()

{

Console.WriteLine(bllServer.ReadDataStr(name));

}public voidOutShowName()

{

Console.WriteLine("我是从构结函数得到Name的值:{0} 而Age的值:{1}",name,age);

}

}

}

3:接着我们修改公共层里的助手类,增加的方法public T GetServer(Dictionary parameterList)其中parameterList是我们用来定义构造函数集合;

usingMicrosoft.Practices.Unity;usingMicrosoft.Practices.Unity.Configuration;usingMicrosoft.Practices.Unity.InterceptionExtension;usingMicrosoft.Practices.Unity.InterceptionExtension.Configuration;usingSystem.Configuration;usingSystem.Reflection;namespaceCommand

{public classUnityContainerHelp

{privateIUnityContainer container;publicUnityContainerHelp()

{

container= newUnityContainer();

UnityConfigurationSection section= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

container.LoadConfiguration(section,"FirstClass");

}public T GetServer()

{return container.Resolve();

}///

///

///

///

/// 配置文件中指定的文字

///

public T GetServer(stringConfigName)

{return container.Resolve(ConfigName);

}///

///返回构结函数带参数///

/// 依赖对象

/// 配置文件中指定的文字(没写会报异常)

/// 参数集合(参数名,参数值)

///

public T GetServer(DictionaryparameterList)

{var list = newParameterOverrides();foreach (KeyValuePair item inparameterList)

{

list.Add(item.Key, item.Value);

}return container.Resolve(list);

}

}

}

4:配置文章里我们增加一个注册依赖对象的节点

5:接着看一下主程序代码,其中Dictionary存储参数的名称跟其对应的值,因为我们的值有可能是不同类型的所以使用object

usingIAopBLL;usingCommand;namespaceAopUnity

{classProgram

{static void Main(string[] args)

{

Dictionary parameterList = new Dictionary();

parameterList.Add("Name", "踏浪帅2");

parameterList.Add("Age", 27);

IPropertyBLL bllProperty= new UnityContainerHelp().GetServer(parameterList);

Console.WriteLine("--------运行方法ShowInfo()---------");

bllProperty.ShowInfo();

Console.WriteLine("--------运行方法OutShowName()---------");

bllProperty.OutShowName();

Console.WriteLine("-----------------------------------");

}

}

}

6:运行效果:

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。  因为,我的写作热情也离不开您的肯定支持。

感谢您的阅读(因为源代码现在我正接着写Unity实现AOP的功能,所以将在实现功能后一起贴出)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值