城堡温莎型工厂设施

时间:2012-01-04 12:46:23

标签: castle-windsor typed-factory-facility

调用 _fwf.GetFileWatcher 时,MailWatcher始终返回。如何返回带有类型工厂设施的FileWatcher类?我尝试了如下代码块,但这始终是第一个组件。

我也试过DefaultTypedFactoryComponentSelector,但我无法得到结果。

 public interface IWatcher : IDisposable
{
    void StartWatching();
}

public class MailWatcher : IWatcher
{

    public void StartWatching()
    {
        Console.WriteLine("Mail watcher");
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

 public class FileWatcher : IWatcher
{
    public void StartWatching()
    {
        Console.WriteLine("File watcher");
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

public interface IFileWatcherFactory : IDisposable
{
    IWatcher GetWatcher(string path);
    void Destroy(IWatcher fw);
}

 public class Bootstrapper
{
    private static WindsorContainer _container;
    private static IFileWatcherFactory _fwf;

    public static void Initialize()
    {
        _container = new WindsorContainer();
        _container.AddFacility<TypedFactoryFacility>();

        _container.Register(Component.For<IWatcher>().ImplementedBy<MailWatcher>().LifeStyle.Transient);
        _container.Register(Component.For<IWatcher>().ImplementedBy<FileWatcher>().LifeStyle.Transient);

        _container.Register(Component.For<IFileWatcherFactory>().AsFactory(x => x.SelectedWith(new FileWatcherSelector())));

        _fwf = _container.Resolve<IFileWatcherFactory>();
        strong textvar fw = _fwf.GetFileWatcher("file", 20); 
        fw.StartWatching();
    }
}

1 个答案:

答案 0 :(得分:1)

对于任何看这个的人来说,使用TypedFactoryFacility来指示Factory如果变化会创建哪种类型,你可以使用Get [Name]约定,其中你的接口将为每种实现类型都有一个create方法。

public class MailWatcher : IWatcher
{

    public void StartWatching()
    {
        Console.WriteLine("Mail watcher");
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

public class FileWatcher : IWatcher
{
    public void StartWatching()
    {
        Console.WriteLine("File watcher");
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

public interface IFileWatcherFactory : IDisposable
{
    IWatcher GetMailWatcher(string path);
    IWatcher GetFileWatcher(string path);
    void Destroy(IWatcher fw);
}

public class Bootstrapper
{
    private static WindsorContainer _container;
    private static IFileWatcherFactory _fwf;

    public static void Initialize()
    {
        _container = new WindsorContainer();
        _container.AddFacility<TypedFactoryFacility>();

        _container.Register(Component.For<IWatcher>().ImplementedBy<MailWatcher>().Named("MailWatcher").LifeStyle.Transient);
        _container.Register(Component.For<IWatcher>().ImplementedBy<FileWatcher>().Named("FileWatcher").LifeStyle.Transient);

        _container.Register(Component.For<IFileWatcherFactory>().AsFactory());

        _fwf = _container.Resolve<IFileWatcherFactory>();
        var fw = _fwf.GetFileWatcher("file", 20); 
        fw.StartWatching();
    }
}

有关详细信息,请参阅:

http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx#Get_methods_lookup_components_by_name_4