TodoApp – Part 3: Fix that data source
Let’s continue with the refactoring. What is the main window really doing? Well:
- Displaying the todo list
- Loading and saving stuff to the database
That’s one thing too many. We’ll implement a Repository. Imho, Fowler is wrong in his explanation. The most important goal of the Repository pattern is the following line: “Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers”. That’s what the pattern is all about, period. We do not want our domain to have any knowledge about the data source.
In later versions, the data source will be a REST web service publishing stuff as JSON. By implementing a Repository now, we get the bonus that we do not have to make that many modifications in later versions. All we need to do is to switch the implementation class from one to another (since we are using an interface and not a class in our code).
The new class is called ItemRepository. The application is a bit more readable now than the first version, right?