Getting started

Daniel Wertheim edited this page Nov 29, 2012 · 25 revisions

The concepts of SisoDb are really simple. You design a pure POCO (plain old clr-object) without any demands on interfaces or base classes what so ever. In SisoDb we call this a Structure. You could look at a Structure as a document in a document-oriented database like MongoDb.

Pure POCOS, really?

Yes. No proxies or base-classes or interfaces. The only demand SisoDb has, is that you add one property containing the Id of the structure. The property should be named, either: SturctureId, [TypeNameId], I[MyInterface]Id or Id. The type of it could be: Guid, String, int, long, ... Read more here

public class Customer
{
    public Guid Id { get; set; }
    public int CustomerNo { get; set; }
    public string Name { get; set; }
}

Chose a provider and create a database

The next step is to create a database which will hold your structures. As of now there's support for:

  • Sql-Server Azure
  • Sql-Server 2005
  • Sql-Server 2008
  • Sql-Server 2012
  • SqlCE4

Note! The Sql-Server providers support Express edition as well. The providers are preferably installed into your project via NuGet, e.g using:

PM> Install-Package SisoDb.Sql2012

You can read more about installation/distrubution here.

When creating a database you pass an implementation of ISisoConnectionInfo to an ISisoDbFactory. There are also some helpers/extensions that lets you say "cnStringOrName".CreateSql2012Db(). When doing this, it doesn't really create the database, it creates an instance of ISisoDatabase that you use to interact with your database.

NOTE! Each database returned should be kept alive for the durability of the application. Read more here

var cnInfo = new Sql2012ConnectionInfo("myConnectionStringName");
//OR
var cnInfo = new Sql2012ConnectionInfo(
    @"Data source=.\sqlexpress;Initial catalog=SisoDb.Sample;Integrated security=SSPI;");

//Keep the db-instance as a singleton or similar. It holds references
//to cached schemas etc and is designed to be long lived
var db = new Sql2012DbFactory().CreateDatabase(cnInfo);
db.EnsureNewDatabase();

The above could be simplified using some extension methods:

var db = "connectionstring | connectionstringname".CreateSql2012Db();
db.EnsureNewDatabase();

You can read more about connections here, like machine specific connections.

Time to insert

The absolutely easiest way to insert something in SisoDb, is doing it like this:

var customer = new Customer
{
    CustomerNo = 42,
    Firstname = "Daniel",
    Lastname = "Wertheim"
};
db.UseOnceTo().Insert(customer);

or if you have a bunch of them:

db.UseOnceTo().InsertMany(customers);

That's it! Really. There's nothing more to it. NOTE, that I use something called UseOnceTo. Essentially what is happening is the following:

using(var session = db.BeginSession())
{
    //Alternative 1
    session.Insert(customer);

    //Alternative 2
    session.InsertMany(customers);
}

A Session in SisoDb is designed to be short lived, since it opens a connection and starts a transaction against the database. Hence, UseOnceTo should not be used by invoking multiple calls right after each other, then you are better of using the BeginSession directly. Read more about sessions here.

Time to query

Querying is done using lambda expressions. They are translated to SQL under the hoods and executed on the server side (in the SQL-server that is).

var customer = db.UseOnceTo().Query<Customer>().Where(c => c.CustomerNo == 42).SingleOrDefault();

Please, again, NOTE that UseOnceTo is used and you should not call it repeatedly after each other, then you are better of using a Session.

using(var session = db.BeginSession())
{
    var customer = session.Query<Customer>().Where(c => c.CustomerNo == 42).First();
    var orders = session.Query<Order>().Where(o => o.CustomerNo == 42).ToList();
}

Keep in mind. Sessions are designed to be short lived. Create them, use them and dispose them. Read more about sessions here.

Session is transactional

SisoDb doesn't make any distinction between Read- or Write sessions. And as long as the underlying provider supports transactions, a transaction is used for the Session. And as long as no exception is thrown, the transaction will be committed.

Continue

This was the basics of how to get started. Obviously there's lots of more to it, like: Transforming objects when returning them, using paging, time stamps, concurrency tokens, ..., . Just read on in the wiki to learn more.