0

I have created a VB .NET application that opens files in their default application - extracts information and returns it to a listview on a form.

All of the code is in my main form. The main form has in it

  • Imports Microsoft.Office.Core
  • Imports Microsoft.Office.Interop.Word
  • Imports Microsoft.Office.Interop.Excel

If in the future I want to modify my software to include another filetype not thought of in this release, am I better off for all of the filetypes I wish to open (including office) adding new classes for each filetype and including the 'Imports' in the individual classes?

So for example I would have:

  • OpenFileDWG.vb
  • Imports Autodesk.AutoCAD.Runtime

  • OpenFileDOC.vb

  • Imports Microsoft.Office.Interop.Word

etc. etc. Is this a standard approach? If I were to do this, could I use:

If exists LCase(My.Computer.FileSystem.GetFileInfo(Filepath).Extension) THEN strFileOpener = OpenFileDWG & Extension Private fileOpener As strFileOpener

Would this approach work, or would I still need to reference the .dll in the main application, making this approach unworthy?

If I were to use this approach, could I just give the .vb file as part of an update?

Any advice is much appreciated.

GoodJuJu
  • 1,296
  • 2
  • 16
  • 37
  • Seems to me like the [factory design pattern](https://msdn.microsoft.com/en-us/library/ee817667.aspx) is what you need. Separate the different file types to different classes, and have them all implement a basic interface (open file, read content, etc`). Then create an abstract factory class and a concrete factory for each class (meaning for each file type). to enable new file types add-ins you can use [MEF](https://msdn.microsoft.com/en-us/library/dd460648%28v=vs.110%29.aspx) to load the concrete classes. – Zohar Peled May 31 '15 at 10:35
  • 1
    'Best practice' questions are mostly off-topic for Stack Overflow as they attract opinionated answers and spam. – AStopher May 31 '15 at 11:09
  • Thankyou Zohar - that is exactly the kind of answer I was looking for. – GoodJuJu May 31 '15 at 11:13
  • Hi Cybermonkey. Sorry, I thought it was a reasonable question to ask, although I realise a best-practice / advice question was going to be subjective. Sometimes though, this can draw answers that open up other avenues to explore not previously thought of. I will bear it in mid in the future. – GoodJuJu May 31 '15 at 11:17

1 Answers1

0

Seems to me like a classing case to use factory design pattern

Basically, the factory design pattern provides loose coupling between the factory created classes and the class that uses them.

Begin by separating the different file types to different classes, and have them all inherit a basic abstract class (MustInherit in vb.net).
Then create an factory class to create create the concrete implementation of every file reader class. (meaning for each file type).

I'll try to illustrate with a simple example:

'' Defines an abstract class that all FileReaders should inherit
Public MustInherit Class FileReader
   Public MustOverride Function ReadFileContent(Path As String) As String
End Class

Now, all of the classes for reading files must inherit the 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

Then you can use a simple factory method (read here to learn about the difference between factory methods and abstract factories) to create your 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

To enable new file types add-ins you can use MEF to load the concrete classes.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Zohar - thank you again for taking the time to provide a detailed explanation. This is the approach I thought I needed, but was not aware of 'Factory Design Patterns'. Much appreciated. – GoodJuJu May 31 '15 at 11:46
  • Not a problem, but the way to show appreciation in SO is to upvote an answer or accept it (Only if it actually provides the solution to your problem!) – Zohar Peled May 31 '15 at 11:57
  • Thanks. I have accepted your answer as it is what I will use to implement the functionality I require. Unfortunately I don't have enough reputation (story of my life) to upvote your answer though :-( – GoodJuJu May 31 '15 at 12:02
  • Not a problem, to me the 10 reputation points wouldn't matter all that much. The reason you should accept an answer is so that other people will know that the problem is solved, thus saving time for people that might want to answer the question and also helping other people that might encounter a similar problem. – Zohar Peled May 31 '15 at 12:05