GraphDiff: "Cascading" Owned Collections

287 Views Asked by At

I'm trying to use GraphDiff (latest available version in NuGet) to handle what I consider a not terribly difficult entity model. Consider a model like so:

class A 
{
    public virtual ICollection<B> Bs { get; set; }
}

class B
{
    public virtual ICollection<C> Cs { get; set; }
}

If I'm updating an instance of A (call it aEntity), I could do:

context.UpdateGraph(aEntity, map => 
    map.OwnedCollection(a => a.Bs, withB => 
        withB.OwnedCollection(b => b.Cs)))

Now I'd also, sometimes, like to update B's independently.

context.UpdateGraph(bEntity, map => map.OwnedCollection(b => b.Cs));

So I figured I could "cascade" the changes by introducing a property, like so:

class BMapper {
    Expression<Func<IUpdateConfiguration<B>, object>> MappingExpression 
    {
        get 
        {
            return map => map.OwnedCollection(b => b.Cs);
        }
    }
}

... and then use that in both scenarios like so:

// Update an A and any of its B's
context.UpdateGraph(aEntity, map =>
    map.OwnedCollection(a => a.Bs, (new BMapper()).MappingExpression))

// Update a B by itself
context.UpdateGraph(bEntity, (new BMapper()).MappingExpression);

Updating the B by itself works fine, but updating an A falls down in expression land:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
    'string' does not contain a definition for 'Body'

Is there a way to share mappings in GraphDiff?

0

There are 0 best solutions below