小编use*_*422的帖子

为什么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) …
Run Code Online (Sandbox Code Playgroud)

c# castle-windsor

6
推荐指数
1
解决办法
1067
查看次数

标签 统计

c# ×1

castle-windsor ×1