Welcome Guest, you are in: Login

dbones docs

RSS RSS

Navigation (ArmChair)





Search the wiki
»

Session API

RSS
Modified on 2014/09/14 05:12 by Dave Categorized as Uncategorized

Table of Contents [Hide/Show]


   Overview
Session
   Add
   Remove
   GetById
   GetByIds
   Attach

Overview

This article explores the Session API operation, providing a brief description and example.

Session

The session represents a unit of work and it is not thread-safe. Any instance which is added/loaded/attached to a session will be tracked for changes. When Commit is called on a session, these changes will be executed against the database using CouchDb's Bulk interface in an All or Nothing transaction.

Add

Adds in instance to the session, which will then be commited to the database.

NOTE:
If no Id has been assigned before the instance is added to the Session, then ArmChair will assign it.
After the object is commited to the database, the revision will then be set onto the instance


using (var session = Database.CreateSession())
{
    var author = new Person("dave");
    session.Add(author); //Id is assigned
       
    session.Commit();    //Revision is assigned
}

Remove

Marks an instance as deleted in the session, the database will be updated on Commit().


using (var session = Database.CreateSession())
{
    var author = session.GetById<Person>(id);
    session.Remove(author);
                
    session.Commit();
}

GetById

Load a single object directly from the database, returns null if the instance does not exist.

NOTE:
If the object has already been loaded into the session, then this command will load it from the session cache


using (var session = Database.CreateSession())
{
    var person = session.GetById<Person>(id);
    //do some work.
}

GetByIds

Loads a collection of objects into the session, if the instance does not exist then it will not have an entry in the returned collection.
NOTE:
If the object has already been loaded into the session, then this command will load it from the session cache


using (var session = Database.CreateSession())
{
    var ids = new List<string> {"id1", "id2"};
    var people = session.GetByIds<Person>(ids);
    //do some work
}

Attach

Slightly more advanced, however very helpful for manipulating an instance across multiple sessions.

Attach an object which was loaded from a previous/another session. To make changes to an existing object in a new session, you have to attach it first, then make any changes.


using (var session = Database.CreateSession())
{
    //attach the book to this session
    session.Attach(book);

    var contibutor = new Person("bob");
    session.Add(contibutor);

    book.AddContributor(contibutor, ContributorType.Editor);

    session.Commit();
}
dbones docs, is a sub-site of dbones.co.uk, all its content belongs to dbones.co.uk, this also includes logo's.