【转载】NDatabase 5 Minutes Tutorial
NDatabase 5 Minutes Tutorial
Here are 10 steps examples that demonstrate how to store, query and update objects with NDatabase.
Query steps are doubled for SODA and LINQ to NDatabase queries.
Step 1: Create and store a Sport instance
var sport = new Sport("volley-ball"); using (var odb = OdbFactory.Open(TutorialDb5MinName)) odb.Store(sport);
Step2: Create and store Game instance
// Create instance var volleyball = new Sport("volley-ball"); // Create 4 players var player1 = new Player("julia", DateTime.Now, volleyball); var player2 = new Player("magdalena", DateTime.Now, volleyball); var player3 = new Player("jacek", DateTime.Now, volleyball); var player4 = new Player("michal", DateTime.Now, volleyball); // Create two teams var team1 = new Team("Krakow"); var team2 = new Team("Skawina"); // Set players for team1 team1.AddPlayer(player1); team1.AddPlayer(player2); // Set players for team2 team2.AddPlayer(player3); team2.AddPlayer(player4); // Then create a volley ball game for the two teams var game = new Game(DateTime.Now, volleyball, team1, team2); using (var odb = OdbFactory.Open(TutorialDb5MinName)) odb.Store(game);
Step 3: Example of a simple SODA query
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); query.Descend("Name").Constrain("julia").Equal(); var players = query.Execute<Player>(); Console.WriteLine("\nStep 3 (Soda): Players with name julia"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(1)); }
Step 3: Example of a simple LINQ to NDatabase query
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where player.Name.Equals("julia") select player; Console.WriteLine("\nStep 3 (Linq): Players with name julia"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(1)); }
Step 4: Example of SODA query navigating between object relations
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var agassi = new Player("André Agassi", DateTime.Now, new Sport("Tennis")); odb.Store(agassi); var query = odb.Query<Player>(); query.Descend("FavoriteSport").Descend("_name") .Constrain("volley-ball").Equal(); var players = query.Execute<Player>(); Console.WriteLine("\nStep 4 (Soda): Players of Voller-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(4)); }
Step 4: Example of LINQ to NDatabase query navigating between object relations
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where player.FavoriteSport.Name == "volley-ball" select player; Console.WriteLine("\nStep 4 (Linq): Players of Voller-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(4)); }
Step 5: Example of SODA query using object references
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { // retrieve the volley ball sport object var query = odb.Query<Sport>(); query.Descend("_name").Constrain("volley-ball").Equal(); var volleyBall = query.Execute<Sport>().GetFirst(); Assert.That(volleyBall.Name, Is.EqualTo("volley-ball")); // Now build a query to get all players that play volley ball, using // the volley ball object query = odb.Query<Player>(); query.Descend("FavoriteSport").Constrain(volleyBall).Identity(); var players = query.Execute<Player>(); Console.WriteLine("\nStep 5 (Soda): Players of Voller-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(4)); }
Step 5: Example of LINQ to NDatabase query using object references
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { // retrieve the volley ball sport object var volleyBall = (from sport in odb.AsQueryable<Sport>() where sport.Name == "volley-ball" select sport).First(); Assert.That(volleyBall.Name, Is.EqualTo("volley-ball")); // Now build a query to get all players that play volley ball, using // the volley ball object var players = from player in odb.AsQueryable<Player>() where player.FavoriteSport.Equals(volleyBall) select player; Console.WriteLine("\nStep 5 (Linq): Players of Voller-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(4)); }
Step 6: Example of SODA query using the OR operator
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); (query.Descend("FavoriteSport._name").Constrain("volley-ball").Equal()) .Or(query.Descend("FavoriteSport._name").Constrain("%nnis").Like()); var players = query.Execute<Player>(); Console.WriteLine("\nStep 6 (Soda): Volley-ball and Tennis Players"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(5)); }
Step 6: Example of LINQ to NDatabase query using the OR operator
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where player.FavoriteSport.Name.Equals("volley-ball") || player.FavoriteSport.Name.EndsWith("nnis") select player; Console.WriteLine("\nStep 6 (Linq): Volley-ball and Tennis Players"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(5)); }
Step 7: Example of SODA query using NOT and like
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); query.Descend("FavoriteSport._name").Constrain("volley-ball") .Equal().Not(); var players = query.Execute<Player>(); Console.WriteLine("\nStep 7 (Soda): Players that don't play Volley-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(1)); }
Step 7: Example of LINQ to NDatabase query using NOT and like
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where !player.FavoriteSport.Name.Equals("volley-ball") select player; Console.WriteLine("\nStep 7 (Linq): Players that don't play Volley-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(1)); }
Step 8: Example of SODA query using StartsWith
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); query.Descend("FavoriteSport._name").Constrain("VOLLEY").StartsWith(false); var players = query.Execute<Player>(); Console.WriteLine("\nStep 8 (Soda): Players that play Volley-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(4)); }
Step 8: Example of LINQ to NDatabase query using StartsWith
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where player.FavoriteSport.Name .StartsWith("VOLLEY", true, CultureInfo.InvariantCulture) select player; Console.WriteLine("\nStep 8 (Linq): Players that play Volley-ball"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(4)); }
Step 9: SODA query with Where on List
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); query.Descend("Name").Constrain("gdalen").Contains(); var players = query.Execute<Player>(); var magdalena = players.GetFirst(); // builds a query to get all teams where mihn plays query = odb.Query<Team>(); query.Descend("Players").Constrain(magdalena).Contains(); var teams = query.Execute<Team>(); Console.WriteLine("\nStep 9 (Soda): Team where magdalena plays"); foreach (var team in teams) Console.WriteLine("\t{0}", team); Assert.That(teams, Has.Count.EqualTo(1)); }
Step 9: LINQ to NDatabase query with Where on List
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() where player.Name.Contains("gdalen") select player; var magdalena = players.First(); var teams = from team in odb.AsQueryable<Team>() where team.Players.Contains(magdalena) select team; Console.WriteLine("\nStep 9 (Linq): Team where magdalena plays"); foreach (var team in teams) Console.WriteLine("\t{0}", team); Assert.That(teams.Count(), Is.EqualTo(1)); }
Step 10: Ordering the query result via SODA query
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var query = odb.Query<Player>(); query.Descend("Name").OrderAscending(); var players = query.Execute<Player>(); Console.WriteLine("\nStep 10 (Soda): Players ordered by name asc"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(5)); query.Descend("Name").OrderDescending(); players = query.Execute<Player>(); Console.WriteLine("\nStep 10 (Soda): Players ordered by name desc"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players, Has.Count.EqualTo(5)); }
Step 10: Ordering the query result via LINQ to NDatabase query
using (var odb = OdbFactory.Open(TutorialDb5MinName)) { var players = from player in odb.AsQueryable<Player>() orderby player.Name ascending select player; Console.WriteLine("\nStep 10 (Linq): Players ordered by name asc"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(5)); players = from player in odb.AsQueryable<Player>() orderby player.Name descending select player; Console.WriteLine("\nStep 10 (Linq): Players ordered by name desc"); foreach (var player in players) Console.WriteLine("\t{0}", player); Assert.That(players.Count(), Is.EqualTo(5)); }
Output from the tutorial
Step 3 : Players with name julia julia Step 4 : Players of Voller-ball julia magdalena jacek michal Step 5: Players of Voller-ball julia magdalena jacek michal Step 6 : Volley-ball and Tennis Players julia magdalena jacek michal André Agassi Step 7 : Players that don't play Volley-ball André Agassi Step 8 bis: Players that play Volley-ball julia magdalena jacek michal Step 9: Team where magdalena plays Team Krakow julia magdalena Step 10: Players ordered by name asc André Agassi jacek julia magdalena michal Step 10: Players ordered by name desc michal magdalena julia jacek André Agassi
这篇文章由TonyFang发布。
所有解释权归TonyFang所有。
Mailto: tony-fang@map-le.net