我想处理文件,因为它们保存在四个目录中,它们都以非常不同的方式处理。
我有一个在调度计时器上运行的通用处理器IFileQueueService,加载所有文件名并为每个文件调用自定义处理器IExecutionProcessor。这可能是一个简单的问题,但我的处理器具有比标准IExecutionProvider更多的属性,我不知道如何调用这些自定义处理器。
//Basic processor interface
public interface IExecutionProvider
{
void ProcessFile(string file);
}
//Simplified version of one of the custom processor interfaces
public interface IKyoExecutionProcessor
{
string DestinationPath { get; set; }
}
public class KyoExecutionProcessor : IExecutionProvider, IKyoExecutionProcessor
{
//This processor moves a file to the DestinationPath.
}
public interface IFileQueueService
{
string SourcePath { get; set; }
IExecutionProvider ExecutionProvider { get; set; }
void Start();
void Stop();
}
public class FileProcessor : IFileQueueService
{
...
public virtual void ProcessFileQueue()
{
IEnumerable<string> filesToProcess = GetFilesReadyToProcess();
foreach (string file in filesToProcess.ToList())
{
ExecutionProvider.ProcessFile(file);
}
}
}
public class KYOFileSysWatcher : ServiceBase
{
private IFileQueueService Processor { get; set; }
private IKyoExecutionProcessor KyoCustomProcessor { get; set; }
public KYOFileSysWatcher()
{
Processor = ObjectFactory.GetInstance<IFileQueueService>();
KyoCustomProcessor = ObjectFactory.GetInstance<IKyoExecutionProcessor>();
//This doesn't work, cannot convert source type to target type, but it implements the IExecutionProvider interface??? How do I do this?
Processor.ExecutionProvider = KyoCustomProcessor;
Processor.Start(); //Sets up dispatch timer
}
}
答案 0 :(得分:1)
您的IKyoExecutionProcessor
界面应来自IExecutionProvider
,如下所示:
public interface IKyoExecutionProcessor : IExecutionProvider
{
string DestinationPath { get; set; }
}
这确保了接口之间存在“is-a”关系,这也使您能够像这样实现KyoExecutionProcessor
类:
public class KyoExecutionProcessor : IKyoExecutionProcessor
{
public void ProcessFile(string file) { ... }
public string DestinationPath { get; set; }
}
这样,任何实现IKyoExecutionProcessor
的对象都可以分配给IExecutionProvider
类型的变量。
Processor.ExecutionProvider = KyoCustomProcessor; // You can now appoint IKyoExecutionProcessor instance to IExecutionProvider variable
答案 1 :(得分:1)
救援的泛型......
//Basic processor interface
public interface IExecutionProvider
{
void ProcessFile(string file);
}
//Simplified version of one of the custom processor interfaces
public interface IKyoExecutionProcessor
{
string DestinationPath { get; set; }
}
public interface IFileQueueService<TProvider>
where TProvider : IExecutionProvider
{
string SourcePath { get; set; }
TProvider ExecutionProvider { get; set; }
void Start();
void Stop();
}
public class FileProcessor<TProvider> : IFileQueueService<TProvider>
where TProvider : IExecutionProvider
{
string[] GetFilesReadyToProcess() { return new string[0]; }
public TProvider ExecutionProvider { get; set; }
public virtual void ProcessFileQueue()
{
IEnumerable<string> filesToProcess = GetFilesReadyToProcess();
foreach (string file in filesToProcess.ToList())
{
ExecutionProvider.ProcessFile(file);
}
}
#region IFileQueueService<TProvider> Members
public string SourcePath { get; set; }
public void Start() { }
public void Stop() { }
#endregion
}
public class KyoExecutionProcessor : IExecutionProvider, IKyoExecutionProcessor
{
//This processor moves a file to the DestinationPath.
public string DestinationPath { get; set; }
public void ProcessFile(string file) { }
}
public class ServiceBase<TProcessor>
where TProcessor : IExecutionProvider
{
protected FileProcessor<TProcessor> Processor { get; set; }
protected TProcessor CustomProcessor { get; set; }
}
public class KYOFileSysWatcher : ServiceBase<KyoExecutionProcessor>
{
public KYOFileSysWatcher()
{
Processor = ObjectFactory.GetInstance<FileProcessor<KyoExecutionProcessor>>();
CustomProcessor = ObjectFactory.GetInstance<KyoExecutionProcessor>();
//This compiles now!
Processor.ExecutionProvider = CustomProcessor;
Processor.Start(); //Sets up dispatch timer
}
}