Usage - Basic Sample

This page demonstrates basic usages of the library.

Note: this code can be found as part of the source code in the U01_Basic.cs file.

Business Data

For the following sample, we will work with two class: Player and Team representing a basic parent-child relation.

Phone.Storage.Usage.png

/// <summary>
/// Player class - basic version.
/// </summary>
public class Player
{
    /// <summary>
    /// Gets or sets the identifier of the player.
    /// </summary>
    public int Identifier { get; set; }

    /// <summary>
    /// Gets or sets the name of the player.
    /// </summary>
    public string Name { get; set; }
}

/// <summary>
/// Team class - basic version.
/// </summary>
public class Team
{
    /// <summary>
    /// Gets or sets the identifier of the team.
    /// </summary>
    public int Identifier { get; set; }

    /// <summary>
    /// Gets or sets the name of the team.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the players that compose the team.
    /// </summary>
    public Player[] Players { get; set; }
}

Basic Usage

// Initialize the data source
IDataSource ds = Engine.Instance.RegisterDataSource(new DefaultDataSourceDefinition("TeamDS")
    .AddClass<Player, int>(p => p.Identifier)
    .AddClass<Team, int>(t => t.Identifier));

// Create business objects
Player player = new Player() { Identifier = 1, Name = "Superman" };
Team team = new Team() { Identifier = 1, Name = "Justice League" };
team.Players = new Player[] { player };

// Save the data
// Note that player is automatically saved as part of the team hierarchy
ds.Save(team);

// Load a specific instance
Player loaded = ds.Load<Player>(1);

// Remove all the data source elements
ds.RemoveEverything();

Related References

Last edited Mar 28, 2011 at 9:19 PM by Faz, version 2

Comments

No comments yet.