具有不同类型参数的接口方法

发布于 2025-01-17 21:11:48 字数 666 浏览 3 评论 0原文

我正在构建一组过滤器类别,这些级别都将具有相同的方法“ applyfilter”。

我应该如何定义包含应用过滤器的接口?唯一的问题是,应用过滤器可以进行第二种类型的参数,例如int,字符串,列表。一些伪代码。

当前接口方法:

Data ApplyFilter(input-data, object value);

示例:

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
}

如果我将参数二的类型定义为“对象”。我可以在应用程序函数中进行一些验证。 如下所述:检查是否是字典或列表更好的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2025-01-24 21:11:48

对于集中式代码,您可以创建一个过滤器属性类

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

,然后创建一个applyfilter方法,该方法将此类作为参数,

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
                                   );
    }
}

现在您有了一种过滤器方法来避免复制代码,并具有灵活性来添加新的可选过滤器容易地。

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.

如何视而不见 2025-01-24 21:11:48

一种方法是使用

为了景象:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文