We have a content delivery system that delivers many different content types to devices.
All the content is stored in a database with a contentID
& mediaTypeID
.
For this example lets assume the MediaType
can be one of these 2 but in reality theres many more of them.
Gif
MP3
Because the content is stored in different places based on mediatype and requires different headers to be sent, theres a big nasty piece of legacy code that essentially switches on each mediatype and sets the correct parameters. *I'd like to change this into a more generic implementation. So heres what I've got so far in my wireframe
public interface IContentTypeDownloader
{
MemoryStream GetContentStream();
Dictionary<string, string> GetHeaderInfo();
}
public class GifDownloader : IContentTypeDownloader
{
public MemoryStream GetContentStream(int contentID)
{
//Retrieve Specific Content gif
}
public Dictionary<string, string> GetHeaderInfo()
{
//Retrieve Header Info Specific To gifs
}
}
public class MP3Downloader : IContentTypeDownloader
{
public MemoryStream GetContentStream(int contentID)
{
//Retrieve Specific Content mp3
}
public Dictionary<string, string> GetHeaderInfo()
{
//Retrieve Header Info Specific To mp3s
}
}
Which all seems sensible... Until I get to the Manager Class.
public class ContentManager<T> where T : IContentTypeDownloader
{
public int ContentID { get; set; }
public MemoryStream GetContent()
{
IContentTypeDownloader ictd = default(T);
return ictd.GetContentStream(this.ContentID);
}
... etc
}
The problem is, I still need to initialise this type with the specific IContentTypeDownloader for that mediaTypeID.
And I'm back to square 1, with a situation like
if(mediaTypeID == 1)
ContentManager<GifDownloader> cm = new ContentManager<GifDownloader>();
else if (mediaTypeID == 2)
ContentManager<MP3Downloader> cm = new ContentManager<MP3Downloader>();
etc...
Anyone any idea on how to make this last decision generic based on the value of the mediaTyepID
that comes out of the Database