// Make instance of "reader"; constructor with InputStream is expected. public HGridReader makeReader(StreamReader instm) { if (m_gridReader == null) { throw new Exception("Format doesn't support reader: " + m_strMime); } try { // For each possible case make reader of the correct type based on the stream if (m_gridReader is HZincReader) { m_gridReader = new HZincReader(instm.BaseStream); } return(m_gridReader); } catch (Exception e) { throw new Exception("Cannot construct: " + m_gridReader.GetType() + "(InputStream)", e); } }
// Constructor public HGridFormat(string mime, HGridReader reader, HGridWriter writer) { if (mime.IndexOf(';') >= 0) { throw new ArgumentException("mime has semicolon " + mime, "mime"); } m_strMime = mime; m_gridReader = reader; m_gridWriter = writer; m_registry = new Dictionary <string, HGridFormat>(); m_syncLock = new object(); // The original implmentation had a storage of the generic type aginst the formats - but I see this // as a distinct issue because: // 1. if this is not storing a real instance with underlying streams attempts to use it should cause // issues unless makereader /writer is called with the instance - this makes the interface not well // designed to oo principals and design patterns - implementation should not hide obvious pitfalls in // calling // Solution has been to get the underlying stream and construct a real instance register(new HGridFormat("text/plain", new HZincReader(reader.BaseStream), new HZincWriter(writer.BaseStream))); register(new HGridFormat("text/zinc", new HZincReader(reader.BaseStream), new HZincWriter(writer.BaseStream))); //register(new HGridFormat("text/csv", null, HCsvWriter.class)); register(new HGridFormat("application/json", null, new HJsonWriter(writer.BaseStream))); }