-
Notifications
You must be signed in to change notification settings - Fork 2
01 Introduction
This module introduces OrigoDB, explaining what it is, how it works, when and when not to use it. The exercises are fairly simple and introduce some of the main concepts which will be explored in depth later.
In this exercise you will play with an existing custom twitter model using scriptcs. If you don't have scriptcs, you can create a visual studio console application. The source for this model can be found here
-
Start a command prompt and navigate to the modules
Ex01folder -
Install the OrigoDB from nuget by typing
scriptcs -install origodb.core -
Start the scriptcs REPL by typing
scriptcsand enter the following statements:#r Twitter.Core.dll using OrigoDB.Core; using Twitter.Core; var t = new TwitterVerse(); t.AddUser("unclebob"); t.AddUser("robtheslob"); t.Follow("robtheslob", "unclebob") t.Tweet("unclebob", "If you don't do TDD ur doing it wrong", DateTime.Now) t.Unfollow("unclebob", "robtheslob")
You have created and manipulated a data model instance in memory but there is no engine controlling concurrency or providing persistence. You will fix this in the next step. Press CTRL+C to exit scriptcs, the current twitterverse will be lost forever.
-
Install origodb.core from nuget
-
Create a text file called
twitter.csxwith the following content:#r Twitter.Core.dll using OrigoDB.Core; using Twitter.Core; var db = Db.For<TwitterVerse>(); //transparent proxy with backing file storage in current directory db.AddUser("robtheslob"); db.AddUser("neo"); db.Follow("neo", "robtheslob"); db.Tweet("neo", "What is the Matrix?", DateTime.Now);
-
Start the script and enter interactive REPL mode using
scriptcs twitter.csx -REPL -
Exit with CTRL+C when you've finished playing around
- A snapshot and journal was created on disk
- If you execute
Db.For<TwitterVerse>()again, the in-memory model will be restored from disk - You used the transparent proxy feature
- Scriptcs is awesome
In this excercise you will create a custom data model using object oriented domain modeling.
-
Create an empty NET 4.0 Class Library Project using Visual Studio named
Todo.CoreinEx02\Starter -
Create an entity class called
Todorepresenting a single todo item.[Serializable] public class Todo { public DateTime? Due { get; private set; } public DateTime? Completed { get; private set; } public string Title { get; private set; } public readonly int Id; public Todo(int id, string title = "Untitled") { Id = id; Title = title; } }
-
Add a nuget reference to
OrigoDB.Core -
Create an anemic root data model class deriving from
OrigoDB.Core.Model[Serializable] public class TodoModel : Model { public SortedDictionary<int, Todo> Todos { get; private set; } int _nextId = 1; public int GetNextId() { return _nextId++; } public TodoModel() { Todos = new SortedDictionary<int, Todo>(); } }
-
Create the following commands:
[Serializable] public class AddTodoCommand : Command<TodoModel, int> { public readonly string Title; public AddTodoCommand(string title) { Title = title; } public override int Execute(TodoModel model) { int id = model.GetNextId(); model.Todos[id] = new Todo(id, Title); return id; } } [Serializable] public class SetCompletedCommand : Command<TodoModel> { public readonly int Id; public readonly DateTime Completed; public SetCompletedCommand(int id, DateTime completed) { Id = id; Completed = completed; } public override void Execute(TodoModel model) { if (!model.Todos.ContainsKey(Id)) Abort("No such todo"); model.Todos[Id].SetCompleted(Completed); } }
-
Add a SetCompleted method to the Todo class and make sure everything builds
Now it's time to test drive the model, let's use scriptcs again.
-
Create a text file named
todo.csxin theEx02directory with the following contents:#r Todo.Core/bin/debug/Todo.Core.dll using Todo.Core; using OrigoDB.Core; var engine = Engine.LoadOrCreate<TodoModel>(); var command = new AddTodoCommand("Learn Scala"); int id = engine.Execute(command); var todo = engine.Execute(db => db.Todos.Values.Where(t => t.Id == id).Single()); engine.Execute(new SetCompletedCommand(id, DateTime.Now)); var todo2 = engine.Execute(db => db.Todos[id]);
-
Start the script and remain in REPL mode.
-
Examine existing variables, play around, ask questions and discuss
- You have created a class library with an entity, a root data model and commands.
- You have created an engine and executed commands and ad-hoc queries
- Note the snapshot and journal
- Scriptcs is awesome