Breeze: How can I create a GUID key for new entities on the client?
Asked Answered
D

2

11

Using Breeze, what is the simplest way to populate a GUID key when an entity is created?

Dinahdinan answered 12/12, 2012 at 22:42 Comment(0)
T
15

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 createFoo function. You certainly don't want to repeat that code.

You can extend the Foo entity 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);
Thundering answered 12/12, 2012 at 23:40 Comment(2)
The breeze.core.getUuid method mentioned above is an undocumented, unofficial Breeze utility. I think it will become official ... perhaps under a different name ... but I can't confirm that. Use at your own risk (as I do) or copy its implementation and make it your own utility.Thundering
just double-checking: Is the exclamation point a typo on the if statement?Motionless
S
1

Assuming you have a Guid surrogate Key on all your entities like we have in our case, you could code a createInstance factory that does the following in a very generic approach:

function createInstance(breezeEntityManager, typeName) {
    var keyProperty = breezeEntityManager.metadataStore.getEntityType(typeName, false).dataProperties.filter(function (p) {
        return p.isPartOfKey;
    })[0];
    var config = {};
    config[keyProperty.name] = breeze.core.getUuid();
    return breezeEntityManager.createEntity(typeName, config);
}

This way, you won't have to create an initializer for all your entities.

Saccharin answered 9/4, 2014 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.