Sunday, 10 February 2013

OS X - Super Simple Database Application with C# in 5 steps

There have been a couple of recent developments in the Mono world that have made creating applications on OS X even easier.

One useful tool that has recently appeared on the scene is Nuget. Visual Studio developers have had access to nuget for many years and now and finally it is available to Mono users. It's made very easy to use via Matt Ward's MonoDevelop nuget add in.

This blog post will walk through using Nuget on MonoDevelop on OS X to create a simple database application.

Install the Nuget Addin into MonoDevelop, there are instructions here, I added a new repository via the Add-in repository manager that points to

http://mrward.github.com/monodevelop-nuget-addin-repository/3.0.5/main.mrep

1) Create a new Console Application


File -> New -> Solution -> Select C# and then Console Project

2) Create the model


The model is a simple POCO that will map directly to the database:

public class Player
{
public int Id { get; set; }
public string Name { get; set; }
}
view raw gistfile1.txt hosted with ❤ by GitHub

Nothing complicated here.


3) Use Nuget to add ServiceStack.Net OrmLite

There are many Object Relational Mappers out there for C#, for example Mono added Microsoft's Entity Framework last year shortly after it became open source. I'll blog about using Entity Framework in the future but for this post I'm going to use an ORM called ServiceStack.Net OrmLite to access a SQLite database.


Goto Project -> Manage Nuget Packages

Add the following package:

  • ServiceStack.OrmLite.Sqlite.Mono

Use the search box to locate the package and hit the Add button to add the package to your project:




4) Add a couple of additional references.


In order to use Linq to query the database a few additional references are required, add these by right clicking on the references folder and selecting Edit References...

  • System.Core
  • System.Data
  • System.Data.Linq


5) Use ORMLite to create the database, populate it with data and query it


public static void Main (string[] args)
{
var dbFactory = new OrmLiteConnectionFactory(
"Arsenal.sqlite".MapAbsolutePath(),
SqliteDialect.Provider);
// Create the database, overwrite any existing data
dbFactory.Run(d => d.CreateTable<Player>(overwrite: true));
IDbConnection db = dbFactory.OpenDbConnection();
// Insert some data into the database
db.Insert(new Player { Id = 1, Name = "SZCZESNY" });
db.Insert(new Player { Id = 2, Name = "DIABY" });
db.Insert(new Player { Id = 3, Name = "SAGNA" });
db.Insert(new Player { Id = 4, Name = "MERTESACKER" });
db.Insert(new Player { Id = 5, Name = "VERMAELEN" });
db.Insert(new Player { Id = 6, Name = "KOSCIELNY" });
db.Insert(new Player { Id = 7, Name = "ROSICKY" });
db.Insert(new Player { Id = 8, Name = "ARTETA" });
db.Insert(new Player { Id = 9, Name = "PODOLSKI" });
db.Insert(new Player { Id = 10, Name = "WILSHERE" });
// Query the Database
var players = db.Select<Player>(p => p.Id > 5);
// Output the query results
foreach (var player in players)
{
Console.WriteLine(player.Name);
}
}
view raw gistfile1.txt hosted with ❤ by GitHub
When run:

The code for this blog post is available for download here.

No comments:

Post a Comment