Wiki
Clone wikiBoot.Multitenancy / Transactions
#Transactions made easy ##Introducing SessionHostFactory
The SessionHostFactory makes it a piece of cake to create updates against a database. All done with within Transaction statement.
Take a look at the following code.
#!c# public void TransactionSave<T>(T t) where T : class { using (var session = SessionHostFactory.With<T>().OpenSession()) { if (session.QueryOver<T>().RowCount() == 0) { session.WithTransaction(s => { s.Save(t); }); } } }
The SessionHostFactory transfer the current object to the extension "With<T>" which has transaction management built in.
Now look at the following code.
Here we are creating two different objects, and persist these to the function above, TransactionSave<T>. This function is a extension of the WithTransaction action.
#!c# var s = new Settings { Id = 1, Title = "Boot Project", FooterText = "(c) All rights reserved Boot Project" }; TransactionSave<Settings>(s); var page = new Page() { Id = 1, Action = "Index", Controller = "Home", Active = true, MetaTitle = "Boot Project", ParentId = 0, Title = "Start", Url = "" }; TransactionSave<Page>(page);
##Specific objects You can also specify your "Entity" to save thrue the transaction extension.
#!c# public void CreateContent() { using (var session = SessionHostFactory.With<Content>().OpenSession()) { if (session.QueryOver<Content>().RowCount() == 0) { var content1 = new Content { Id = 1, PageId = 1, Title = "Boot Multitenancy", Html = "<p class='lead'>Read our guidelines at our development website bitbucket. There's a lot of valueable information when using this project in your application.</p>" }; var content2 = new Content { Id = 2, PageId = 1, Title = "Getting started", Html = "<p>Visit Bitbucket to learn how to get started, and read information about this and other project in this serie.</p>" }; var content3 = new Content { Id = 3, PageId = 1, Title = "Get more libraries", Html = "<p>Visit the developers site where you can download several project.</p>" }; var content4 = new Content { Id = 4, PageId = 1, Title = "About Boot Project", Html = "<p>Boot Project is a project for developing and enhance content management systems. Project are built as their own solutions and can be deployed into any application.</p>" }; session.WithTransaction(s => { s.Save(content1); }); session.WithTransaction(s => { s.Save(content2); }); session.WithTransaction(s => { s.Save(content3); }); session.WithTransaction(s => { s.Save(content4); }); session.Flush(); } } }
Updated