16. using Raven.Client.Document;
namespace Northwind
{
class Program
{
static void Main()
{
var documentStore = new DocumentStore
{
Url = "http://localhost:8080",
DefaultDatabase = "Northwind"
};
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
var p = session.Load<dynamic>("products/1");
System.Console.WriteLine(p.Name);
}
}
}
}
17. public class Product
{
public string Name { get; set; }
public string Supplier { get; set; }
public string Category { get; set; }
public string QuantityPerUnit { get; set; }
public float PricePerUnit { get; set; }
public int UnitsInStock { get; set; }
public int UnitsOnOrder { get; set; }
public bool Discontinued { get; set; }
public int ReorderLevel { get; set; }
}
18. using Raven.Client.Document;
namespace Northwind
{
class Program
{
static void Main()
{
var documentStore = new DocumentStore
{
Url = "http://localhost:8080",
DefaultDatabase = "Northwind"
};
documentStore.Initialize();
using (var session = documentStore.OpenSession())
{
var p = session.Load<Product>("products/1");
System.Console.WriteLine(p.Name);
}
}
}
}
19. public static class DocumentStoreHolder
{
private static readonly Lazy<IDocumentStore> LazyStore =
new Lazy<IDocumentStore>(() =>
{
var store = new DocumentStore
{
Url = "http://localhost:8080",
DefaultDatabase = "Northwind"
};
return store.Initialize();
});
public static IDocumentStore Store =>
LazyStore.Value;
}
20. string categoryId;
using (var session = DocumentStoreHolder.Store.OpenSession())
{
var newCategory = new Category
{
Name = "My New Category",
Description = "Description of the new category"
};
session.Store(newCategory);
categoryId = newCategory.Id;
session.SaveChanges();
}
21. using (var session = DocumentStoreHolder.Store.OpenSession())
{
var storedCategory = session
.Load<Category>(categoryId);
storedCategory.Name = "abcd";
session.SaveChanges();
}
22. using (var session = DocumentStoreHolder.Store.OpenSession())
{
session.Delete(categoryId);
session.SaveChanges();
}
27. private static void QueryCompanyOrders(int companyId)
{
using (var session = DocumentStoreHolder.Store.OpenSession())
{
var orders = (
from order in session.Query<Order>()
.Include(o => o.Company)
where order.Company == $"companies/{companyId}"
select order
).ToList();
var company = session.Load<Company>(companyId);
if (company == null)
{
WriteLine("Company not found.");
return;
}
WriteLine($"Orders for {company.Name}");
foreach (var order in orders)
{
WriteLine($" {order.Id} - {order.OrderedAt}");
}
}
}
28. var orders = (
from order in session.Query<Order>()
where order.Company == "companies/1"
orderby order.OrderedAt
select order
).ToList();
29. var results = new List<Order>();
foreach (var o in GetDocumentsFor("Orders"))
{
if (o.Company == "companies/1")
results.Add(o);
}
var orderedResults = results.Sort((a, b) => a.OrderedAt.CompareTo(b.OrderedAt));
30.
31. public class Employees_ByFirstAndLastName : AbstractIndexCreationTask<Employee>
{
public Employees_ByFirstAndLastName()
{
Map = (employees) =>
from employee in employees
select new
{
FirstName = employee.FirstName,
LastName = employee.LastName
};
}
}
32. public class People_Search : AbstractMultiMapIndexCreationTask<People_Search.Result>
{
public class Result
{
public string SourceId { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
public People_Search()
{
AddMap<Company>(companies =>
from company in companies
select new Result
{
SourceId = company.Id,
Name = company.Contact.Name,
Type = "Company's contact"
}
);
...
33. AddMap<Supplier>(suppliers =>
from supplier in suppliers
select new Result
{
SourceId = supplier.Id,
Name = supplier.Contact.Name,
Type = "Supplier's contact"
}
);
AddMap<Employee>(employees =>
from employee in employees
select new Result
{
SourceId = employee.Id,
Name = $"{employee.FirstName} {employee.LastName}",
Type = "Employee"
}
);
...
36. static void Main(string[] args)
{
Console.Title = "Multi-map sample";
using (var session = DocumentStoreHolder.Store.OpenSession())
{
while (true)
{
Console.Write("nSearch terms: ");
var searchTerms = Console.ReadLine();
foreach (var result in Search(session, searchTerms))
{
Console.WriteLine($"{result.SourceId}t{result.Type}t{result.Name}");
}
}
}
}
37.
38. public class Products_ByCategory : AbstractIndexCreationTask<Product, Products_ByCategory.Result>
{
public class Result
{
public string Category { get; set; }
public int Count { get; set; }
}
public Products_ByCategory()
{
Map = products =>
from product in products
select new
{
Category = product.Category,
Count = 1
};
Reduce = results =>
from result in results
group result by result.Category into g
select new
{
Category = g.Key,
Count = g.Sum(x => x.Count)
};
}
}
39. public class Employees_SalesPerMonth :
AbstractIndexCreationTask<Order, Employees_SalesPerMonth.Result>
{
public class Result
{
public string Employee { get; set; }
public string Month { get; set; }
public int TotalSales { get; set; }
}
public Employees_SalesPerMonth()
{
}
}
40. Map = orders =>
from order in orders
select new
{
order.Employee,
Month = order.OrderedAt.ToString("yyyy-MM"),
TotalSales = 1
};
41. Reduce = results =>
from result in results
group result by new
{
result.Employee,
result.Month
}
into g
select new
{
g.Key.Employee,
g.Key.Month,
TotalSales = g.Sum(x => x.TotalSales)
};
42. using (var session = DocumentStoreHolder.Store.OpenSession())
{
var query = session
.Query<Employees_SalesPerMonth.Result, Employees_SalesPerMonth>()
.Include(x => x.Employee);
var results = (
from result in query
where result.Month == "1998-03"
orderby result.TotalSales descending
select result
).ToList();
foreach (var result in results)
{
var employee = session.Load<Employee>(result.Employee);
Console.WriteLine($"{employee.FirstName} {employee.LastName} made
{result.TotalSales} sales.");
}
}
43. public class Products_ByCategory : AbstractIndexCreationTask<Product, Products_ByCategory.Result>
{
public class Result
{
public string Category { get; set; }
public int Count { get; set; }
}
public Products_ByCategory()
{
Map = products =>
from product in products
let categoryName = LoadDocument<Category>(product.Category).Name
select new
{
Category = categoryName,
Count = 1
};
Reduce = results =>
from result in results
group result by result.Category into g
select new
{
Category = g.Key,
Count = g.Sum(x => x.Count)
};
}
}
44. public class Products_ProductAndSupplierName : AbstractTransformerCreationTask<Product>
{
public class Result
{
public string ProductName { get; set; }
public string SupplierName { get; set; }
}
public Products_ProductAndSupplierName()
{
TransformResults = products =>
from product in products
let category = LoadDocument<Supplier>(product.Supplier)
select new
{
ProductName = product.Name,
SupplierName = category.Name
};
}
}