I’m trying to design a interface for iTextsharp (PDF creation library) that I’m using for my project. I don’t want any reference to iTextsharp in my project, just the interface.
Lets say, I have
interface IPdfTable { /* */ }
public class PdfTable : IPdfTable { /* */ }
interface IPdfCell { /* */ }
public class PdfCell : PdfCell { /* */ }
While I can easily build interfaces for each class individualy, I’m having difficulty on the implementation when these classes interact with each other. Somewhere in the code, I need tables to be able to accept an collection of cells.
The problem arrives when I have a have an collection of cells, and I need to add it to the table. Somehow I need to transform the IPdfCell into the original element that is accepted by the library (iTextSharp). I believe the quick and easy implementation was downcasting, but not a good design.
The only other solution I can think is using the interface to collection varies settings and create the orginal element (accepted by iTextsharp) on the fly when it is being passed around into other elements.
Is there a better implementation?
Typically, you would have a translation layer that translates your interface facade implementation into the types supported natively by iTextSharp.
AutoMapper can help for this if you require property to property mappings and can remove a lot of the translation legwork for you.