MeXperience – Step 3 – Architecture, implementing pipes and filters

In step 2 I explained about the Architecture of MeXperience I had in mind. This article explains the implementation of the pipes and filters pattern to filter the list of experience objects. I will start to tell that my implementation is based on an article from Oren Eini. The Filter In MeXperience there are currently only two types of filters: by tag and by role. But I could think about others like a filter by year of experience. The idea of the filter in the pipes and filters patterns is to have a simple operation, and a lot of combine simple operation in one pipeline make a complex operation. My filters are also used as objects to represent an item in the TagCloud. This is my base.
public abstract class CloudItem
{
    public CloudItem()
    {
        Weight = 1;
    }

    public int Weight { get; set; }
    public string Name { get; set; }

    public abstract IEnumerable<Experience> Filter(IEnumerable<Experience> experiences);
}
Yes I know it’s abstract and there’s no filter implementation. First the signature, there’s an enumeration of experiences coming as input, and there’s an enumeration as output. Let’s see one of the implementations, the of CloudItemTag.
public class CloudItemTag : CloudItem
{
    public override IEnumerable<Experience> Filter(IEnumerable<Experience> experiences)
    {
        foreach (Experience experience in experiences)
        {
                if (experience.Tags.Where(t => t.Name == Name).Count() > 0)
                    yield return experience;
        }
    }
}
The Filter implementation is a simple loop through the experiences, and if the experience corresponds to the filter it will yield return this experience. You can probably figure out how the CloudItemRole would look like. These filters are simple, let’s combine the pipeline of filters. The Pipeline Code explains more than words.
private IEnumerable<Experience> ApplyFilter(IEnumerable<CloudItem> filter)
{
    if (filter == null)
        return _experiences;
    IEnumerable<Experience> current = _experiences;
    foreach (CloudItem filterItem in filter)
    {
        current = filterItem.Filter(current);
    }
    IEnumerator<Experience> enumerator = current.GetEnumerator();
    while (enumerator.MoveNext()) ;

    return current;
}
Alright a little bit of explanation. First we start with the list of filters to apply, if the list is null we will not filter, else we will start with chaining all filters together in line 8. After everything is chained together we see all the magic happen in line 11. Until line 11 no filter has been executed, but by iterating through the experiences filter chain we will get a filtered list of experiences. What’s to come? There will at least be one more article on the MVVM implementation, but this is how it’s going to look like. Or look at this video. image