我有一个读取特定文件格式的应用程序。此外,该应用程序还支持读取替代文件格式的“插件”,并将它们转换为该工具本机支持的标准化格式。
我想定义一个类似这样的接口:
/// <summary>
/// Interface for file transformer. Implementers of this interface provide a means to
/// convert a given file into Foo Format.
/// </summary>
public interface IFileTransformerPlugin
{
/// <summary>Gets the name of the transformer to display to the user.</summary>
/// <value>The name of the transformer displayed to the user.</value>
string DisplayName
{
get;
}
/// <summary>Determines if this transformer can handle the given format.</summary>
/// <remarks>
/// This method should return very quickly; reading only a small portion of the
/// input stream. This method is intended to be a rough cut check -- returning true
/// does not necessarily mean that the file will parse successfully on full load.
/// </remarks>
/// <param name="fileContents">The file contents.</param>
/// <returns>
/// true if this transformer can handle the supplied format, false otherwise.
/// </returns>
bool CanHandleLogFormat(Stream fileContents);
/// <summary>Transforms a file into Foo Format.</summary>
/// <param name="inputFile">The input log stream.</param>
/// <param name="outputFile">The output log stream (where the output should be
/// written).</param>
/// <returns>A transformation result which includes error information.</returns>
LogTransformationResult Transform(Stream inputFile, Stream outputFile);
}
问题来自采用流的转换方法。从概念上讲,我希望插件主机而不是插件“拥有”这些流,并负责在这些流上调用 IDispose 或其他任何内容。例如,在测试场景下,调用者最好能够将 MemoryStream 作为输出传递,然后验证输出是否有效。
但是,作为插件作者,希望能够在框架中使用上层格式化结构;即TextReader/TextWriter;XmlTextReader/XmlTextWriter;等等。但是这些类拥有底层流的所有权并在底层流上调用 Dispose,而不管提供流的代码做什么。(至少,假设这些类本身被正确处理)
我该如何修改这个界面来解决这个问题?它甚至是一个可以解决的问题吗?