How does Unity.Resolve know which constructor to use?

Question:

Given a class with several constructors - how can I tell Resolve which constructor to use?

Consider the following example class:

public class Foo
{
    public Foo() { }
    public Foo(IBar bar)
    {
        Bar = bar;
    }
    public Foo(string name, IBar bar)
    {
        Bar = bar;
        Name = name;
    }
    public IBar Bar { get; set; }
    public string Name { get; set; }
}

If I want to create an object of type Foo using Resolve how will Resolve know which constructor to use? And how can I tell it to use the right one? Let‘s say I have a container with an IBar registered - will it understand that it should favor the constructor taking IBar? And if I specify a string too - will it use the (string, IBar) constructor?

Foo foo = unityContainer.Resolve<Foo>();

And please ignore the fact that it would probably be easier if the class just had a single constructor...

Answer:

When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the “longest” with the same number of parameters), Unity will raise an exception.

时间: 2024-08-29 00:03:29

How does Unity.Resolve know which constructor to use?的相关文章

Unity: Passing Constructor Parameters to Resolve

ebruary 11, 2012 by mikael koskinen 2 comments In this tutorial we will go through of couple different ways of using custom constructor parameters when resolving an instance with Unity: By using the built-in ParameterOverride By creating a custom Res

Unity 学习笔记(1):认识Unity

Unity是什么? Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能还包含了其它被依赖的对象实例. Unity允许将预先配置的对象注入到类中,实现了inversion of control (IoC)的功能.在Unity中,支持constructor injection(构造器注入), property setter injection(属性设值注入)以及method ca

Unity是什么?

Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 1. 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能还包含了其它被依赖的对象实例. 2. Unity允许将预先配置的对象注入到类中,实现了inversion of control (IoC)的功能.在Unity中,支持constructor injection(构造器注入), property setter injection(属性设值注入)以及method call i

(转)Overview : Writing Scripts in C# 使用C#书写脚本

Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are: 除了句法规则, 使用C#或Boo编写脚本还有一些不同,当.需要特别注意的是: 1. Inherit from MonoBehaviour继承自MonoBehaviour All behaviour scripts must inherit from MonoBehaviour (directly or

angular服务插件1——对话框&amp;提示框

对话框服务: ( 需要调用amazeui的modal样式 ) const angular = require("angular"); const jqLite = angular.element; /** * @ngdoc service * @name commonModal * @module common.modal.service * @usage commonModal.fromTemplateUrl('./modal.html',{ scope: $scope, }).th

使用Unity依赖注入的时候,最上层的类是Resolve还是New的问题

在使用Unity的时候,很多时候是这样一种引用的关系.就是一个类需要另一个类在其中做工具类.因为是构造方法注入,所以要在构造方法中加入一个引用参数. public interface IRepository { void Execute(); } public class Repository : IRepository { public void Execute() { // Some database operation } } public interface IService { voi

IoC之Unity实现

public static class Bootstrapper { private static ILog logger = LogManager.GetLogger(typeof(Bootstrapper)); static Bootstrapper() { try { logger.Info("start load container"); Container.InitializeWith(new DependencyResolverFactory()); } catch (Ex

[IoC容器Unity]第三回:依赖注入

上节介绍了,Unity的Lifetime Managers生命周期,Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍. 2.构造函数注入 Unity利用Resolve方法解析一个对象,都是调用注册类型的构造函数来初始化的,初始化时,Unity能够控制初始化的值,当然,我们要给Unity提供足够的原料,要不然也是巧妇难无米之炊,下面看一些简单的示例. 先准备几个类如下: /// <summary> /// 班级接口 /// </summa

Net Unity IOC注入总结

简介 Unity 应用程序块(Unity)是一个轻量级.可扩展的依赖注入容器,支持构造函数.属性和方法调用注入.它为开发人员提供了如下好处: 简化了对象的创建,尤其是分层的对象结构和依赖. 允许开发人员在运行时或者配置中指定依赖的需求抽象,以及简化了横切关注点的管理. 服务定位功能允许客户代码保存或者缓存容器.这在开发人员可以持久化容器到 ASP.NET Session 或者 Application 中的 ASP.NET Web 应用程序中特别有用. 配置文件(1)简单register方式 <?