Skip to content

Instantly share code, notes, and snippets.

@atheken
Created July 22, 2010 00:21
Show Gist options
  • Save atheken/485375 to your computer and use it in GitHub Desktop.
Save atheken/485375 to your computer and use it in GitHub Desktop.
Multiple snippets of how to use NoRM to do common tasks, originally authored by: Anirudh Sanjeev (http://github.com/ninjagod)
//This is a model that will be used in each of the examples below.
public class Product
{
public Product()
{
// Default values are to be set here
Shipping = new Address();
}
public ObjectId _id{get;set;}
public double Price{get;set;}
public Address Shipping{get;set;}
public DbReference<Brand> TheBrand{get;set;}
public string Name{get;set;}
public override string ToString ()
{
return string.Format ("[Product: Price={0}, Shipping={1}, Name={2}]", Price, Shipping, Name);
}
}
public class Brand
{
public Brand()
{
Suppliers = new List<Address>();
}
public ObjectId _id{get;set;}
public string Name{get;set;}
public int CustomerRating{get;set;}
public List<Address> Suppliers{get;set;}
public override string ToString ()
{
return string.Format ("[Brand: Name={0}, CustomerRating={1}", Name, CustomerRating);
}
}
public class Address
{
public Address()
{
}
public ObjectId _id{get;set;}
public string StreetName{get;set;}
public override string ToString ()
{
return string.Format ("[Address: _id={0}]", _id);
}
}
// create four addresses
List<Address> places = new List<Address>();
for(int i = 0; i < 6; i++)
places.Add(new Address{StreetName=String.Format("#{0}, Street St", i)});
// create brands
Brand acme = new Brand{Name="Acme Inc.", CustomerRating = 8};
acme.Suppliers.Add(places[0]);
acme.Suppliers.Add(places[1]);
brandCollection.Save(acme);
//
Brand apple = new Brand{Name = "Apple, Inc.", CustomerRating = 7};
apple.Suppliers.Add( new Address{StreetName = "Infinite Loop"});
brandCollection.Save(apple);
// Create products
Product rocketBoots = new Product{Price = 100, Shipping = places[4], Name="Rocket Boots"};
rocketBoots.TheBrand = new DbReference<Brand>(acme._id);
prodCollection.Save(rocketBoots);
//
Product explosives = new Product{Price = 10, Shipping = places[2], Name = "Explosives"};
explosives.TheBrand = new DbReference<Brand>(acme._id);
//
Product iPod = new Product{Price=350, Shipping = places[5], Name = "iPod Mini"};
iPod.TheBrand = new DbReference<Brand>(apple._id);
prodCollection.Save(iPod);
using(var db = Mongo.Create("mongodb://localhost/test"))
{
var prodCollection = db.GetCollection<Product>();
var brandCollection = db.GetCollection<Brand>();
// your code here.
}
using(var db = Mongo.Create("mongodb://localhost/test"))
{
var prod = db.GetCollection<Product>();
var brand = db.GetCollection<Brand>();
// iterate over the products
foreach(var product in prod.AsQueryable().ToList())
{
// Retrieve a brand from a database reference
Brand theBrand = product.TheBrand.Fetch(() => db);
//
Console.WriteLine ("Available: {0} {1}", product.ToString(), theBrand.ToString());
}
}
@atheken
Copy link
Author

atheken commented Jul 22, 2010

Test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment