Posts tagged with: orm

Introducing the data mapper in Griffin.Framework

As you might know I’m running a .NET exception service called OneTrueError. When I moved from a NoSQL db to SQL Azure I had to be able to work with the database in some way. I’m not using OR/Ms any more. They might significantly reduce the bootstrapping, but in the long run they always tend to make you struggle as the application grow. To me, ORMs is a bit like ASP.NET WebForms, but for data. i.e. it tries to make something what it isn’t. I therefore wanted something that did not take away the control from me nor hide the underlying layer. I still want to work with my domain entities though.

Continue Reading


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.


Everyone know what it is, but no one have used it?

I must confess that I’ve never used the northwind database, despite that I’ve been developing using Visual Studio for more than 10 years.

I’m going to try Entity Framework 4 due to a request from a colleague of mine. The tutorial that I found is using Northwind database. I though that the DB was included in vstudio, but I didn’t find it anywhere. Therefore I downloaded it from Microsoft.

I really hope that the POCO implementation is well done. I’ve never used EF before since it couldn’t keep its hands of my POCOs.