Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#

Taking Sterling for a Test-Drive

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
27 Jan 2011CPOL 5.3K   3  
Taking Sterling for a Test-Drive

What is Sterling?

“Sterling is a lightweight object-oriented database implementation for Silverlight and Windows Phone 7 that works with your existing class structures. Sterling supports full LINQ to Object queries over keys and indexes for fast retrieval of information from large data sets.”

If you ever need to store large volumes of data on your phone, sterling is a very cool object-oriented database available on WP7! Let’s take a look at it…

Before we start, let’s look at the model we want to store in this database?

C#
public class Match
{
    public int Id { get; set; }
    public string Team1 { get; set; }
    public int Score1 { get; set; }
    public string Team2 { get; set; }
    public int Score2 { get; set; }
    public string Date { get; set; }
}

Next, the database used to store the data:

C#
public class Super14Database : BaseDatabaseInstance  
{
    public override string Name
    {
        get { return "Super14Database"; }
    }

    protected override List<ITableDefinition> _RegisterTables()
    {
        return new System.Collections.Generic.List<ITableDefinition>  
        {  
            CreateTableDefinition<Match,int>(i=>i.Id)  
        };  
    }
}

Now we can setup sterling:

C#
_engine = new SterlingEngine();
_engine.Activate();
_database = _engine.SterlingDatabase.RegisterDatabase<Super14Database>();

We are ready!

In this article, I will just look at a simple example of saving data into the database and getting it out again using a query. Here is how to save data to the database:

C#
_database.Save<Match>(new Match() { Id = 0, Team1 = "Blues", Score1 = 20, 
	Team2 = "Hurricanes", Score2 = 34, Date = "12/02/10" });
_database.Save<Match>(new Match() { Id = 1, Team1 = "W Force", Score1 = 15, 
	Team2 = "Brumbies", Score2 = 24, Date = "12/02/10" });
_database.Save<Match>(new Match() { Id = 2, Team1 = "Cheetahs", Score1 = 34, 
	Team2 = "Bulls", Score2 = 51, Date = "12/02/10" });
_database.Save<Match>(new Match() { Id = 3, Team1 = "Crusaders", Score1 = 32, 
	Team2 = "Highlanders", Score2 = 17, Date = "13/02/10" });
_database.Save<Match>(new Match() { Id = 4, Team1 = "Reds", Score1 = 28, 
	Team2 = "Waratahs", Score2 = 30, Date = "13/02/10" });
_database.Save<Match>(new Match() { Id = 5, Team1 = "Lions", Score1 = 13, 
	Team2 = "Stormers", Score2 = 26, Date = "13/02/10" });
_database.Save<Match>(new Match() { Id = 6, Team1 = "Sharks", Score1 = 18, 
	Team2 = "Chiefs", Score2 = 19, Date = "13/02/10" });

and here is how to get it out again using a simple query:

C#
var matches = (from _ in _database.Query<Match, int>()
                select _.LazyValue.Value).ToList();

Easy, isn’t it?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --