A simple Domain Sample
The GUID (uniqueidentifier) represents “The Persistent Objet Id”, this is utilized to identify unique object state in a database
Why GUID
Why not use an Identity Field? Because I Think “Identity” breaks the Unit of work Pattern
Martin Fowler say:
“Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems”
With identity you don’t know the ID before inserting the record, you need a round trip to database to know it
Let’s start by creating some new Student instances and persisting them to the database.
var newStudent = new Student { Id = System.Guid.NewGuid(), firstName = "Elisha", lastName = "C." }; //.............................. //...................... //............ // ..Domain.SubmitChanges();
Nice! GUID not need a round trip to database because is generated in the client side
So what happen if I decide use a Silverlight DataFrom to get a rapid CRUD in my application?
The GUID is not auto generated!!, the new GUID is all zeros! Next time I will get an error
I ask on twitter and get this response by @ColinBlair
How I do that?
Go to msdn for documentation: Customizing Generated Code
Client Side:
1. Add a partial class with the same name and namespace as the generated class you want to customize
2. Implement the method that is executed at the time when your custom code must be executed.
Our case: OnCreated() : Executed when the DomainContext object is instantiated.
Done!! Every time the GUID is generated at the client side
I hope that the next WCF Ria Services version there a better way to solve this
Please go vote to get support for “Enable client generated keys for associations”