I would like to create a generic template class for my document processor. Assuming the template has a source file path and a "content", here's what I came up with:
Template
public interface ITemplate<T>
{
/// <summary>
/// The filepath of the base document for the template.
/// </summary>
String BaseDocumentFilePath { get; }
/// <summary>
/// Retrieve the underlying template source.
/// </summary>
/// <returns>T</returns>
T GetContent();
}
public class WordTemplate : ITemplate<XmlDocument>
{
private string _baseDocumentFilePath;
private XmlDocument _content;
public WordTemplate(XmlDocument content, string baseDocumentFilepath)
{
_content = content;
_baseDocumentFilePath = baseDocumentFilepath;
}
public string BaseDocumentFilePath { get { return _baseDocumentFilePath;}}
public XmlDocument GetContent()
{
return _content;
}
}
Ex Doc Generator
public interface IDocumentGenerator
{
/// <summary>
/// Generates a document from an instance of <see cref="ITemplate"/>.
/// </summary>
/// <param name="template">The template for the document.</param>
/// <param name="outputDocumentFilepath">The full file path of the result output.</param>
/// <returns>Returns filepath for generated document.</returns>
void Generate<T>(ITemplate<T> template, string outputDocumentFilepath);
}
public class WordDocumentGenerator : IDocumentGenerator
{
public void Generate<T>(ITemplate<T> template, string outputDocumentFilepath)
{
XmlDocument xmlDocumentBody = template.GetContent() as XmlDocument;
// generate doc
}
}
The template would be created elsewhere and injected into the document generator. My question is, is this a good design? I am casting the generic T
into XmlDocument (which is what I need for that specific implementation) and I don't think that's the right way to use generics. I've always been a little lost when it comes to generics.
template.GetContent() as XmlDocument
unlessT
isXmlDocument
. You need some sort ofFunc<T, XmlDocument>
to make this work. Any why are you working with the oldXmlDocument
and not the newerXDocument
? \$\endgroup\$