Castle.Windsor:使用空受保护构造函数解析类型

发布于 2024-09-27 16:13:52 字数 655 浏览 1 评论 0原文

我正在使用 NHibernate + Castle.Windsor 向我的实体添加一些行为。这意味着 NHibernate 通过 Windsor 创建实体。这意味着我必须有一个默认的空构造函数,以便 Windsor 能够实例化我的实体。我不喜欢这个有很多原因,最主要的是我不喜欢让对象处于不稳定状态。

有什么方法可以在没有公共无参数构造函数的情况下实例化实体吗?

更多信息:我尝试为我的实体使用自定义激活器,但这不起作用,因为温莎在检测到无法解决依赖关系时会抛出异常,因此在使用我的激活器之前它会崩溃。

一些代码:

public class Product
{
    public string Name { get; set; }
    public Product(string name)
    {
        Name = name;
    }
    protected Product() { }
}

假设我有上述实体:如果我尝试通过 Windsor 解析该实体,它会抛出一个异常,告诉我 Product 正在等待一些未注册的依赖项。如果我单独使用 NHibernate,一切都会正常工作。因此,基本上我试图在该场景中模拟 NHibernate 行为,同时通过 Windsor 解析我的实体。

提前致谢,

豪尔赫·巴尔加斯。

I'm using NHibernate + Castle.Windsor to add some behaviour to my entities. This means that NHibernate creates entities through Windsor. This means that I must have a default empty constructor so Windsor will be able to instantiate my entities. I don't like this for many reasons, the main one being that I dislike to have objects in an unstable state.

Is there any way I can instantiate an entity without a public parameterless constructor?

More info: I tried using a custom Activator for my entities but that doesn't work since Windsor throws an exception when it detects that there's no way to resolve dependencies, so it blows before using my activator.

Some code:

public class Product
{
    public string Name { get; set; }
    public Product(string name)
    {
        Name = name;
    }
    protected Product() { }
}

Let's suppose I have the above entity: If I try to resolve that entity through Windsor it throws an exception telling me that Product is waiting for some dependencies that weren't registered. If I use NHibernate alone, everything works correctly. So, basically I'm trying to emulate NHibernate behaviour in that scenario while resolving my entities through Windsor.

Thanks in advance,

Jorge Vargas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2024-10-04 16:13:52

您不需要在 NHibernate 实体中使用无参数构造函数或编写自定义 Windsor 激活器。请参阅本文

You don't need to have parameterless constructors in your NHibernate entities or write custom Windsor activators. See this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文