tumtumtum / Shaolinq

ORM for .NET with full LINQ support for Postgres, Sqlite, MySql and SqlServer
Other
127 stars 19 forks source link

Inconsistencies in changed properties on data model hook callbacks #98

Open samcook opened 5 years ago

samcook commented 5 years ago

I've come across a couple of inconsistencies with tracking what properties have changed using data model hooks.

I've pushed a branch with some test cases demonstrating the issue here: samcook/Shaolinq@11d0e0c740f09b76024995fb9ab216ee8cbede4a (or see branch feature/data-hook-props-changed)

Part of the issue seems to be down to database generated properties, and maybe also a difference in behaviour between create and update.

Examples

[Test]
public void Test_Changed_Properties_Long_AutoIncrement()
{
    long id;

    using (var scope = new DataAccessScope())
    {
        var obj = this.model.ObjectWithLongAutoIncrementPrimaryKeys.Create();

        scope.Flush();

        id = obj.Id;

        scope.Complete();
    }

    using (var scope = new DataAccessScope())
    {
        var obj = this.model.ObjectWithLongAutoIncrementPrimaryKeys.GetByPrimaryKey(id);

        obj.Id = 200L;

        scope.Complete();
    }
}

In this example, during the first scope (object creation) the auto-generated Id field isn't listed as a changed property in any of the callbacks.


[Test]
public void Test_Changed_Properties_Long_NonAutoIncrement()
{
    var id = 100L;

    using (var scope = new DataAccessScope())
    {
        var obj = this.model.ObjectWithLongNonAutoIncrementPrimaryKeys.Create(id);

        scope.Complete();
    }

    using (var scope = new DataAccessScope())
    {
        var obj = this.model.ObjectWithLongNonAutoIncrementPrimaryKeys.GetByPrimaryKey(id);

        obj.Id = 200L;

        scope.Complete();
    }
}

In this example, during the first scope's commit (creating the object), the Id is available during BeforeSubmit (twice actually, via both the New and Updated collections), but not during AfterSubmit.

However, during the second scope's commit (updating the object), the changed Id property is available during both BeforeSubmit and AfterSubmit.

I'll look into it some more, but let me know if you have any thoughts on it @tumtumtum.