0

下面的代码运行良好,并成功为DummyComponnent类创建了一个实例。但是当我将工厂方法名称更改CreatDummyComponnent()GetDummyComponnent()或除Creat作为方法名称的开头之外的任何其他名称时,就会出现问题,比如AnyThingComponent抛出异常。工厂方法是否有任何指定的命名规则?

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

namespace AsFactoryImplementation
{
    public interface IDummyComponnentFactory
    {
        IDummyComponnent CreatDummyComponnent();
//        void Relese(IDummyComponnent factory);
    }

    public interface IDummyComponnent
    {
        void Show();
    }

    public class DummyComponnent:IDummyComponnent
    {
        public DummyComponnent()
        {
            Console.WriteLine("we are working here");
        }
        public void Show()
        {
            Console.WriteLine("just testing this for better performance");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var container = new WindsorContainer();
            container.AddFacility<TypedFactoryFacility>();

            container.Register(Component.For<IDummyComponnent>().ImplementedBy<DummyComponnent>().Named("FirstConnection"),
                Component.For<IDummyComponnentFactory>().AsFactory());

            var val = container.Resolve<IDummyComponnentFactory>();
            var iDummy = val.CreatDummyComponnent();
            iDummy.Show();
                Console.WriteLine("OK its done ");
                Console.ReadLine();
        }
    }
}
4

1 回答 1

0

您应该能够使用任何东西来启动 Factory 上的方法名称,除了以 Get 开头。

如果您从 Get 开始,它将尝试按名称而不是按接口解析组件。因此,在您的示例中起作用的是:

var iDummy = val.GetFirstConnection();

祝你好运,

马尔维恩。

于 2013-02-18T15:26:10.313 回答