【发布时间】: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