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()
– PauloHDSousa
In fact it does not exist yet, I added in the parent class the imagined. I would like it to cascade.
– Cleber
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.
– Virok
It would be an objectThat when I called filterSelected, it would come out filtering all the lists and sublists below, leaving it clean.
– Cleber