Castle windsor 温莎城堡式工厂设施
调用\u fwf.GetFileWatcher时,始终会返回MailWatcher。如何返回带有类型化工厂设施的FileWatcher类?我尝试了下面的代码块,但这总是第一个组件 我还尝试了DefaultTypedFactoryComponentSelector,但没有得到结果Castle windsor 温莎城堡式工厂设施,castle-windsor,typed-factory-facility,Castle Windsor,Typed Factory Facility,调用\u fwf.GetFileWatcher时,始终会返回MailWatcher。如何返回带有类型化工厂设施的FileWatcher类?我尝试了下面的代码块,但这总是第一个组件 我还尝试了DefaultTypedFactoryComponentSelector,但没有得到结果 public interface IWatcher : IDisposable { void StartWatching(); } public class MailWatcher : IWatcher {
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();
}
}
公共接口IWatcher:IDisposable
{
无效起始匹配();
}
公共类邮件观察者:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“邮件观察者”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共类FileWatcher:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“文件监视程序”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共接口IFileWatcherFactory:IDisposable
{
IWatcher GetWatcher(字符串路径);
无效销毁(IWatcher fw);
}
公共类引导程序
{
私人静态WindsorContainer\u集装箱;
私人静态IFileWatcherFactory_fwf;
公共静态void Initialize()
{
_容器=新WindsorContainer();
_container.AddFacility();
_container.Register(Component.For().ImplementedBy().lifety.Transient);
_container.Register(Component.For().ImplementedBy().lifety.Transient);
_container.Register(Component.For().AsFactory(x=>x.SelectedWith(newfilewatcherselector()));
_fwf=_container.Resolve();
strong textvar fw=_fwf.GetFileWatcher(“文件”,20);
fw.开始匹配();
}
}
对于任何使用TypedFactoryFacility查看此问题的人,如果情况有所不同,您可以使用Get[Name]约定来指定工厂将创建的类型,其中您的接口将为每个实现类型提供一个创建方法
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();
}
}
公共类邮件观察者:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“邮件观察者”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共类FileWatcher:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“文件监视程序”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共接口IFileWatcherFactory:IDisposable
{
IWatcher GetMailWatcher(字符串路径);
IWatcher GetFileWatcher(字符串路径);
无效销毁(IWatcher fw);
}
公共类引导程序
{
私人静态WindsorContainer\u集装箱;
私人静态IFileWatcherFactory_fwf;
公共静态void Initialize()
{
_容器=新WindsorContainer();
_container.AddFacility();
_container.Register(Component.For().ImplementedBy().Named(“MailWatcher”).LifeStyle.Transient);
_container.Register(Component.For().ImplementedBy().Named(“FileWatcher”).lifety.Transient);
_container.Register(Component.For().AsFactory());
_fwf=_container.Resolve();
var fw=_fwf.GetFileWatcher(“文件”,20);
fw.开始匹配();
}
}
有关更多信息,请参阅:
对于任何使用TypedFactoryFacility查看此问题的人,如果情况有所不同,您可以使用Get[Name]约定来指定工厂将创建的类型,其中您的接口将为每个实现类型提供一个创建方法
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();
}
}
公共类邮件观察者:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“邮件观察者”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共类FileWatcher:IWatcher
{
公共无效开始匹配()
{
Console.WriteLine(“文件监视程序”);
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
公共接口IFileWatcherFactory:IDisposable
{
IWatcher GetMailWatcher(字符串路径);
IWatcher GetFileWatcher(字符串路径);
无效销毁(IWatcher fw);
}
公共类引导程序
{
私人静态WindsorContainer\u集装箱;
私人静态IFileWatcherFactory_fwf;
公共静态void Initialize()
{
_容器=新WindsorContainer();
_container.AddFacility();
_container.Register(Component.For().ImplementedBy().Named(“MailWatcher”).LifeStyle.Transient);
_container.Register(Component.For().ImplementedBy().Named(“FileWatcher”).lifety.Transient);
_container.Register(Component.For().AsFactory());
_fwf=_container.Resolve();
var fw=_fwf.GetFileWatcher(“文件”,20);
fw.开始匹配();
}
}
有关更多信息,请参阅:
FileWatcherSelector是什么样子的?我有点困惑。。。为了调用GetFileWatcher(“文件”,20),您的IFileWatcherFactory应该包括该方法/签名,并且通过名称转换将解析/创建FileWatcher的实例,该实例应该在其自己的构造函数中具有这两个参数。我猜你对FileWatcherSelector做了一些错误的魔法。FileWatcherSelector是什么样子的?我有点困惑。。。为了调用GetFileWatcher(“文件”,20),您的IFileWatcherFactory应该包括该方法/签名,并且通过名称转换将解析/创建FileWatcher的实例,该实例应该在其自己的构造函数中具有这两个参数。我猜你对FileWatcherSelector做了一些错误的魔法。