【问题标题】:Castle Windsor Typed Factory Facility温莎城堡型工厂设施
【发布时间】:2012-01-04 12:46:22
【问题描述】:

_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();
    }
}

【问题讨论】:

  • FileWatcherSelector 长什么样子?
  • 我有点困惑......为了调用 GetFileWatcher("file", 20) 你的 IFileWatcherFactory 应该包括该方法/签名,并且通过 nameconvetion 将解析/创建一个 FileWatcher 的实例,它应该在它自己的构造函数中有这两个参数。我猜你对 FileWatcherSelector 做了一些错误的魔法。

标签: castle-windsor typed-factory-facility


【解决方案1】:

对于查看此内容的任何人,使用 TypedFactoryFacility 来指示工厂将在不同情况下创建哪种类型,您可以使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多