Hierarchical Object Filter

Asked

Viewed 344 times

1

I have an Asp.net MVC 4.5 C# Razor screen typed by a main class:

public class ClassePaiDTO 
{
    public virtual ICollection<Filho1DTO> Filho1s { get; set; }
    public virtual ICollection<Filho2DTO> Filho2s { get; set; }

    //codigo Filter imaginado
    private FilterSelected()
    {
       this.Filho1s =  this.Filho1s.Where(s => s.Selected).ToList();
       this.Filho2s =  this.Filho2s.Where(s => s.Selected).ToList();
    }
}

public class Filho1DTO 
{
    public bool Selected { get; set; }
    public virtual ICollection<Neto1DTO> Neto1s { get; set; }
}

public class Filho2DTO
{
    public bool Selected { get; set; }
}

public class Neto1DTO 
{
    public bool Selected { get; set; }
}

After running a POST, in my controller, I would like to run a filter to only get what is marked true. Ex:

objClassePaiDTO.FilterSelected();

After that, I would only use an Automapper to transform into an Entity. I tried to use a resolve to effect a filter, but it is not always that I need it, that is, only in the same post of my screen.

How the system is filled with "Parent classes". What clean and semantic implementation is required for this situation?

At first I had thought of creating an abstract class:

  • With a bool property called Selected;
  • With an abstract method called Filter();
  • Every class that needed filtering I would inherit from this class.

But I guess I got a little complicated in design and I couldn’t reach a common denominator.

Then I thought about creating a Reflection for:

When the parent class called the Filterselected() method it would navigate through the property by checking without having lists of children, grandchildren and the like by filtering through everything it encountered.

I confess that this last approach I found a little extreme, but if it works, it would work in a generic way for other classes Father of the system.

  • You could enter the Filterselected code()

  • In fact it does not exist yet, I added in the parent class the imagined. I would like it to cascade.

  • If I understand correctly, do you want each list of children and grandchildren to be filtered when Selected is true? If so, I would create a common interface, and make an Extension method for that type of interface that would sort when it was true, so whenever you want it ordered it is just call this method. You can do with abstract class too. Now if you really want a single list with all the Selected items, then you can do it with recursive method.

  • It would be an objectThat when I called filterSelected, it would come out filtering all the lists and sublists below, leaving it clean.

1 answer

1

I would create an abstract class like this below where Father, Children and grandchildren would inherit:

public abstract class SortBase : ISortBase
{
    #region ISortBase Members

    public IList<ISortBase> ChildList { get; set; }
    public bool Selected { get; set; }

    public virtual void Sort()
    {
        ChildList = ChildList.Where(x => x.Selected).ToList();
        foreach (var sortBase in ChildList)
            sortBase.Sort();

    }

    #endregion
}

Now if your parent/child class and etc have more than one child list that needs to be sorted as well, I would use Reflection to call the Sort method of these collections "Sortbase".

  • Nice, but there are situations where children and grandchildren would have more than one list that would need to be filtered out anyway, I think I’ll go to Reflection.

  • Now that I saw, The nomenclature of my code above should be Filter and not Sort, but the code is the same. I believe you will have to use Reflection even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.