c# - 构建通用过滤器参数输入接口(interface)?

标签 c# .net generics interface

我有接受不同输入的方法调用,即:

public Authors GetAuthors(string name, string sortBy, string sortDir, int startRow, int numRow)
{
    // Get authors based on filters
}

public Books GetBooks(string id, string year, string sortBy, string sorDir, int startRow, int numRow)
{
    // Get books based on filters
}

我打算更改它以便过滤器是对象,即:

public Authors GetAuthors(GetAuthorsFilters filters)
{
    // Get authors based on filters
}

public Books GetBooks(GetBooksFilters filters)
{
    // Get books based on filters
}

但许多过滤器在这些方法中是通用的,我想为此构建一个通用接口(interface)(即 IFilter),它可以采用不同的过滤器对象,但不确定从哪里开始。有什么建议或建议吗?

谢谢。

最佳答案

在我看来,我会使用抽象类来完成您正在寻找的内容。您可以为每种搜索类型创建一个接口(interface),但随后您必须每次都实现该接口(interface),而且 BookFilters 和 AuthorFilters 中的共享属性之间似乎没有编程差异。也许是这样的:

public abstract class BaseFilter
{
    public string SortBy { get; set; }
    public bool SortAscending { get; set; }
    public int RowStart { get; set; }
    public int RowCount { get; set; }
}

public class BookFilter : BaseFilter
{
    public string ISBN { get; set; }
    public int Year { get; set; }
}

public class AuthorFilter : BaseFilter
{
    public string Name { get; set; }
}

关于c# - 构建通用过滤器参数输入接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7365377/

相关文章:

C# : how to schedule a certain limit of restarts for my PC

c# - 在 .NET 4.5 中编译的 LINQ 查询

java - 实例化泛型类型 ArrayList<T>

c# - 获取作为基类传递给泛型方法的派生 C# 类的属性

C# 字符串文件路径问题

c# - 在 C# 中将 JS 'escape' d 字符串转换回正确的格式

c# - 如何在 C# 中以编程方式启动 apache 和 mysql 服务

c# - Environment.FailFast() 创建的应用程序转储位于磁盘的什么位置?

c# - ASP.Net Web 应用程序中的随机高 JIT 时间

c# - Java 的 Class<X> 类型在 C# 中的等价物是什么?