Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnDeleteTransactor has no effect when context is obtained with DbProxy.GetContext #23

Open
miyconst opened this issue Nov 9, 2020 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@miyconst
Copy link
Member

miyconst commented Nov 9, 2020

Reported by @Dhaigvip.

A simple scenario to reproduce the issue:

public void TriggerCallbackWhenDeleted()
{
    var transactor = CreateServices(
        s => s.Decorate<ITransactor, OnDeleteTransactor>())
        .GetRequiredService<ITransactor>();

    var wasDeleted = transactor.Transact(db =>
    {
        var p = db.Insert<Person>();

        var deleted = false;
        p.WhenDeleted(p => deleted = true);

        db.Delete(p);

        return deleted;
    });

    Assert.True(wasDeleted);

    wasDeleted = transactor.Transact(db =>
    {
        var p = db.Insert<Person>();

        var deleted = false;
        p.WhenDeleted(p => deleted = true);

        // The WhenDeleted method is not called,
        // because DbProxy.GetContext returns the original not decorated `IDatabaseContext`.
        db = DbProxy.GetContext(p);
        db.Delete(p);

        return deleted;
    });

    // This fails.
    Assert.True(wasDeleted);
}
@miyconst miyconst added bug Something isn't working enhancement New feature or request labels Nov 9, 2020
@miyconst
Copy link
Member Author

miyconst commented Nov 9, 2020

This limitation can be easily worked around with the following code:

public class OnDeleteTransactor : TransactorBase<object>
{
    /* ... */

    public static IDatabaseContext GetContext(object proxy)
    {
        IDatabaseContext db = DbProxy.GetContext(proxy);
        return new OnDeleteContext(db);
    }

    /* ... */
}

Later on, the OnDeleteTransactor.GetContext method should be used instead of DbProxy.GetContext.

@miyconst
Copy link
Member Author

miyconst commented Nov 9, 2020

cc @per-samuelsson

@eriksunsol
Copy link

I can confirm that the proposed solution seems to work as expected.

eriksunsol added a commit to ModularManagement/Starcounter.Database.Extensions that referenced this issue Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants