返回通用<对象>;接口方法上的集合,无需在实现中转换为对象对象>
我正在考虑以通用/标准化的方式向我的 WCF Restful Web 服务添加一些基本的搜索和过滤功能。
这个想法是客户端将 POST SearchRequest 到任何容器资源,即 /users 或 /sessions - 然后服务器应该为搜索结果构造一个 uri 并重定向到它们(POST-Redirect-GET 模式)。
我认为我需要这样做(接受建议)的方式是每个可搜索资源都应该实现我定义的接口。然后,该资源可以与我将创建的通用实用程序一起使用,从而只需几行代码即可实现。
我提出的接口是:
public interface ISearchable
{
ChunkedList<object> GetAll(int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValue(string searchValue, int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValueWithFilters(string searchValue, List<string> filters, int chunkStart, int chunkEnd);
}
这个想法是,任何实现此接口的资源都可以进行优化搜索并限制结果集(分块列表具有对象集合和上一个/下一个块 uri)。
我遇到的问题是接口上有一个泛型 ChunkedList
我知道我可以使用 list.convert 手动将每个项目转换为一个对象,但对于每个实现来说都必须这样做会很痛苦。
是否有更合适的接口或 OO 模式可以用于此目的?例如,我可以使用基类实现一些“更干净”的东西,并从中派生出可搜索资源吗?
I'm looking at adding some basic search and filtering functionality in a generic/standardized way to my WCF Restful webservices.
The idea is a client will POST a SearchRequest to any container resource i.e. /users or /sessions - And the server should then construct a uri to the search results and redirect to them (POST-Redirect-GET pattern).
They way I think I need to do this (Open to suggestions) is that each searchable resource should implement an interface I define. That resource can then be used with the generic utilities I'll create for making this only a few lines of code to implement.
The interface I have come up with is:
public interface ISearchable
{
ChunkedList<object> GetAll(int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValue(string searchValue, int chunkStart, int chunkEnd);
ChunkedList<object> SearchByValueWithFilters(string searchValue, List<string> filters, int chunkStart, int chunkEnd);
}
The idea being that any resource that implements this interface can do an optimized search and limit the result set (A chunked list has a collection of objects, and a prev/next chunk uri).
The problem I have is that the interface has a generic on it ChunkedList<object>
but the actual implementations want to return ChunkedList<User>
or ChunkedList<Session>
etc. and this gives me an invalid cast exception.
I know I can use list.convert to manually cast each item to an object, but it would be pain for every implementation to have to do this.
Is there a more appropriate interface or OO pattern to use for this? For example could I achieve something "cleaner" with a base class and derive the searchable resource off of that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)