public void Create(ReadingCollectionDto dto) { SeekEnd(); // The collection must never grow past max size this.maxSize = dto.MaxReadings; // Write maximum size Writer.Write(this.maxSize); // Record the position of the current size field this.currentSizePosition = Writer.BaseStream.Position; // Write current size Writer.Write(this.currentSize); ReadingDto emptyReadingDto = new ReadingDto(); // Create an empty reading DTO emptyReadingDto.Empty = true; emptyReadingDto.Value = 0D; emptyReadingDto.Timestamp = DateTime.Now; // Create empty readings in the database for (int i = 0; i < this.maxSize; i++) { BinaryFileReadingDao emptyReadingDao = new BinaryFileReadingDao(archive); emptyReadingDao.Create(emptyReadingDto); this.unallocatedReadings.Add(emptyReadingDao); } }
public ReadingCollectionDto Read() { ReadingCollectionDto dto = new ReadingCollectionDto(); dto.Dao = this; SeekPosition(); // Maximum size dto.MaxReadings = this.maxSize = Reader.ReadInt32(); // Record the position of the current size field this.currentSizePosition = Reader.BaseStream.Position; // Current size this.currentSize = Reader.ReadInt32(); int counter = 0; // Read all of the non-empty readings for (; counter < this.currentSize; counter++) { BinaryFileReadingDao newReadingDao = new BinaryFileReadingDao(archive); ReadingDto newReadingDto = newReadingDao.Read(); newReadingDto.Dao = (IReadingDao)newReadingDao; this.readings.Add(newReadingDao); dto.Add(newReadingDto); } // Read all of the empty readings for (; counter < this.maxSize; counter++) { BinaryFileReadingDao newReadingDao = new BinaryFileReadingDao(archive); ReadingDto newReadingDto = newReadingDao.Read(); Debug.Assert(newReadingDto.Empty); newReadingDto.Dao = (IReadingDao)newReadingDao; this.unallocatedReadings.Add(newReadingDao); } return(dto); }