public
Created

RavenDb demo for Kiev ALT.NET

  • Download Gist
RavenDb.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
// Kiev ALT.NET: NoSQL
// Install RavenDb from http://ravendb.net/ , run server
// Reference RavenDB from NuGet
// Compile and execute this code
// Enjoy
 
using System;
using System.Linq;
using Raven.Client.Document;
using Raven.Client.Indexes;
 
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Connection..");
var db = new DocumentStore
{
Url = "http://localhost:8080/"
};
db.Initialize();
Console.WriteLine("First doc...");
using(var session = db.OpenSession())
{
session.Store(new SaleRecord());
session.SaveChanges();
}
Console.WriteLine("Update doc..");
using(var session = db.OpenSession())
{
var rec = session.Load<SaleRecord>("salerecords/1");
rec.Total = 12.0m;
rec.Date = DateTime.Now;
session.SaveChanges();
}
Console.WriteLine("Delete doc...");
using(var session = db.OpenSession())
{
var rec = session.Load<SaleRecord>("salerecords/1");
session.Delete(rec);
session.SaveChanges();
}
Console.WriteLine("Index, Map/Reduce demo...");
IndexCreation.CreateIndexes(typeof(Program).Assembly, db);
using(var session = db.OpenSession())
{
session.Store(new SaleRecord
{
Total = 10.0m,
Date = new DateTime(2012, 11, 12)
});
session.Store(new SaleRecord
{
Total = 13.0m,
Date = new DateTime(2012, 11, 13)
});
session.Store(new SaleRecord
{
Total = 12.0m,
Date = new DateTime(2012, 11, 13)
});
session.SaveChanges();
}
}
}
 
public class SaleRecord
{
public DateTime Date {get; set;}
public decimal Total {get; set;}
}
 
 
public class SaleRecords_Daily
: AbstractIndexCreationTask<SaleRecord>
{
// http://localhost:8080/indexes/salerecords/daily
public SaleRecords_Daily()
{
Map = docs => from doc in docs
select new
{
Date = doc.Date,
Total = doc.Total
};
Reduce = results => from result in results
group result by result.Date.Date into g
select new
{
Date = g.Key,
Total = g.Sum(x => x.Total)
};
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.