- Creating the Persistent Class
- Creating the Mapping Files
- Querying the Customer
- Updating the Customer
- CMP vs. BMP
- Summary
Updating the Customer
Of course, you can also create new rows in a database using ObjectSpaces. This is done using the CreateObject method of the IObjectSpace interface. The code in Listing 4 creates a new Customer and populates its properties before calling the Update method to persist the object in the data store.
Listing 4: Creating a New Customer
myCustomer = CType(os.CreateObject(GetType(Customer)), Customer) With myCustomer .FName = "Beth" .LName = "Fox" .Address = "21508 W44th" .City = "Overland Park" .StateProv = "MO" .PostalCode = "33221" .EmailAddress = "bethafox@foxden.com" End With Try ' Save Changes os.Update(myCustomer) Catch ex As UpdateException ' Handle error End Try Console.WriteLine("New ID = " & myCustomer.Id)
You'll notice from Listing 4 that the Update method is passed the new customer. The framework then proceeds to generate an INSERT or UPDATE SQL statement, as appropriate, and execute it on the data source. If the update succeeds, the framework automatically populates the new Id generated from the database.
If multiple customers are modified or added, the UpdateAll method can be used instead and can accept different arguments specified in the UpdateBehavior enumeration. For example, specifying the ThrowAtFirstError value will throw an exception as soon as the first error is returned from the data source. The IObjectSpace interface also supports Resync and ResyncAll methods that can be used to synchronize one or more objects from the data store to the client.
NOTE
Although persistence managed by the ObjectSpaces framework does not support stored procedures directly in the technical preview version, look for it to do so at least with the SQL Server .NET data provider in future releases.