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 102 forks source link

OrderBy while loading child entities #157

Open b9chris opened 8 years ago

b9chris commented 8 years ago

This is less of an Issue and more a Discussion.

EFGraphDiff is a huge help when saving, but I've found I come up wanting when loading for a couple of reasons, and I want to focus in on the big one here: OrderBy.

EFGraphDiff doesn't use an OrderBy when loading an Entity's child Collections from the db.

These combine to mean EFGraphDiff's loading capabilities are in the way of the right way of loading these Entities. I've put together code that lets you declaratively mark a class with how you'd like it ordered when loading as a child of a query. I've not addressed this but it would be reasonably easy to override the declarations when you want to via a config.

I can just post it as a separate library and people can combine them if they wish, but this does seem like a blindspot for EFGraphDiff so I wonder if this or some portion of it belongs in EFGraphDiff. The work was relatively significant since it requires emitting custom classes (that act like a runtime Anonymous class) and then asking EF to fill them with custom Select statements that are the sum of many, many Lambda Expressions.

For example:

        var propColl = Expression.Property(selectParam, dbProp);
        var orderByParam = Expression.Parameter(itemType, "p");
        var orderIndexProp = itemType.GetProperty(orderedPropName);
        var orderByExp = Expression.Lambda(
            Expression.MakeMemberAccess(orderByParam, orderIndexProp),
            orderByParam
        );
        var orderedProp = Expression.Call(
            typeof(Enumerable), "OrderBy",
            new[] { itemType, typeof(int) },
            propColl,
            orderByExp
        );
        var bindSetter = Expression.Bind(runtimeProp.SetMethod, orderedProp);

Just wanted to start the discussion. If I post this as a separate library as well I'll link back here.