Castle.Windsor TypedFactoryFacility可以构造,只有一些参考参数传递的直列类型? [英] Can Castle.Windsor TypedFactoryFacility construct a type with only some ref arguments passed inline?

查看:238
本文介绍了Castle.Windsor TypedFactoryFacility可以构造,只有一些参考参数传递的直列类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Castle.Windsor的TypedFactoryFacility概念打了一个问题,不知道如何解决它(或者,如果我可以)。

I have hit a problem in my use of Castle.Windsor's TypedFactoryFacility concept, and not sure how to resolve it (or if I can).

我有一个视图模型定义像这样;

I have a ViewModel defined like so;

public class MyViewModel : IMyViewModel
{
    // constructor which I aim to access through the factory
    public MyViewModel(
        ISomeContainerResolvableDependency a,
        ISomePassedInDependency b)
    { ... }
}

而像定义相应的工厂接口;

And a corresponding factory interface defined like;

public interface IMyViewModelFactory 
{
    IMyViewModel Create(ISomePassedInDependency a);

    void Release(IMyViewModel vm);
}



然而,当我尝试打电话给厂里一个有效的,非空 ISomePassedInDependency 像这样的实例;

ISomePassedInDependency o = new SomePassedInDependency();
IMyViewModelFactory factory = IoC.Get<IMyViewModelFactory>();
IMyViewModel viewModel = factory.Create(o);



我得到一个异常说明温莎城堡无法从 IMyViewModel解决的依赖 ISomePassedInDependency 。在这种情况下,我供给 ISomePassedInDependency 实例,并且只想设施解决 ISomeContainerResolvableDependency 我。

I get an Exception stating that Castle Windsor could not resolve the dependency from IMyViewModel on ISomePassedInDependency. In this case, I am supplying the ISomePassedInDependency instance, and only want the facility to resolve ISomeContainerResolvableDependency for me.

如果我改变构造函数的定义来接受我传递一个值类型( INT ,例如),而不是一个 ISomePassedInDependency 实例,工厂通话效果。因此,似乎有温莎城堡内的假设,即所有引用类型将容器解决,什么都不会呢?

If I change the constructor definition to accept a value type which I pass in (int, for example) instead of an ISomePassedInDependency instance, the factory call works. So, it seems that there is an assumption inside Castle Windsor that all reference types will be container resolved and anything else will not?

有一些方法,使这项工作?

Is there some way to make this work?

推荐答案

OK,我重读的文档,发现我忽略了信息的一个关键部分;在出厂参数名的必须匹配的对象中的参数名称中实例化。

OK, I re-read the documentation and noticed a key piece of info I had overlooked; the argument names in the factory must match the argument names in the object being instantiated.

所以,在我上面的例子,如果构造视图模型更改为;

So, in my example above, if the constructor of the ViewModel is changed to;

public MyViewModel(
    ISomeContainerResolvableDependency x,
    ISomePassedInDependency a) // <-- NOTE this argument is now named 'a' to match the factory definition
{ ... }    

它的工作原理。

魔术队。

这篇关于Castle.Windsor TypedFactoryFacility可以构造,只有一些参考参数传递的直列类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆