无实现类型工厂问题

发布于 2024-11-27 06:37:20 字数 2428 浏览 0 评论 0原文

举这个简单的例子:

class Program
{
    static void Main(string[] args)
    {
        var windsorContainer = new WindsorContainer();
        windsorContainer.Install(new WindsorInstaller());

        var editor = windsorContainer.Resolve<IEditor>();
        editor.DoSomething();

        Console.ReadKey();
    }
}

public class WindsorInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>();

        container.Register(Component.For<ISomeOtherDependency>().ImplementedBy<SomeOtherDependency>());
        container.Register(Component.For<IReviewingService>().ImplementedBy<ReviewingService>());
        container.Register(Component.For<IEditor>().ImplementedBy<Editor>());
        container.Register(Component.For<Func<IReviewingServiceFactory>>().AsFactory());
    }
}

public interface IEditor
{
    void DoSomething();
}

public class Editor : IEditor
{
    private readonly Func<IReviewingServiceFactory> _reviewingService;

    public Editor(Func<IReviewingServiceFactory> reviewingService)
    {
        _reviewingService = reviewingService;
    }

    public void DoSomething()
    {
        var rs = _reviewingService();
        var reviews = new List<string> {"Review #1", "Review #2"};
        var reviewingService = rs.Create(reviews);

        reviewingService.Review();
    }
}

public interface IReviewingServiceFactory
{
    IReviewingService Create(IList<string> reviews);
}

public interface IReviewingService
{
    void Review();
}

public class ReviewingService : IReviewingService
{
    private readonly IList<string> _reviews;
    private readonly ISomeOtherDependency _someOtherDependency;

    public ReviewingService(IList<string> reviews, ISomeOtherDependency someOtherDependency)
    {
        _reviews = reviews;
        _someOtherDependency = someOtherDependency;
    }

    public void Review()
    {
        Console.WriteLine("Reviewing...");
    }
}

public interface ISomeOtherDependency
{
}

public class SomeOtherDependency : ISomeOtherDependency
{
}

在这个例子中,我希望控制台输出“正在审查...”。然而,Windsor 抛出异常:

No component for supporting the service CastleWindsorTypedFactor.IReviewingServiceFactory was found

我的 Windsor 安装程序出了什么问题?

Take this simple example:

class Program
{
    static void Main(string[] args)
    {
        var windsorContainer = new WindsorContainer();
        windsorContainer.Install(new WindsorInstaller());

        var editor = windsorContainer.Resolve<IEditor>();
        editor.DoSomething();

        Console.ReadKey();
    }
}

public class WindsorInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.AddFacility<TypedFactoryFacility>();

        container.Register(Component.For<ISomeOtherDependency>().ImplementedBy<SomeOtherDependency>());
        container.Register(Component.For<IReviewingService>().ImplementedBy<ReviewingService>());
        container.Register(Component.For<IEditor>().ImplementedBy<Editor>());
        container.Register(Component.For<Func<IReviewingServiceFactory>>().AsFactory());
    }
}

public interface IEditor
{
    void DoSomething();
}

public class Editor : IEditor
{
    private readonly Func<IReviewingServiceFactory> _reviewingService;

    public Editor(Func<IReviewingServiceFactory> reviewingService)
    {
        _reviewingService = reviewingService;
    }

    public void DoSomething()
    {
        var rs = _reviewingService();
        var reviews = new List<string> {"Review #1", "Review #2"};
        var reviewingService = rs.Create(reviews);

        reviewingService.Review();
    }
}

public interface IReviewingServiceFactory
{
    IReviewingService Create(IList<string> reviews);
}

public interface IReviewingService
{
    void Review();
}

public class ReviewingService : IReviewingService
{
    private readonly IList<string> _reviews;
    private readonly ISomeOtherDependency _someOtherDependency;

    public ReviewingService(IList<string> reviews, ISomeOtherDependency someOtherDependency)
    {
        _reviews = reviews;
        _someOtherDependency = someOtherDependency;
    }

    public void Review()
    {
        Console.WriteLine("Reviewing...");
    }
}

public interface ISomeOtherDependency
{
}

public class SomeOtherDependency : ISomeOtherDependency
{
}

With this example I would expect the console to output "Reviewing...". However, Windsor throws exceptions:

No component for supporting the service CastleWindsorTypedFactor.IReviewingServiceFactory was found

What is wrong with my Windsor installer?

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

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

发布评论

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

评论(1

妳是的陽光 2024-12-04 06:37:20

您注册了 Func 而不是 IReviewingServiceFactory...尝试替换

container.Register(Component.For<Func<IReviewingServiceFactory>>().AsFactory());

container.Register(Component.For<IReviewingServiceFactory>().AsFactory());

并相应地调整代码 - 然后它应该可以工作。

哦,还有一件事 - 您注册了 IReviewingService 而没有指定生活方式,默认为 SINGLETON。这很可能不是您想要的,因为您的reviews参数只会在创建时传递给实例,而这种情况只会发生第一次工厂被称为...!对工厂的其他调用将返回单例实例。

因此:将IReviewingService 的生活方式更改为瞬态,并在工厂接口上创建适当的发布方法签名(例如void Destroy(IReviewingService service))。

You registered Func<IReviewingServiceFactory> instead of IReviewingServiceFactory... try replacing

container.Register(Component.For<Func<IReviewingServiceFactory>>().AsFactory());

with

container.Register(Component.For<IReviewingServiceFactory>().AsFactory());

and adapt the code accordingly - then it should work.

Oh, and another thing - you registered your IReviewingService without specifying a lifestyle, which will default to SINGLETON. That is most likely not what you want, because then your reviews argument will only be passed to the instance when is gets created, which only happens the first time the factory is called...! Additional calls to the factory will return the singleton instance.

Therefore: Change the lifestyle of IReviewingService to transient, AND create an appropriate release method signature on the factory interface (e.g. void Destroy(IReviewingService service)).

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