So, consider the following code:public class OneConcreteTypeOfOutputFormat {    private Dictionary<Type, IEntityWriter> writers;    public Dispatcher()    {        this.writers = new Dictionary<string, IEntityWriter();        CreateWriters(writers);    }    private void CreateWriters(Dictionary<string, IEntityWriter> writers)    {        string nspace = typeof(OneConcreteWriter).Namespace;        Assembly assembly = Assembly.GetExecutingAssembly();        foreach (var writer in assembly.GetAllFromNamespace<IEntityWriter>(nspace))            writers.Add(writer.EntityType.FullName, writer);    }    private bool HasWriterForAllPropTypes(IEnumerable<Entity> data)    {        return data.All(ent => writers.ContainsKey(ent.GetType().FullName));    }    public void WriteEntities(IEnumerable<Entity> entities)    {         Require.That(() => HasWriterForAllPropTypes(outputData));         foreach(Entity entity in entities)             writers[entity.GetType().FullName].Write(entity);    }}Here, I am using the actual type of the Entity (which is a baseclass) to write it. I've heard that it's considered inelegant to use type-checking to perform operations, but I really don't want each Entity to write itself. This is a very easy and comfortable way to match something like this up. Do you find this clashing with the tenets of object-oriented programming? Sadly, each writer need to cast to it's specific type in the Write method.UPDATENote that the code above is completely updated - changes include:Added writer initialization code. It is delegated to a unit-tested extension method.Changed the name of the class to reflect that it is only one of different types of output formats - so it would require more than just creating one writer for each Entity.Added a little bit of the validation code.
Type checking to perform a variation of the strategy pattern
c#