joelweiss/ChangeTracking

Add ability to disable tracking on desired properties

mattbrailsford opened this issue · 6 comments

I know in #18 it was asked if there was a way to ignore certain members, but I think it would be a handy feature, so I'm raising a new issue to discuss how this could be implemented. I'm happy to contribute, but I'm pretty new to the source so could do with some guidance on where / how this could be done.

I have 2 ideas, 1 being that you add an option to the AsTrackable to ignore non-virtual properties. This way one could just disable tracking by making it non-virtual. This does have a problem though if you have a base class you want to allow properties to be overridden on though so option 2 is to introduce a DoNoTrackChangesAttribute that can be added to properties to signify that this property shouldn't be tracked.

I think you could introduce both, but if you just wanted 1 solution that would work, I think the attribute is the better option.

As I say, I'd be happy to contribute this feature, I'd just like to ask for some guidance on where this would get added in the code (I'm still trying to figure things out).

Matt

Hmm, could this be as simple as adding a check for the attribute on the property inside CanCollectionBeTrackable and CanComplexPropertyBeTrackable? Or are there other areas I would need to consider?

Ahh, no, appears you need more in order to prevent it firing events. I do have something working whereby all property interceptors check before triggering events whether the property should be tracked, and if not just call invocation.Proceed(); and technically all the unit tests pass, but I'm not sure if over all behavior is right. I'll need to write some more unit tests to check.

If you don't mind test if ec2ea2e5 will work in your scenario.

To exlude a property from being tracked, apply the DoNoTrack attribute to the property or to the the property class.

public class Order
{
    [DoNoTrack]
    public virtual Address Address { get; set; }

    //will not be tracked bacause the Lead class is marked [DoNotTrack].
    public virtual Lead Lead { get; set; }
}

[DoNoTrack]
public class Lead
{
    public virtual int LeadId { get; set; }
}

Is that Attribute supposed to be DoNotTrack?

Is that Attribute supposed to be DoNotTrack?

Yes