为什么使用不同的参数创建时,Castle Windsor类型的工厂会返回相同的实例 [英] Why does a Castle Windsor typed factory return the same instance when creating with different parameters

查看:80
本文介绍了为什么使用不同的参数创建时,Castle Windsor类型的工厂会返回相同的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用键入工厂设施

using System;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new WindsorContainer();

            container.AddFacility<TypedFactoryFacility>();

            container.Register(Component
                .For<IFactory>()
                .AsFactory()
                .LifestyleSingleton());

            container.Register(Component
                .For<IImplementation>()
                .ImplementedBy<Implementation>()
                .LifestylePerThread());

            var factory = container.Resolve<IFactory>();
            var implementation1 = factory.Create(1);
            var implementation2 = factory.Create(2);

            Console.WriteLine(implementation1 == implementation2);//Returns true!
            Console.Read();
        }
    }

    public interface IFactory
    {
        IImplementation Create(int dependency);
    }

    public interface IImplementation
    {}

    public class Implementation : IImplementation
    {
        private readonly int _dependency;

        public Implementation(int dependency)
        {
            _dependency = dependency;
        }
    }
}

我也尝试过使用参数作为覆盖 .Equals() .GetHashCode()的引用类型,而不是int没什么区别。

I've also tried it with the parameter as a reference type that overrides .Equals() and .GetHashCode() instead of an int but it makes no difference.

我意识到我可以使用 LifestyleTransient 解决此问题,但实际上我想收到

I realise I can use LifestyleTransient to solve this problem but I would actually want to receive the same instance if I pass in the same parameter.

推荐答案

您的期望不正确。

传递给工厂方法的参数是当且仅当所需服务中尚无组件时,才用于构造新组件的细节。在容器中可用。

The parameters passed to the factory method are the details that will be used to construct a new component if, and only if, there is not already a component of the required service available in the container.

您的第二个请求是从同一线程发出的,并且是针对同一服务的,因此Windsor正确地返回了已经构造的请求。

Your second request is made from the same thread and is for the same service so Windsor is correctly returning the one that was already constructed.

尽管吉拉德的建议是可以遵循的建议,但您可能仍然发现自己正在与容器打架并使事情变得比他们需要的更为复杂。

While Gilad's suggestion is a possible one to follow, you may still find yourself 'fighting' the container and making things more complex than they need to be.

我建议您采用Windsor中可用的机制,这些机制完全依赖于服务类型(接口)来使服务彼此区分。

I would suggest that you embrace the mechanisms available in Windsor, which rely completely on service types (interfaces) to differentiate services from one another.

问一下这两个不同实例的含义,并从接口方面反映出这些差异。例如也许您应该具有 IBigImplementation ISmallImplementation ?然后,可以在容器中注册和配置这些不同服务的实现。您将获得所有打算的合并/重用;

Ask yourself what it is about the two instances that is different and reflect those differences in terms of interfaces. e.g. maybe you should have an IBigImplementation and ISmallImplementation? The implementations of these different services can then be registered and configured in the container; you get all the pooling/reuse you intend; and consuming code remains blissfully unaware and decoupled from the implementation details.

[RANT:虽然工厂允许更大的灵活性,但通常我认为在工厂方法中使用参数是有代码气味。如您所知,它要求使用者对服务实现的生命周期进行假设。这也意味着控制实现细节的杠杆和开关分散在代码库中,而不是全部在容器注册代码中集中管理。]

[RANT: While factories allow for much greater flexibility, generally I regard the use of parameters to factory methods as a code smell. As you've discovered, it requires the consumer to make assumptions about the lifecycle of the service implementation. It also means that the levers and switches that control implementation details are scattered through the code base rather than all being managed centrally in container registration code.]

这篇关于为什么使用不同的参数创建时,Castle Windsor类型的工厂会返回相同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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