Last active
August 29, 2015 13:56
-
-
Save joeriks/8933380 to your computer and use it in GitHub Desktop.
OrigoDb Gettingstarted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| using OrigoDB.Core; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| namespace PlayingWithOrigoDB | |
| { | |
| [TestClass] | |
| public class QuickStartGuide | |
| { | |
| [Serializable] | |
| public class Task | |
| { | |
| public string Title { get; set; } | |
| public string Description { get; set; } | |
| public DateTime DueBy { get; set; } | |
| } | |
| [Serializable] | |
| public class TaskModel : Model | |
| { | |
| public List<Task> Tasks { get; set; } | |
| public TaskModel() { Tasks = new List<Task>(); } | |
| } | |
| [Serializable] | |
| public class AddTaskCommand : Command<TaskModel> | |
| { | |
| public string Title { get; set; } | |
| public string Description { get; set; } | |
| public DateTime DueBy { get; set; } | |
| protected override void Execute(TaskModel model) | |
| { | |
| var task = new Task { Title = Title, Description = Description, DueBy = DueBy }; | |
| model.Tasks.Add(task); | |
| } | |
| } | |
| [Serializable] | |
| public class TasksDueBefore : Query<TaskModel, IEnumerable<Task>> | |
| { | |
| public DateTime DueDate { get; set; } | |
| protected override IEnumerable<Task> Execute(TaskModel model) | |
| { | |
| return model.Tasks.Where(t => DueDate > t.DueBy).ToArray(); | |
| } | |
| } | |
| private IEngine<TaskModel> engine; | |
| [TestInitialize] | |
| public void Hosting_The_Engine() | |
| { | |
| engine = Engine.For<TaskModel>(); | |
| } | |
| [TestMethod] | |
| public void Executing_Commands_And_Executing_Queries() | |
| { | |
| // Executing Commands | |
| var title = "Start using OrigoDB"; | |
| AddTaskCommand addTaskCommand = new AddTaskCommand | |
| { | |
| Title = title, | |
| Description = "No more relational modeling, sql or object relational mapping for me!", | |
| DueBy = DateTime.Now.AddDays(-1) | |
| }; | |
| //The engine will execute the command against the model and persist to the command journal | |
| engine.Execute(addTaskCommand); | |
| // Executing Queries | |
| // ad-hoc linq lambda | |
| var tasksDueLambda = (engine as ILocalEngine<TaskModel>).Execute<TaskModel, IEnumerable<Task>>(model => model.Tasks.Where(t => DateTime.Today > t.DueBy).OrderByDescending(t=>t.DueBy).ToArray()); | |
| Assert.AreEqual(title, tasksDueLambda.LastOrDefault().Title); | |
| // executing the strongly typed query | |
| var query = new TasksDueBefore { DueDate = DateTime.Today.AddDays(1) }; | |
| var tasksDueStronglyTypedQuery = engine.Execute(query); | |
| Assert.AreEqual(title, tasksDueStronglyTypedQuery.LastOrDefault().Title); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment