What design pattern should I use?

Viewed 66

I have to create project to document conversion. Base on parameters and specific buissnes type I have to get document from database and convert it to preper typy. It can by a lot of conversion types, for example: binary document to xmlDocument to transformed xml by xsl or binary document to pdf. For each conversion I use external libraries. What design pattern should I use? Maybe, factory method foreach specific buissnes type and adapters inside it?

This is only simple sample:

public class Document
{
    public int Id { get; set; }
    public byte[] Content { get; set; }
    public string FileName { get; set; }
    public string Encoding { get; set; }
    public string CompressionType { get; set; }
    public string Format { get; set; }
}

   public  class DocumentContext
{ 
    public string TransformationParameters { get; set; }

    public string BaseParameters { get; set; }
}

  public interface IDocumentReceiveService<in Params, out Item>
{
    Item GetDocument(Params parameters);
}

public class DocumentReceiveService : 
    IDocumentReceiveService<DocumentContext, DocumentBin>
{
    public DocumentBin GetDocument(DocumentContext parameters)
    {
        throw new NotImplementedException();
    }
}

and DocumentFactory abstraction:

public interface IDocumentFactory<in Params, out Item>
{
    Item Create(Params parameters);
}

and its concrete implementations:

public class DocumentFactoryXml : IDocumentFactory<DocumentContext, 
    Document>
{
    private readonly IDocumentReceiveService<DocumentContext, DocumentBin> 
        _documentreceiver;

    private readonly DocumentBinToDocumentXmlAdapter _adapter;

    public DocumentFactoryXml(
        IDocumentReceiveService<DocumentContext, DocumentBin> documentreceiver, 
        DocumentBinToDocumentXmlAdapter adapter)
    {
        _documentreceiver = documentreceiver;
        _adapter = adapter;
    }
    public Document Create(DocumentContext parameters)
    {
        var documentBin = _documentreceiver.GetDocument(parameters);

        return _adapter.Adaptee(documentBin);
    }
}

and:

public class DocumentFactoryBin : IDocumentFactory<DocumentContext, 
    Document>
{
    private readonly IDocumentReceiveService<DocumentContext, DocumentBin> 
        _documentreceiver;

    public DocumentFactoryBin(
        IDocumentReceiveService<DocumentContext, DocumentBin> 
            documentreceiver)
    {
        _documentreceiver = documentreceiver;
    }
    public Document Create(DocumentContext parameters)
    {
        return _documentreceiver.GetDocument(parameters);
    }
}

and:

public class DocumentBin: Document
{
}    

public class DocumentXml : Document
{
}

public class DocumentBinToDocumentXmlAdapter
{
    public DocumentXml Adaptee(DocumentBin documentBin)
    {
        throw new NotImplementedException();
    }
}
1 Answers

yeah, it is possible to apply Factory method here. But it is incorrect way to apply pattern with goal that some pattern should be applied in project. Why? Because it would bring extra layer of abstraction and a little more complexity. If you avoid to add additional complexity, then your code will be simpler. Simple code is almost always the best choice.

Related