All sample code is organized as static methods of the Program
class. The first and probably the simplest is EnterFreud
. It creates
- a
Location
instance to accommodate Dr Freud - a
Person
instance representing Dr Freud - a
PhoneNumber
instance for Dr Freud
Here is the code:
Code Block |
---|
static void EnterFreud () { using (ClientTransaction.CreateRootTransaction ().EnterDiscardingScope ()) { var loc = Location.NewObject (); loc.Street = "Berggasse"; loc.Number = "19"; loc.City = "Vienna"; loc.Country = Country.Austria; loc.ZipCode = 1090; var person = Person.NewObject (); person.FirstName = "Sigmund"; person.Surname = "Freud"; person.Location = loc; var pn = PhoneNumber.NewObject (); pn.CountryCode = "0043"; pn.AreaCode = "1"; pn.Number = "3191596"; pn.Person = person; ClientTransaction.Current.Commit (); } } |
...
Domain objects, transaction and transaction scope conspire to ensure that
- operations on domain objects are always limited to transaction scopes
- transaction scopes limit the life expectancy and visibility of the transaction it belongs to
ClientTransaction.Current
always references the transaction passed in the scope- domain objects have no life outside of client transactions
Rollback
As you might guess, RollBack
undoes all modifications to domain objects since the last Commit
. If you glue this code into EnterFreud
after the ClientTransaction.Current.Commit()
, you will rename Dr Freud to "Arnold", but back up from it (as evidenced by the succeeding Console.WriteLine
:
...