Skip to content

Instantly share code, notes, and snippets.

View JudahGabriel's full-sized avatar

Judah Gabriel Himango JudahGabriel

View GitHub Profile
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
static double items[1000000];
int i = 0;
for (i = 0; i < 1000000; i++)
{
/// <summary>
/// View model for a Recipe. Contains everything we want to display on the Recipe details UI page.
/// </summary>
public class RecipeViewModel
{
public string RecipeId { get; set; }
public string Name { get; set; }
public string PictureUrl { get; set; }
public List<Ingredient> Ingredients { get; set; }
public List<string> Categories { get; set; }
// Theoretical LINQ code to perform a JOIN against a relational database.
public RecipeViewModel GetRecipeDetails(string recipeId)
{
var recipeViewModel = from recipe in dbContext.Recipes
where recipe.Id == 42
join chef in dbContext.Chefs on chef.Id equals recipe.ChefId
let ingredients = dbContext.Ingredients.Where(i => i.RecipeId == recipe.Id)
let comments = dbContext.Comments.Where(c => c.RecipeId == recipe.Id)
let categories = dbContext.Categories.Where(c => c.RecipeId == recipeId)
select new RecipeViewModel
// Query for the honey glazed salmon recipe
// and .Include the related Chef and Comment objects.
var recipe = ravenSession.Query<Recipe>()
.Include(r => r.ChefId)
.Include(r => r.CommentIds)
.Where(r => r.Name == "Sweet honey glazed salmon")
.First();
// No extra DB call here; it's already loaded into memory via the .Include calls.
var chef = ravenSession.Load<Chef>(recipe.ChefId);
/// <summary>
/// RavenDB Transformer that turns a Recipe into a RecipeViewModel.
/// </summary>
public class RecipeViewModelTransformer : AbstractTransformerCreationTask<Recipe>
{
public RecipeViewModelTransformer()
{
TransformResults = allRecipes => from recipe in allRecipes
let chef = LoadDocument<Chef>(recipe.ChefId)
let comments = LoadDocument<Comment>(recipe.CommentIds)
// Query for the honey glazed salmon recipe
// and transform it into a RecipeViewModel.
var recipeViewModel = ravenSession.Query<Recipe>()
.TransformWith<RecipeViewModelTransformer, RecipeViewModel>()
.Where(r => r.Name == "Sweet honey glazed salmon")
.First();
// Query for all recipes and transform them into RecipeViewModels.
var allRecipeViewModels = ravenSession.Query<Recipe>()
.TransformWith<RecipeViewModelTransformer, RecipeViewModel>()
.ToList();
var recipeViewModel = new RecipeViewModel
{
Name = "Sweet honey glazed salmon",
RecipeId = "recipes/1",
PictureUrl = "http://tastyrecipesyum.com/recipes/1/profile.jpg",
Categories = new List<string> { "salmon", "fish", "seafood", "healthy" },
ChefName = "Swei D. Sheff",
ChefEmail = "borkbork@bork.com",
Comments = new List<Comment> { new Comment { Name = "Dee Liteful", Content = "I really enjoyed this dish!" } },
Ingredients = new List<Ingredient> { new Ingredient { Name = "salmon fillet", Amount = "5 ounce" } }
// Listen for changes to Recipes.
// When that happens, update the corresponding RecipeViewModel.
ravenStore
.Changes()
.ForDocumentsOfType<Recipe>()
.Subscribe(docChange => this.UpdateViewModelFromRecipe(docChange.Id));
/// <summary>
/// RavenDB index that is run automatically whenever a Recipe changes. For every recipe, the index outputs a RecipeViewModel.
/// </summary>
public class RecipeViewModelIndex : AbstractIndexCreationTask<Recipe>
{
public RecipeViewModelIndex()
{
Map = allRecipes => from recipe in allRecipes
let chef = LoadDocument<Chef>(recipe.ChefId)
let comments = LoadDocument<Comment>(recipe.CommentIds)