Created
October 1, 2012 08:59
-
-
Save chrismcv/3810439 to your computer and use it in GitHub Desktop.
Live Projection that doesn't seem to work
This file contains 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Raven.Client.Document; | |
using Raven.Client.Indexes; | |
namespace ravenplay | |
{ | |
public class Activity | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public CategoryRef Category { get; set; } | |
} | |
public class ActivityVM | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string CategoryName { get; set; } | |
public string CategoryColour { get; set; } | |
public string CatId { get; set; } | |
} | |
public class CategoryRef | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class Category | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Colour { get; set; } | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var ds = new DocumentStore() { Url = "http://localhost:8080" }) | |
{ | |
// BuildData(ds); | |
ds.Initialize(); | |
IndexCreation.CreateIndexes(typeof(Activity_WithCategory).Assembly, ds); | |
using (var session = ds.OpenSession()) | |
{ | |
foreach (var vm in session.Query<ActivityVM, Activity_WithCategory>().ToArray()) | |
Console.WriteLine(vm.Name + " " + vm.CategoryColour); | |
} | |
} | |
Console.ReadLine(); | |
} | |
private static void BuildData(DocumentStore ds) | |
{ | |
var c = new Category() { Colour = "Green", Name = "Work-Based Learning" }; | |
var c2 = new Category() { Colour = "Red", Name = "Research" }; | |
using (var session = ds.OpenSession()) | |
{ | |
session.Store(c); | |
session.Store(c2); | |
session.SaveChanges(); | |
} | |
var a = new Activity() { Name = "Learning by Doing", Category = new CategoryRef() { Id = c.Id, Name = c.Name } }; | |
var a2 = new Activity() { Name = "Private Study", Category = new CategoryRef() { Id = c2.Id, Name = c2.Name } }; | |
using (var session = ds.OpenSession()) | |
{ | |
session.Store(a); | |
session.Store(a2); | |
session.SaveChanges(); | |
} | |
} | |
} | |
public class Activity_WithCategory : AbstractIndexCreationTask<Activity, ActivityVM> | |
{ | |
public Activity_WithCategory() | |
{ | |
Map = activities => from a in activities | |
select new { CatID = a.Category.Id }; | |
TransformResults = (db, activities) => from a2 in activities | |
let c = db.Load<Category>(a2.CatId) | |
select new { Name = a2.Name, CategoryColour = c.Colour }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment