'Interface Method with different type of arguments

I'm constructing a set of filter-classes which will all have the same method 'Applyfilter'.

How should I define the interface which contains apply filter? The only issue is that apply filter can take a second argument of various types e.g. int, string, Lists. Some pseudo code.

Current Interface method:

Data ApplyFilter(input-data, object value);

Example:

public *data* ApplyFilter(input-data, ***string color***) {
      // Do something with to data with the color string 
}

public *data* ApplyFilter(input-data, ***List<int> size***) {
      // Do something with to data with the size list
}

If I defined the type of argument two as an 'object'. I can do some validation within the ApplyFilter function. As mentioned here: Check if Object is Dictionary or List but is there a better way to do this?



Solution 1:[1]

For centralized code , you can create a filter properties class

public class FilterProperties
{
    public string Color { get; set; }
    public List<int> Sizes { get; set; }
    //add filter properties as you want
}

Then create an ApplyFilter method that takes this class as an argument

public object ApplyFilter(List<object> inputData , FilterProperties filterProperties)
{
    var queryable = inputData as IQueryable<object>;

    // if the color property has value , then filter with it ,else don't filter
    if (!string.IsNullOrEmpty(filterProperties.Color))
    {
        queryable = queryable.Where(//your condition
                                   );
    }

    if (filterProperties.Sizes.Count > 0)
    {
        queryable = queryable.Where(//your condition
                                   );
    }
}

Now you have one filter method to avoid duplicating code, and have the flexibility to add new optional filters easily.

Solution 2:[2]

An approach would be to use Generic

For exemple:

public interface Filter
{
    string ApplyFilter<T>(string inputData, T secondArgument);
}
public class MyImplementationClass : Filter
{

    public string ApplyFilter<T>(string inputData, T secondArgument)
    {
        throw new NotImplementedException();
    }
}
public class UseCase
{
    MyImplementationClass myImplementationClass = new MyImplementationClass();
    void applyFilter()
    {
        string color="";
        myImplementationClass.ApplyFilter<string>("input-data", color);
        List<int> size=new List<int>();
        myImplementationClass.ApplyFilter<List<int>>("input-data", size);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s
Solution 2 Cyril ANDRE