I have a collection of domain objects that I need to convert into another type for use by the .NET framework. What is the best practice for doing such a transformation?
Specifically, I have a type called ContentEntry and I need to convert it into a SyndicationItem for use in putting into a SyndicationFeed. The conversion itself is straight forward but I’m looking for a good pattern. Do I create a method on ContentEntry called CreateSyndicationItem() or perhaps a separate converter object? Perhaps an extension method?
This will be somewhat subjective, but I’d appreciate some ideas.
Edit Note: I’d like to note that I don’t control SyndicationItem. It is built into the .NET Framework. Also, I’d really like to convert several ContentEntry objects into SyndicationItems at one time.
As as cannot modify the SyndicationItem’s constructors, I’d suggest you use the factory pattern. Create a SyndicationItemFactory class that has the method CreateSyndicationItem(). This method returns a SyndicationItem object. In this case, you’ll only need one version of the method, that will take a ContentEntry object.
When you say you’d like to create multiple ContentEntry objects at once, I’m assuming you have say an array of ContentEntry objects. Another method (say CreateSyndicationItems()) might return an array of SyndicationItems, and take an array of ContentEntry objects as its parameter.