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:
- User enters his ID / sends his data to the mvc endpoint;
- We check the db if all necessary data is valid;
- We compose a viewmodel (mvc) / dto (desktop) object;
- As a requirement there are two type of documents to be printed;
- 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