Ninja Database Pro has implicit and explicit transactions. When saving an object with relationships defined such as an order having order details, the save is automatically wrapped in a transaction and it is rolled back if there is a problem. There is also an implicit transaction if a list of items are saved.
Example of an Implicit Tree Transaction
NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase(); //Valid only for non Silverlight projects
Order order = new Order();
order.Buyer = new Person();
order.Buyer.Name = "John Doe";
order.Buyer.DateCreated = DateTime.Now;
order.OrderDetails = new List<OrderDetail>();
OrderDetail detail1 = new OrderDetail();
detail1.Item = new Product();
detail1.Item.Name = "Wizard";
detail1.Item.Cost = 29.98M;
detail1.Quantity = 2;
detail1.Order = order;
order.OrderDetails.Add(detail1);
OrderDetail detail2 = new OrderDetail();
detail2.Item = new Product();
detail2.Item.Name = ".NET Caching Library";
detail2.Item.Cost = 199.95M;
detail2.Quantity = 1;
detail2.Order = order;
order.OrderDetails.Add(detail2);
db.OpenDatabase();
//Everything will be saved to the database in separate tables in an implicit transaction
//You must have the relationships set up as attributes. See setting up relationships.
//This is wrapped in an implicit transaction, if any record fails it will all roll back
db.Save(order);
//Reload the entire tree
Order orderCopy = db.Load<Order>(order.OrderId);
//Get just a single object
Person personCopy = db.Load<Person>(order.Buyer.PersonId);
db.CloseDatabase();
Example of an Implicit List Transaction
NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase(); //Valid only for non Silverlight projects
db.OpenDatabase();
List<Person> persons = new List<Person>();
Person person1 = new Person();
person1.DateCreated = DateTime.Now;
person1.Name = "Greg1";
persons.Add(person1);
Person person2 = new Person();
person2.DateCreated = DateTime.Now;
person2.Name = "Greg2";
persons.Add(person2);
//Save the entire list as separate records in the persons table
//This is wrapped in an implicit transaction, if any record fails it will all roll back
db.Save(persons);
List<Person> personsCopy = db.LoadAll<Person>();
db.CloseDatabase();
Example of an Explicit Transaction
NinjaDbPro db = new NinjaDbPro("MyDatabaseDirectory", "MyDatabaseName");
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase(); //Valid only for non Silverlight projects
Person person1 = new Person();
person1.Name = "John";
Person person2 = new Person();
person2.Name = "Jane";
db.OpenDatabase();
//These two persons will be saved as a group
try
{
db.BeginTransaction();
db.Save(person1);
db.Save(person2);
db.CommitTransaction();
}
catch (Exception)
{
//Rollback when there is an error
db.RollbackTransaction();
throw;
}
//This will load a person since it was committed
Person person1Copy = db.Load<Person>(person1.PersonId);
db.CloseDatabase();