Posts tagged with: repository

TodoApp – part 4: Welcome softcore!

The application as getting a bit more well designed when it comes to DRY KISS, SR. But before we continue, we have a small problem in the last iteration.

We didn’t find a good way to get typed data back instead of a data reader. We didn’t want to fill a List<> and return it, because it would take a lot of memory if we have millions of todo items. Instead we developed two classes turning a datareader into a typed IEnumrable.

The resulting code in the ItemRepository looks like this:

        /// <summary>
        ///   List all items.
        /// </summary>
        /// <returns>A collection of items, or a empty collection if no items was found.</returns>
        public IEnumerable<TodoItem> List()
        {
            var cmd = _db.CreateCommand("SELECT * FROM todo_items");
            return new ForwardOnlyCollection<TodoItem>(cmd.ExecuteReader(), OnFillItem);
        }

        /// <summary>
        ///   Callback from ForwardOnlyCollection
        /// </summary>
        /// <param name = "reader"></param>
        /// <param name = "item"></param>
        private static void OnFillItem(IDataReader reader, TodoItem item)
        {
            item.Id = (long) reader["id"];
            item.Title = (string) reader["title"];
            item.Description = (string) reader["description"];
        }

And the method in MainWindow was turned into:

        /// <summary>
        /// Loads items from the database and inserts them into the list box.
        /// </summary>
        private void PopulateListbox()
        {
            lbTodos.Items.Clear();
            foreach (var item in _repository.List())
                lbTodos.Items.Add(item);
        }

The MainWindow class is now totally unaware of the data source. Neat, huh?

The next day

After sleeping very nicely the developer (humm, the one making the Todo application) woke up with four letters circling around in his head: S K I S. He didn’t make any sense of them and went to work. After looking at the code he remembered what he did yesterday.

“We didn’t want to fill a List<> and return it, because it would take a lot of memory if we have millions of todo items”

omg! The letters was in the wrong order, they should be “KISS”. Keep it simple (and) stupid. How often do we have millions of todo items? That would be quite sad, wouldn’t it?

He refactored into a regular list instead:

        /// <summary>
        ///   List all items.
        /// </summary>
        /// <returns>A collection of items, or a empty collection if no items was found.</returns>
        public List<TodoItem> List()
        {
            var cmd = _db.CreateCommand("SELECT * FROM todo_items");
            var items = new List<TodoItem>();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                var item = new TodoItem();
                FillItem(reader, item);
                items.Add(item);
            }
            return items;
        }

A code duplication was also found. A TodoItem was filled with data from a IDataReader in both Get and OnFillData. OnFillData was renamed to Filldata and used in both places.

All code can be viewed at codeplex.