- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看了这个,它回答了我一半的问题:
Castle Windsor: Register class with internal constructor?
但是您可以使用 Windsor 来使用内部构造函数/类以及依赖项注入(inject)吗? (所以构造函数参数也被注入(inject))?我希望将类/构造函数保留在内部,以实现最佳封装(这些类不应公开给公众)。
我需要这个来支持 Silverlight,所以我认为这不是一个选择:
Castle Windsor: How to register internal implementations
谢谢。
最佳答案
这可能不是一个令人满意的答案,但最佳实践是使用公共(public)构造函数将您需要通过 CaSTLe 实例化的所有类设为公共(public)。您的设计应该允许下游依赖项实例化您的对象,而不依赖于 CaSTLe 或 InternalsVisibleTo。
对于你的问题,CaSTLe 只会搜索公共(public)构造函数来实例化对象。我不相信有办法让它搜索内部或私有(private)构造函数。但是,如果您的类是内部类,则可以将内部构造函数公开,而无需更改封装。请参阅以下测试用例:
[TestFixture]
public class InternalConstructorTests
{
[Test]
public void Test()
{
using (var container = new WindsorContainer())
{
container.Register(
Component.For<IFoo>().ImplementedBy<Foo>(),
Component.For<IBar>().ImplementedBy<Bar>(),
Component.For<IBaz>().ImplementedBy<Baz>()
);
// fails because Castle can't find, and won't call, the internal constructor
Assert.Throws<ComponentActivatorException>(()=>container.Resolve<IFoo>());
// passes because the Baz constructor is public, but the "real" encapsulation
// level is the same because the class is internal, effectively making the
// public constructor internal as well
container.Resolve<IBaz>();
}
}
}
internal interface IBar{}
internal class Bar : IBar{}
internal interface IFoo{}
internal class Foo : IFoo
{
internal Foo(IBar bar)
{
}
}
internal interface IBaz { }
internal class Baz : IBaz
{
public Baz(IBar bar)
{
}
}
关于.net - 温莎城堡内部构造函数/类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4017068/
关于我的 MVP 实现的快速问题: 目前我有下面的代码,其中 presenter 和 view 都是通过容器解析的。 然后演示者调用 View.Init 将自己传递给 View 。 但是我想知道是否有
我正在尝试在 NHibernate 上用 CaSTLe 做一个简单的“从产品中选择计数(*),日期 > xxx”。 如果我直接使用 NHibernate,我可以重用 this question答案但不
刚接触城堡/温莎,请耐心等待。 我目前正在使用框架System.Web.Mvc.Extensibility在其启动代码中,它注册了 HttpContextBase,如下所示: container.Re
您好,我在尝试初始化 ActiveRecord 时遇到异常,我不知道我遗漏了什么。我正试图说服我工作的公司使用 CaSTLe ActiveRecord,如果我不能演示它是如何工作的,它看起来不会很好。
对于我当前的项目,我在 C# 中使用 CaSTLe 的 ActiveRecord。对于我的一张表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度的转换)。为了保持我的代码干净,我喜欢在对象映射类
在 IIS 中托管的 Windsor ioc、wcf 设施设置下,raven doc session 和存储的推荐生活方式是什么? 我一直看到这个错误: Error TempPathInUse (JE
我决定将我的项目 ASP.NET MVC 4 中的 CaSTLe ActiveRecord 版本从 2.1.2 版本迁移到 3.0.0。还更新了 NHibernate 2.1.2 版本到 3.1.0
我是一名优秀的程序员,十分优秀!