Creating a Generic Template Interface/Class and injecting into another object 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&lt;T&gt;
{
    /// &lt;summary&gt;
    /// The filepath of the base document for the template.
    /// &lt;/summary&gt;
    String BaseDocumentFilePath { get; }

    /// &lt;summary&gt;
    /// Retrieve the underlying template source.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;T&lt;/returns&gt;
    T GetContent();
}
public class WordTemplate : ITemplate&lt;XmlDocument&gt;
{
    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
{
    /// &lt;summary&gt;
    /// Generates a document from an instance of &lt;see cref="ITemplate"/&gt;. 
    /// &lt;/summary&gt;
    /// &lt;param name="template"&gt;The template for the document.&lt;/param&gt;
    /// &lt;param name="outputDocumentFilepath"&gt;The full file path of the result output.&lt;/param&gt;
    /// &lt;returns&gt;Returns filepath for generated document.&lt;/returns&gt;
    void Generate&lt;T&gt;(ITemplate&lt;T&gt; template, string outputDocumentFilepath);
}

public class WordDocumentGenerator : IDocumentGenerator
{
    public void Generate&lt;T&gt;(ITemplate&lt;T&gt; 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.
The template related class and interface are looking good to me. But the generator related class and interface are flawed.  

Assume one would call the Generate() method with an ITemplate&lt;string&gt; which would be totally legal. Sure it would work, but xmlDocumentBody would be null.  

You could if the passed in template isn't a ITemplate&lt;XmlDocument&gt; throw some exception, but a user of this class wouldn't expect for a generic class which is called correctly to get an exception.

So you should better make the IDocumentGenerator generic and not the Generate() method.  

public interface IDocumentGenerator&lt;T&gt;
{
    void Generate(ITemplate&lt;T&gt; template, string outputDocumentFilepath);
}


and now your WordDocumentGenerator class should implement IDocumentGenerator&lt;XmlDocument&gt; like  

public class WordDocumentGenerator : IDocumentGenerator&lt;XmlDocument&gt;
{
    public void Generate&lt;T&gt;(ITemplate&lt;XmlDocument&gt; template, string outputDocumentFilepath)
    {
        XmlDocument xmlDocumentBody = template.GetContent();

        // generate doc
    }
}


For sure if you have different types of templates you should add a DocumentGeneratorFactory which returns the desired DocumentGenerator for the task at hand.