-
Notifications
You must be signed in to change notification settings - Fork 2
02 Modeling
Robert Friberg edited this page May 28, 2014
·
5 revisions
This module goes a bit deeper into modeling.
In this exercise you will extend the Todo.Core model from module 1 to support categories, and create commands and queries with emphasis on the topics presented in the slides.
- Open the Todo.Core solution in Visual studio located in the
Ex01
folder - Add a class named
Category
with aName
andHashSet<Todo>
property - Add a
HashSet<Category>
property to theTodo
class - Add a
Dictionary<string,Category>
to theTodoModel
class
-
SetCategoryCommand
- add a todo item to a category, create the category if necessary -
ClearCategoryCommand
- remove a todo item from a category -
SetCategoriesCommand
- set the category associations for a given todo
- Create a todo.csx file in the excercise folder with the following content:
#r Todo.Core\bin\debug\Todo.Core.dll using Todo.Core; using OrigoDB.Core; var engine = Engine.LoadOrCreate<TodoModel>(); var haskell = engine.Execute(new AddTodoCommand("Learn Haskell")); var cook = engine.Execute(new AddTodoCommand("Lamb Roast on Sunday")); var dishes = engine.Execute(new AddTodoCommand("Do the dishes")); engine.Execute(new SetCategoriesCommand{ TodoId = haskell, Categories = new[]{"work", "play"} }); engine.Execute(new SetCategoryCommand(cook,"play")); engine.Execute(new SetCategoryCommand(dishes, "work"));
- Launch the script and remain in the REPL.
- Have a look at a dump of the model by typing
engine.GetModel()
In the scriptcs repl, type lambdas to return the following:
- Category names
- Todo item names in alphabetical order
- Unfinished todo items
- Overdue todo items
See the Completed folder for example solutions in the file lambdas.csx
- In Visual Studio, create the following view class:
[Serializable] public class TodoView { public readonly int Id; public readonly string Title; public readonly string[] Categories; public TodoView(Todo todo) { Id = todo.Id; Title = todo.Title; Categories = todo.Categories.Select(c => c.Name).ToArray(); } }
- Create a query to retrieve todos by id:
[Serializable] public class GetTodoByIdQuery : Query<TodoModel, TodoView> { public readonly int Id; public GetTodoByIdQuery(int id) { Id = id; } public override TodoView Execute(TodoModel model) { return new TodoView(model.Todos[Id]); } }
- Create a query named PagedTodosQuery returning TodoView[] with Skip and Take properties (order by Id).
- Rewrite one or more of the lambdas from task 4 as queries, try LINQ syntax