zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

Config to prevent deletes? #105

Open ghost opened 10 years ago

ghost commented 10 years ago

How can I prevent GraphDiff from removing missing children in the object graph?

I think we can quite easily add a config for this.

mortb commented 9 years ago

I was actually just implementing something quite (but not completely) like this We have webmodels that are populated on demand when loading different tabs in our page. Therefore we may not have everything populated when we end up backend in the Save method. Especially child collections may be null. The code I've just written in CollectionGraphNode.cs looks like:

    public override void Update<T>(IChangeTracker changeTracker, IEntityManager entityManager, T existing, T entity)
        {
            var updateValues = GetValue<IEnumerable>(entity);
            if (updateValues == null)
            {
                if (GraphDiffConfiguration.IgnoreNullSourceCollections)
                {
                    return;
                }
                updateValues = new List<object>();
            }
            var innerElementType = GetCollectionElementType();
            var dbCollection = GetValue<IEnumerable>(existing) ?? CreateMissingCollection(existing, innerElementType);

And then I found your un-merged pull which is solves a different problem in a similar way. I think ignoring null has a larger use case (at least when speaking for our project ;) ) -- in our case we would like to remove items from db when the collection is populated with less or other items than in DB.

Thinking forward, if it was to be a fancy solution it could be implemented via the fluent API... like .AssociatedCollection(entity => entity.SubCollection, UpdateStrategy.IgnoreNullSource) or .AssociatedCollection(entity => entity.SubCollection, UpdateStrategy.Add) for your original use case. (An approach would be to make it an enum that is bitmaskable; it could be specified as UpdateStrategy.Add | UpdateStrategy.Remove | UpdateStrategy.Update)

Of course this could be implemented for Associated/OwnedProperties as well, but the largest gain (again, at least for us) would be for missing child collections

What do you think? Would you consider implementing any of this?