NDatabase 1 Minute Tutorial


Here is a simple example to demonstrate how it is easy to persist objects with NDatabase, and then how to use persisted objects.

Here, a C# instance is created, the NDatabase database is opened, the store method is called to save two objects and then the database is closed (auto commit). After that we could use persisted objects, so database is opened again and then we are displaying the content of persisted objects.

To restore instance by class, we simply need to use Query<T> method to create a query for T type objects, and then we are executing this query. The result will contain all objects derived from mentioned base class or interface.

const string dbFileName = "game.db";

const double mageAttackValue = 3.3;
const double mageDefenseValue = 3.4;

const double warriorAttackValue = 4.4;
const double warriorDefenseValue = 2.2;

// create two objects
IHero mage = new Mage("Merlin", mageAttackValue, mageDefenseValue);
IHero warrior = new Warrior("Conan", warriorAttackValue, warriorDefenseValue);

// store them
using (var odb = OdbFactory.Open(dbFileName))
{
    odb.Store(mage);
    odb.Store(warrior);
}

// retrieve them by classes and by interface
using (var odb = OdbFactory.Open(dbFileName))
{
    // work with mages
    var mages = odb.Query<Mage>().Execute<Mage>();
    foreach (var hero in mages)
        Console.WriteLine(hero);

    // work with warriors
    var warriors = odb.Query<Warrior>().Execute<Warrior>();
    foreach (var hero in warriors)
        Console.WriteLine(hero);

    Console.WriteLine("Start working with IHero interface.");

    // work with heroes
    var heroes = odb.Query<IHero>().Execute<IHero>();
    foreach (var hero in heroes)
        Console.WriteLine(hero);

Output from the tutorial

[Mage] Name: Merlin, Attack: 3,3, Defense: 3,4
[Warrior] Name: Conan, Attack: 4,4, Defense: 2,2

Start working with IHero interface.

[Mage] Name: Merlin, Attack: 3,3, Defense: 3,4
[Warrior] Name: Conan, Attack: 4,4, Defense: 2,2

The base set of classes used in this sample

Simple interface IHero which describes any hero implementation.

public interface IHero
{
    string Name { get; }

    double Attack { get; }
    double Defense { get; }
}
Mage implementation of IHero interface. Represents the Mage.

public sealed class Mage : IHero
{
    public Mage(string name, double attack, double defense)
    {
        Name = name;
        Attack = attack;
        Defense = defense;
    }

    #region Implementation of IHero

    public string Name { get; private set; }
    public double Attack { get; private set; }
    public double Defense { get; private set; }

    #endregion

    public override string ToString()
    {
        return string.Format("[Mage] Name: {0}, Attack: {1}, Defense: {2}",
                                        Name, Attack, Defense);
    }
}
Warrior implementation of IHero interface. Represents the Warrior.

public sealed class Warrior : IHero
{
    public Warrior(string name, double attack, double defense)
    {
        Name = name;
        Attack = attack;
        Defense = defense;
    }

    #region Implementation of IHero

    public string Name { get; private set; }
    public double Attack { get; private set; }
    public double Defense { get; private set; }

    #endregion

    public override string ToString()
    {
        return string.Format("[Warrior] Name: {0}, Attack: {1}, Defense: {2}", 
                                        Name, Attack, Defense);
    }
}
If you want to learn more about NDatabase, click here to have a look at the 5 minutes tutorial.

Last edited Dec 29, 2012 at 8:34 AM by jacek, version 12

Comments

Ticols Feb 20, 2013 at 12:20 PM 
It is wonderful! Thanks,I will try.