Way to Simplify?

Dec 8, 2009 at 8:13 AM
Edited Dec 8, 2009 at 8:15 AM
Is there a way to simplify this code?

Expression<Func<TDetail, TMaster>> getMasterExpression = ...arbitrary lambda expression...; Func<TDetail, IValueProvider<TMaster>> getMaster = ExpressionObserver.Compile(getMasterExpression); Expression<Func<TDetail, DetailMaster>> getPairExpression = item => new DetailMaster(item, getMaster(item).Value); Func<TDetail, IValueProvider<DetailMaster>> getPair = ExpressionObserver.Compile(getPairExpression); _DetailMasterCollection = Obtics.Collections.ObservableEnumerable.Select(combinedList, getPair);

I would assume there is some sort of transformation that would condense lines 3 and 4 into a line similar to the Select line, but it wasn't apparent to me what it would be called.

PS.  The elegance and power of Obtics continues to impress.  Hat's off to a great design.

Coordinator
Dec 9, 2009 at 9:01 AM

I think something like this should work:

Expression<Func<TDetail, TMaster>> getMasterExpression = ...arbitrary lambda expression...;
Func<TDetail, IValueProvider<TMaster>> getMaster = ExpressionObserver.Compile(getMasterExpression);
Func<TCombinedList,IValueProvider<IEnumerable<DetailMaster>>> getDetailMasterCollection =
    ExpressionObserver.Compile(
        (TCombinedList combinedList) =>
            combinedList.Select(item => new DetailMaster(item, getMaster(item).Value))
    );

_DetailMasterCollection = getDetailMasterCollection(combinedList).Cascade();    

Note that DetailMaster should be a properly behaving type (two DetailMasters instantiated with the same arguments should be equal).

And thanks :-) I hope you get some good value out of it.