Julián Yuste
Julián Yuste

Reputation: 1472

what to expect at the client side when returning false from BeforeSaveEntity

I'm looking for documentation about what to expect at the client side when returning false from BeforeSaveEntity(EntityInfo entityInfo) but i found nothing so I decided to experiment myself.

I made a test on the Doccode project:

test("create customer returning false from contextprovider", 1, function () {

    var em = newEm();
    var category = em.createEntity('Category', { CategoryName: 'Julian Category' });

    stop();

    em.saveChanges().then(function (saveResult) {
        //ok(category.entityAspect.entityState.isAdded(), "Added state because contextprovider return false");
        ok(em.hasChanges() === false,"No changes pending");
    })
    .fail(function (error) {
        debugger;
    })
    .fin(function () {
        start();
    });
});

And i found that the two assertions were true, so i think that it may be a bug.

To make the test i created a custom provider:

public class CustomEFContextProvider : EFContextProvider<NorthwindContext>
{
    public CustomEFContextProvider() : base()
    {

    }

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)
    {
        if(entityInfo.Entity.GetType() == typeof( Category)){
            return false;
        }

        return true;
    }
} 

and changed the NorthwindController to use it:

readonly CustomEFContextProvider _contextProvider =
    new CustomEFContextProvider();

I'm returning false when a Category is saved, so the category doesn't get inserted on the database and it's not returned in the saveResult.entities array. The keyMappings is also empty. All of this is what i expected. What i didn't expect is that the entitymanager.hasChanges function returns false because the category entity is marked as added, what in my opinion leaves the manager inconsistent.

It's a bug? I'm doing something wrong? Were my expectations wrong?

Thx.

Upvotes: 1

Views: 177

Answers (2)

Jay Traband
Jay Traband

Reputation: 17052

Ok, as of v 1.2.5 ( just released), this should be fixed. Please post back to confirm if possible, and thx for the bug report. :)

Upvotes: 1

Jay Traband
Jay Traband

Reputation: 17052

Hm... it's an interesting question.

The reason that EntityManager.hasChanges returns true is that you haven't saved the "added" Category object, and therefore breeze doesn't call EntityAspect.acceptChanges on it because it is still in fact in an "added" but not savable state.

I think this is actually the right behavior, because if we were to call acceptChanges automatically on this entity, you would never know on the client that it had not been saved. You can of course call acceptChanges directly within the promise callback.

Does this make sense?

Upvotes: 0

Related Questions