Let's go back to our Data Access Object stubs we created earlier, and add some functionality to them.
We will start by defining a
Save
method that takes aContact
object as a parameter:public int Save(Contact contact) { ITransaction transaction = null; try { transaction = Session.BeginTransaction(); Session.SaveOrUpdate(contact); transaction.Commit(); return contact.Id; } catch (Exception ex) { log.Error(ex); if (transaction != null && transaction.IsActive) transaction.Rollback(); throw; } }
Our VB.NET code is also simple to implement:
Public Function Save(ByVal contact As Contact) As Integer Dim transaction As ITransaction = Nothing Try transaction = Session.BeginTransaction() Session.SaveOrUpdate(contact) transaction.Commit() return contact.Id Catch ex As Exception log.[Error](ex) If transaction IsNot Nothing AndAlso transaction.IsActive Then ...