I’m currently building a web app that will take some info from a database and replace some text in a Word document with that information. Then the document will be saved to a predefined location.
Problem now is that when development began we were talking about 5 documents, but now we are looking at 20+ and documents will probably change over time. My intial thought was to “hardcode” classes for each document. But now that seems like a a really stupid idea.
The signature for the method would probably be something like: void Generate(string template, string outputFileName);. How is this done in the best way? Let’s say I create an interface IGenerator which defines the Generate method and some method to describe the current “plugin”. Could I then simply put .cs-files (or dlls) with a single class implementing that interface in some directory and then let the application find all these and let the user choose one?
EDIT: Is there a good way to add/edit/delete these Document-classes without having to recompile the entire app or library with all classes? I would like each Document-class to stand for itself. I’ve read this article, which might be my best choice?
Thanks.
I would look at using a Factory Pattern to produce your document instances and if you need to deal with different document types then think about having an
Documentabstract class that exposes overrideable methods for getting data from and into a document.You then code to handle the
Documentclass and not specific child classes as the rest of your code doesn’t care what the document is, only that it is handling one.You would then need to implement a
Documentsub-class to cope with different document types (Word .doc, .docx; Excel, Text etc.) and generate an instance of the correct class by using aDocumentFactory to which you pass a variable to determine the creation of the correctDocumentsub-class.If you are to have multiple documents in existance at anyone time then you store instances of them in a
List<Document>.edit
Well here’s my thinking based on what you’ve stated so far.
You’re really only dealing with Office documents and so you can either use an Office SDK, in which case you’ll need to load up the appropriate library or if you stick to Open XML format documents, you can produce them yourself.
Have a look here for information on Office document formats.
In any case you’ll be dealing with say a Word document or an Excel document, but you may have multiple layouts of a Word document.
If you write your document class to accept template information of some sort then you can store this in XML either in your database or within a config file.
If storing within a config file then reference it as a data source from your
web.confige.g.That way you can make changes to the
appSettingsstored withindifferent.configwithout needing to restart your web pages.