以交互方式使用默认应用程序打开文件的面向未来的软件的最佳方式
Best way to future proof software that opens files with default application interactively
我创建了一个 VB .NET
应用程序,它在默认应用程序中打开文件 - 提取信息并将其 returns 到表单的列表视图中。
所有代码都在我的主窗体中。主窗体里面有
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Excel
如果将来我想修改我的软件以包含此版本中未考虑的另一种文件类型,我是否最好为我希望打开的所有文件类型(包括 office)添加新的 类对于每个文件类型,并在 类?
中包含 'Imports'
例如我会:
- OpenFileDWG.vb
Imports Autodesk.AutoCAD.Runtime
OpenFileDOC.vb
Imports Microsoft.Office.Interop.Word
等等等
这是标准方法吗?如果我要这样做,我可以使用:
If exists LCase(My.Computer.FileSystem.GetFileInfo(Filepath).Extension) THEN
strFileOpener = OpenFileDWG & Extension
Private fileOpener As strFileOpener
这种方法行得通吗,还是我仍然需要在主应用程序中引用 .dll,从而使这种方法不值得?
如果我要使用这种方法,我可以只提供 .vb 文件作为更新的一部分吗?
非常感谢任何建议。
在我看来像 classing 案例使用 factory design pattern
基本上,工厂设计模式在工厂创建的 class 和使用它们的 class 之间提供了 loose coupling。
首先将不同的文件类型分成不同的 classes,并让它们都继承一个基本的抽象 class(vb.net 中的MustInherit
)。
然后创建一个工厂class来创建每个文件readerclass的具体实现。 (对每种文件类型的意思)。
我将尝试用一个简单的例子来说明:
'' Defines an abstract class that all FileReaders should inherit
Public MustInherit Class FileReader
Public MustOverride Function ReadFileContent(Path As String) As String
End Class
现在,所有用于读取文件的 classes 都必须继承 FileReader class:
Public Class WordFileReader
Inherits FileReader
Public Override Function ReadFileContent(Path As String) As String
'' TODO: Add code to read the content of the word document and return it as a string.
End Function
End Class
Public Class ExcelFileReader
Inherits FileReader
Public Override Function ReadFileContent(Path As String) As String
'' TODO: Add code to read the content of the excel file and return it as a string.
End Function
End Class
然后您可以使用简单的工厂方法 (read here to learn about the difference between factory methods and abstract factories) 来创建您的 classes:
Enum FileTypes
Word,
Excel
End Enum
Public Function GetFileReader(FileType As FileTypes) As FileReader
Dim FileReader as FileReader = Nothing
Select case FileType
Case FileTypes.Word:
FileReader = New WordFileReader()
Case FileTypes.Excel:
FileReader = New ExcelFileReader()
End Select
Return FileReader
End Function
要启用新的文件类型加载项,您可以使用 MEF 加载具体的 classes。
我创建了一个 VB .NET
应用程序,它在默认应用程序中打开文件 - 提取信息并将其 returns 到表单的列表视图中。
所有代码都在我的主窗体中。主窗体里面有
Imports Microsoft.Office.Core
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop.Excel
如果将来我想修改我的软件以包含此版本中未考虑的另一种文件类型,我是否最好为我希望打开的所有文件类型(包括 office)添加新的 类对于每个文件类型,并在 类?
中包含 'Imports'例如我会:
- OpenFileDWG.vb
Imports Autodesk.AutoCAD.Runtime
OpenFileDOC.vb
Imports Microsoft.Office.Interop.Word
等等等 这是标准方法吗?如果我要这样做,我可以使用:
If exists LCase(My.Computer.FileSystem.GetFileInfo(Filepath).Extension) THEN
strFileOpener = OpenFileDWG & Extension
Private fileOpener As strFileOpener
这种方法行得通吗,还是我仍然需要在主应用程序中引用 .dll,从而使这种方法不值得?
如果我要使用这种方法,我可以只提供 .vb 文件作为更新的一部分吗?
非常感谢任何建议。
在我看来像 classing 案例使用 factory design pattern
基本上,工厂设计模式在工厂创建的 class 和使用它们的 class 之间提供了 loose coupling。
首先将不同的文件类型分成不同的 classes,并让它们都继承一个基本的抽象 class(vb.net 中的MustInherit
)。
然后创建一个工厂class来创建每个文件readerclass的具体实现。 (对每种文件类型的意思)。
我将尝试用一个简单的例子来说明:
'' Defines an abstract class that all FileReaders should inherit
Public MustInherit Class FileReader
Public MustOverride Function ReadFileContent(Path As String) As String
End Class
现在,所有用于读取文件的 classes 都必须继承 FileReader class:
Public Class WordFileReader
Inherits FileReader
Public Override Function ReadFileContent(Path As String) As String
'' TODO: Add code to read the content of the word document and return it as a string.
End Function
End Class
Public Class ExcelFileReader
Inherits FileReader
Public Override Function ReadFileContent(Path As String) As String
'' TODO: Add code to read the content of the excel file and return it as a string.
End Function
End Class
然后您可以使用简单的工厂方法 (read here to learn about the difference between factory methods and abstract factories) 来创建您的 classes:
Enum FileTypes
Word,
Excel
End Enum
Public Function GetFileReader(FileType As FileTypes) As FileReader
Dim FileReader as FileReader = Nothing
Select case FileType
Case FileTypes.Word:
FileReader = New WordFileReader()
Case FileTypes.Excel:
FileReader = New ExcelFileReader()
End Select
Return FileReader
End Function
要启用新的文件类型加载项,您可以使用 MEF 加载具体的 classes。