How to define classes:

    public class Person: TransactedObject
    {
        private readonly ITransactedField<Address> _address;
        private readonly ITransactedField<List<Phone>> _phones;

        public Person()
        {
            _address = AddField<Address>();
            _phones = AddCollection<Phone, List<Phone>>(() => new List<Phone>());
        }

        public Address Address
        {
            get
            {
                return _address.Value;
            }
            set
            {
                _address.Value = value;
            }
        }

        public List<Phone> Phones
        {
            get
            {
                return _phones.Value;
            }
        }

        public new void Commit()
        {
            base.Commit();
        }

        public new void Rollback()
        {
            base.Rollback();
        }
    }

    public class Address: TransactedObject
    {
        private readonly ITransactedField<string> _city;
        private readonly ITransactedField<string> _street;
        private readonly ITransactedField<int> _house;

        public Address()
        {
            _city = AddField<string>();
            _street = AddField<string>();
            _house = AddField<int>();
        }

        public string City
        {
            get
            {
                return _city.Value;
            }
            set
            {
                _city.Value = value;
            }
        }

        public string Street
        {
            get
            {
                return _street.Value;
            }
            set
            {
                _street.Value = value;
            }
        }

        public int House
        {
            get
            {
                return _house.Value;
            }
            set
            {
                _house.Value = value;
            }
        }
    }

    public class Phone: TransactedObject
    {
        private readonly ITransactedField<int> _code;
        private readonly ITransactedField<string> _number;

        public Phone()
        {
            _code = AddField<int>();
            _number = AddField<string>();
        }

        public int Code
        {
            get
            {
                return _code.Value;
            }
            set
            {
                _code.Value = value;
            }
        }

        public string Number
        {
            get
            {
                return _number.Value;
            }
            set
            {
                _number.Value = value;
            }
        }
    }


How to use objects:

class Program
{
	static void Main(string[] args)
	{
            Person p = new Person();
            p.Address = new Address()
            {
                City = "City",
                Street = "Street",
                House = 51
            };
            p.Phones.Add(new Phone()
            {
                Code = 123,
                Number = "234-156-132"
            });
            p.Phones.Add(new Phone()
            {
                Code = 890,
                Number = "567-890-456"
            });

            p.Commit();

            Console.WriteLine("Address = " + p.Address.House + ", " + p.Address.Street + " " + p.Address.City);
            foreach (Phone phone in p.Phones)
                Console.WriteLine("Phone = (" + phone.Code + ") " + phone.Number);
            Console.WriteLine();

            p.Phones.RemoveAt(1);
            p.Address.House = 170;
            p.Address.Street = "street5";

            p.Phones.Insert(0, new Phone()
            {
                Code = 999,
                Number = "111-222-333"
            });

            Console.WriteLine("Address = " + p.Address.House + ", " + p.Address.Street + " " + p.Address.City);
            foreach (Phone phone in p.Phones)
                Console.WriteLine("Phone = (" + phone.Code + ") " + phone.Number);
            Console.WriteLine();

            p.Rollback();

            Console.WriteLine("Address = " + p.Address.House + ", " + p.Address.Street + " " + p.Address.City);
            foreach (Phone phone in p.Phones)
                Console.WriteLine("Phone = (" + phone.Code + ") " + phone.Number);
            Console.WriteLine();
	}
}


The output:
Address = 51, Street City
Phone = (123) 234-156-132
Phone = (890) 567-890-456

Address = 170, street5 City
Phone = (999) 111-222-333
Phone = (123) 234-156-132

Address = 51, Street City
Phone = (123) 234-156-132
Phone = (890) 567-890-456

Last edited Oct 1, 2009 at 6:45 PM by antonr, version 2

Comments

No comments yet.