jeudi 7 juin 2018

Decoupling pdf rendering into a reusable assembly / using different rendering startegies

We have two apps: desktop client & mvc backend. Both apps have printing functionality. And it's quite obvious that we're repeating ourselves with that. Let me explain this. The routine looks as follows:

  1. User enters his ID / sends his data to the mvc endpoint;
  2. We check the db if all necessary data is valid;
  3. We compose a viewmodel (mvc) / dto (desktop) object;
  4. As a requirement there are two type of documents to be printed;
  5. Then we make identical calls to a PDF-rendering API (we use PdfSharp) composing two documents.

I think it would be better if we had that pdf-composing logics in a separate assembly. Then it could be reusable. The problem with this is that documents use slightly different properties (data). As a solution we can use one shared dto with all necessary properties:

public IEnumerable<string> Render(DocumentDto document) {
    // ioc
    foreach(var strategy in this.strategies) {
        if(strategy.CanRender(document)) {
            yield strategy.Render(document);
        }
    }
}

We can also inject DbContext object into our startegies. And each strategy would request the desired properties on its own:

public class StrategyA {
    // I'll omit ctor here
    private DbContext db;
    public string Render() {
        // make db calls
        // render the document
    }
}

But I don't think this is a good solution either since it requires a db dependency.

Can we design it so that each strategy only uses its own set of properties?

Aucun commentaire:

Enregistrer un commentaire