Using Breeze, what is the simplest way to populate a GUID key when an entity is created?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’ll assume that your entity is configured such that the client is responsible for setting the Guid key for new entities. That’s the default for the Guid key of an Entity Framework Code First entity; it is as if the key property were adorned with
[DatabaseGenerated(DatabaseGeneratedOption.None)]The obvious approach is to set the key after creating the entity and before adding it to the manager, e.g.:
function createFoo() { var foo = fooType.createEntity(); foo.id(breeze.core.getUuid()); // Knockout implementation manager.addEntity(foo); }This may be all you ever need.
On the other hand, you may find that you’re creating new Foos in many places and for some strange reason you can’t use the
createFoofunction. You certainly don’t want to repeat that code.You can extend the
Fooentity type with id-setting behavior after which you’d be able to write:function createFoo() { var foo = fooType.createEntity(); // foo.id is set for you manager.addEntity(foo); }There are two approaches to consider – custom constructor and type initializer; both are described in “Extending Entities“
Constructor
You can initialize the key inside a custom constructor. Breeze calls the constructor both when you create the entity and when it materializes a queried entity. Breeze will replace the initial key value when materializing.
Here’s an example that assumes the Knockout model library.
function Foo() { foo.id(breeze.core.getUuid()); // using KO } // one way to get the MetadataStore var store = manager.metadataStore; // register the ctor with the Foo type store.registerEntityTypeCtor("Foo", Foo);Pretty simple. The only downside is that Breeze will generate a Guid every time it makes an entity, whether creating a new one or materializing one from a query. It’s wasted effort during materialization but so what? Well, I suppose that might become a performance issue although I wouldn’t assume so until I had measured it.
Initializer
Suppose you measured and the repeated Guid generation is a serious problem (really?). You could set the key in a type initializer instead and only call the Guid generator when creating a new entity.
Breeze calls a type initializer after the entity has been created or materialized from query just before returning that entity to the application. Clearly you don’t want to overwrite a materialized key from the database so you’ll test the key value to make sure it’s not real (i.e. to make sure you’re fixing a created entity) before assigning it. Here’s an example.
function fooInitializer(foo) { var emptyGuid = "00000000-0000-0000-0000-000000000000"; if (foo.id() !=== emptyGuid) { foo.id(breeze.core.getUuid()); } } var store = manager.metadataStore; // register the initializer; no ctor in this example store.registerEntityTypeCtor("Foo", function(){}, fooInitializer);