简体   繁体   English

如果无法解析属性,我如何告诉 Windsor 出错?

[英]How do I tell Windsor to error if it can't resolve a property?

Say I have the following classes说我有以下课程

    public class TestA
    {
        public string Blah { get; set; }
    }

    public class TestB
    {
        public TestA Testing { get; set; }
    }

So say I want Windsor to give me an instance of TestB , but I also want it to use property injection to instantiate the TestB.Testing property.所以说我希望 Windsor 给我一个TestB的实例,但我也希望它使用属性注入来实例化TestB.Testing属性。 This part is easy, but I need a way to know if the Windsor registration for TestA is removed.这部分很简单,但我需要一种方法来知道是否删除了TestA的 Windsor 注册。

Is there any way to mark the Testing property so that Windsor knows that TestA is a dependency of TestB , and thus should error out if it can't resolve the Testing property?有没有办法标记Testing属性,以便 Windsor 知道TestATestB的依赖项,因此如果它无法解析Testing属性应该出错? I do not want to use the TestB constructor, as I am looking at ways to lower constructor bloat.我不想使用TestB构造函数,因为我正在寻找降低构造函数膨胀的方法。

If it is a mandatory dependency it should go into the constructor period.如果它是强制依赖,它应该 go 进入构造函数期间。 Otherwise you're allowing to instantiate an invalid component, and this has nothing to do with Windsor or IoC.否则,您将允许实例化无效组件,这与 Windsor 或 IoC 无关。

"Constructor bloat" can sometimes be a sign that the code needs to be refactored, it may have acquired too many responsibilities. “构造函数膨胀”有时可能表明代码需要重构,它可能承担了太多责任。

EDIT: if you really know what you're doing you can "manually" change dependencies as optional or mandatory using a component model contributor .编辑:如果您真的知道自己在做什么,则可以使用组件 model 贡献者“手动”将依赖项更改为可选或强制。 I'd argue that this should only be used for code that you can't control, almost never for your own code.我认为这应该只用于您无法控制的代码,几乎从不用于您自己的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决该错误? - How do I can to resolve this error? 如何告诉Web API / Castle Windsor路由引擎在我的存储库中使用其他数据库实例? - How can I tell the Web API / Castle Windsor routing engine to use a different database instance in my Repository? 我如何单元测试Windsor可以解决Asp.net MVC3控制器依赖? - How can I unit test that Windsor can resolve Asp.net MVC3 controller dependencies? 我如何对我的控制器进行单元测试,以确保Windsor在使用PerWebRequestLifestyle时可以解析依赖关系 - How can I unit test my controller to make sure Windsor can resolve dependencies when using PerWebRequestLifestyle 如何判断某物是属性还是方法? - How can I tell if something is a property or a method? 如何判断 class 是否可以序列化? - How do I tell if a class can be serialized? 我该如何在温莎城堡注册? - How do would I register this in Castle Windsor? 如何命名温莎城堡中的容器? - How can I name a container in Castle Windsor? 如何判断类属性是否具有公共集(.NET)? - How do I tell if a class property has a public set (.NET)? Castle Windsor:当我的装配无法访问基础类型时,如何注册工厂方法? - Castle Windsor: How do I register a factory method, when the underlying type isn't accessible to my assembly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM