Skip to main content
Polished the code as per t3chb0t's comment
Source Link
public interface IProjectTree<T>
    : IEnumerable<IProjectTree<T>> // !!! The implementation should rely on Children field
{
    IEnumerable<IProjectTree<T>> Children { get; set; }
    IEnumerable<ActivityDto> Activities { get; set; }

    T SubtreeSpecificData { get; set; }
}
public static class Traverse
{
    public static IProjectTree<T> ShakeOff<T>(this IProjectTree<T> targetTree, Func<ActivityDto, bool> activityPredicate)
    {
        if (!targetTree.Children == nullAny())
        {
            targetTree.Children = new List<IProjectTree<T>>();
        }
        else
        {
            targetTree.Children = targetTree
                .Children
                .Select(subTree => TraversesubTree.ShakeOff(subTree, activityPredicate))
                .ToList();
        }

        targetTree.Children = targetTree.Children.Where(subtree => subtree.ActivityPredicateApplies(activityPredicate));

        return targetTree;
    }

    public static bool ActivityPredicateApplies<T>(this IProjectTree<T> target, Func<ActivityDto, bool> activityPredicate)
    {
        target.Activities = target.Activities.Where(activityPredicate);

        var hasSubtreesAndUnfilteredActivities = target.Children.CountAny() > 0 && target.Activities.CountAny() > 0;;
        return hasSubtreesAndUnfilteredActivities;
    }
}
  1. Traverse.ShakeOff() method is not a pure function, it modifies the provided targetTree object in-place. This may be dangerous in some scenarios (but probably, not in your case).
  2. The code above may or may not be easy to apply due to architectural changes -- it's your judgement call. If I were you, I would not limit myself to a rigid unmanageable data structure just because my database/service returns DTOs in this format. As mentioned earlier, generic representation enables generic algorithms which are used a LOT (meaning the effort pays off).
  3. I didn't have much time to test the solution (in fact, I still don't have access to my compiler), so burden of testing is on you.

Update 1

I updated code as per t3chb0t's comment and own observations:

  1. Added IEnumerable<IProjectTree<T>> to IProjectTree<T>.
  2. Replaced .Count() > 0 with .Any() where applicable.
  3. Using extension method subTree.ShakeOff(activityPredicate) instead of an explicit static method invocation.
public interface IProjectTree<T>
{
    IEnumerable<IProjectTree<T>> Children { get; set; }
    IEnumerable<ActivityDto> Activities { get; set; }

    T SubtreeSpecificData { get; set; }
}
public static class Traverse
{
    public static IProjectTree<T> ShakeOff<T>(this IProjectTree<T> targetTree, Func<ActivityDto, bool> activityPredicate) {
        if (targetTree.Children == null)
        {
            targetTree.Children = new List<IProjectTree<T>>();
        }
        else
        {
            targetTree.Children = targetTree
                .Children
                .Select(subTree => Traverse.ShakeOff(subTree, activityPredicate))
                .ToList();
        }

        targetTree.Children = targetTree.Children.Where(subtree => subtree.ActivityPredicateApplies(activityPredicate));

        return targetTree;
    }

    public static bool ActivityPredicateApplies<T>(this IProjectTree<T> target, Func<ActivityDto, bool> activityPredicate) {
        target.Activities = target.Activities.Where(activityPredicate);

        var hasSubtreesAndUnfilteredActivities = target.Children.Count() > 0 && target.Activities.Count() > 0;
        return hasSubtreesAndUnfilteredActivities;
    }
}
  1. Traverse.ShakeOff() method is not a pure function, it modifies the provided targetTree object in-place. This may be dangerous in some scenarios (but probably, not in your case).
  2. The code above may or may not be easy to apply due to architectural changes -- it's your judgement call. If I were you, I would not limit myself to a rigid unmanageable data structure just because my database/service returns DTOs in this format. As mentioned earlier, generic representation enables generic algorithms which are used a LOT (meaning the effort pays off).
  3. I didn't have much time to test the solution (in fact, I still don't have access to my compiler), so burden of testing is on you.
public interface IProjectTree<T>
    : IEnumerable<IProjectTree<T>> // !!! The implementation should rely on Children field
{
    IEnumerable<IProjectTree<T>> Children { get; set; }
    IEnumerable<ActivityDto> Activities { get; set; }

    T SubtreeSpecificData { get; set; }
}
public static class Traverse
{
    public static IProjectTree<T> ShakeOff<T>(this IProjectTree<T> targetTree, Func<ActivityDto, bool> activityPredicate)
    {
        if (!targetTree.Any())
        {
            targetTree.Children = new List<IProjectTree<T>>();
        }
        else
        {
            targetTree.Children = targetTree
                .Children
                .Select(subTree => subTree.ShakeOff(activityPredicate))
                .ToList();
        }

        targetTree.Children = targetTree.Children.Where(subtree => subtree.ActivityPredicateApplies(activityPredicate));

        return targetTree;
    }

    public static bool ActivityPredicateApplies<T>(this IProjectTree<T> target, Func<ActivityDto, bool> activityPredicate)
    {
        target.Activities = target.Activities.Where(activityPredicate);

        var hasSubtreesAndUnfilteredActivities = target.Any() && target.Activities.Any();
        return hasSubtreesAndUnfilteredActivities;
    }
}
  1. Traverse.ShakeOff() method is not a pure function, it modifies the provided targetTree object in-place. This may be dangerous in some scenarios (but probably, not in your case).
  2. The code above may or may not be easy to apply due to architectural changes -- it's your judgement call. If I were you, I would not limit myself to a rigid unmanageable data structure just because my database/service returns DTOs in this format. As mentioned earlier, generic representation enables generic algorithms which are used a LOT (meaning the effort pays off).
  3. I didn't have much time to test the solution (in fact, I still don't have access to my compiler), so burden of testing is on you.

Update 1

I updated code as per t3chb0t's comment and own observations:

  1. Added IEnumerable<IProjectTree<T>> to IProjectTree<T>.
  2. Replaced .Count() > 0 with .Any() where applicable.
  3. Using extension method subTree.ShakeOff(activityPredicate) instead of an explicit static method invocation.
typo
Source Link

I tried to rewrite your original code without much luck. After making a little bit of analysis I came to the idea that in order to have a more generic project shaking algorithm, we need a more generic project representation (data structure). Here's what the code looks after my second attempt.

I tried to rewrite your original code without much luck. After making a little bit of analysis I came to the idea that in order to have more generic project shaking algorithm, we need a more generic project representation (data structure). Here's what the code looks after my second attempt.

I tried to rewrite your original code without much luck. After making a little bit of analysis I came to the idea that in order to have a more generic project shaking algorithm, we need a more generic project representation (data structure). Here's what the code looks after my second attempt.

Source Link

Direction

I tried to rewrite your original code without much luck. After making a little bit of analysis I came to the idea that in order to have more generic project shaking algorithm, we need a more generic project representation (data structure). Here's what the code looks after my second attempt.

Code

Here's the project tree object that has arbitrary number of subproject children defined as the same type as project itself (see Composite Pattern). The type placeholder T is something that will need some time to design properly, and this we can address separately.

Also, see that we've got the Activities enumerable declared on this level. Which means that even the root of the project hierarchy may have some activities. This is good and bad at the same time: it's good because now there's no artificial limitation applied to the root level; but it's bad because it diverges from what you had originally and if the original limitation is a business rule, you will need an external mechanism to maintain the rule.

public interface IProjectTree<T>
{
    IEnumerable<IProjectTree<T>> Children { get; set; }
    IEnumerable<ActivityDto> Activities { get; set; }

    T SubtreeSpecificData { get; set; }
}

Now we can build a Traverse class that implements a generic method ShakeOff which is generalized and can be applied to any part of the tree and with any thinkable predicate on activity object (passed as a parameter).

public static class Traverse
{
    public static IProjectTree<T> ShakeOff<T>(this IProjectTree<T> targetTree, Func<ActivityDto, bool> activityPredicate) {
        if (targetTree.Children == null)
        {
            targetTree.Children = new List<IProjectTree<T>>();
        }
        else
        {
            targetTree.Children = targetTree
                .Children
                .Select(subTree => Traverse.ShakeOff(subTree, activityPredicate))
                .ToList();
        }

        targetTree.Children = targetTree.Children.Where(subtree => subtree.ActivityPredicateApplies(activityPredicate));

        return targetTree;
    }

    public static bool ActivityPredicateApplies<T>(this IProjectTree<T> target, Func<ActivityDto, bool> activityPredicate) {
        target.Activities = target.Activities.Where(activityPredicate);

        var hasSubtreesAndUnfilteredActivities = target.Children.Count() > 0 && target.Activities.Count() > 0;
        return hasSubtreesAndUnfilteredActivities;
    }
}

Consumption

The consumer will then look like this:

DateTime fromEndDate = ...;
DateTime toEndDate = ...;
Func<ActivityDto, bool> activityPredicate = activity =>
     activity.EndDate >= fromEndDate && activity.EndDate < toEndDate;

IProjectTree<...> projectTree = null;
projectTree = projectTree.ShakeOff(activityPredicate);

Side notes

But wait, I can't change my DTO classes into IProjectTree. I am pretty sure your DTO structure is shaped by the Database or the service and you want to keep them as they are. This is the hardest part, and you may not like the answer.

You will need to translate your DTOs into IProjectTree<T>. It means, all your DTOs stay unchanged; add the ProjectTree type; and write a class that translates one thing into another (and vice versa). This will require a bit of work. And you will need to find a proper way to define the <T> (the trivial solution would be an object, the SubtreeSpecificData could then point to the source DTO ;) ).

IMO, this would be a better design. Operating directly on DTOs is counter-intuitive to me. DTOs are not supposed to be used as a part of the domain logic, but rather as a dummy objects in client-server communication processes.

Disclaimer

  1. Traverse.ShakeOff() method is not a pure function, it modifies the provided targetTree object in-place. This may be dangerous in some scenarios (but probably, not in your case).
  2. The code above may or may not be easy to apply due to architectural changes -- it's your judgement call. If I were you, I would not limit myself to a rigid unmanageable data structure just because my database/service returns DTOs in this format. As mentioned earlier, generic representation enables generic algorithms which are used a LOT (meaning the effort pays off).
  3. I didn't have much time to test the solution (in fact, I still don't have access to my compiler), so burden of testing is on you.