We will need to create a Save()
method to handle create and update operations, a Delete()
method to handle deletions, and a few GetX()
methods to retrieve data.
In order to implement the basic Save()
and Delete()
methods, we should do two things:
Insert the logic for the method
Wrap the method in a transaction
Why do we need to wrap it in a transaction? If something goes wrong and our action can't be completed, we can roll back the transaction and handle it in our code.
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; } }
Adding our Save()
method in VB.NET is just as trivial.
Public Function Save(ByVal contact As Contact) As System.Nullable(Of Integer) Dim...