I am trying to create an aggregator that can loop through several implementations of an interface (IContentReader
) with a generic type that is constrained to another interface (IContent
). Each implementation of the IContentReader
will use a different implementation of the 'IContent` interface. This does not appear to work with DI. I am kind of at a loss as to how to do this.
My requirements will expand over time to include new games each with their own readers and content. How can I make this an DI friendly solution?
Here is the interface with the generic type:
public interface IContentReader<TContent> where TContent: IContent
{
IList<TContent> Content { get; }
void Read(string file);
}
Content interface:
public interface IContent
{
int Id { get; }
}
This is an example of the aggregator that will loop through the implementations:
public class ContentReaderAggregator : IContentReaderAggregator
{
private readonly IEnumerable<IContentReader<IContent>> _readers;
public ContentReaderAggregator(IEnumerable<IContentReader<IContent>> readers)
{
_readers = readers;
}
public void Read(string file)
{
foreach (var reader in _readers)
reader.Read(file);
}
}
An example implementation of a type of content and a reader:
public class Game1Content : IContent
{
public int Id { get; set; }
}
public class Game1ContentReader : IContentReader<Game1Content>
{
public IList<Game1Content> Content { get; }
public void Read(string file)
{
//Read implementation code
}
}
The compiler error I get when I try to create a collection of the IContentReader
implementations is CS1503
I understand this conversion error for the most part, so I'm more asking what I am doing wrong from a philosophical/design pattern point of view.
Aucun commentaire:
Enregistrer un commentaire