WPF MVVM - 存储库模式查询
我有一个网络服务,它提供了查询数据的接口。我正在使用 MVVM 编写 WPF 应用程序。我正在创建一个存储库,我的视图模型可以使用它来检索模型。需要时,存储库将调用 Web 服务来获取数据。
我需要在我的存储库中使用各种 Find 方法,根据各种条件查找数据,而不仅仅是“Id”等一个条件。
在我的存储库中,我创建了一个 Find 方法,该方法采用规范作为输入,
void IList<MyData> Find (ISpecification spec)
其中基本 ISpecification 接口是
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
}
Find 方法的高级实现如下,
- 首先在缓存中搜索满足规范的候选者
- 如果找到返回候选人列表
- 否则使用规范/标准调用网络服务来获取候选人并返回列表
我对上面的其他场景感到困惑 - 设计规范的正确方法是什么,以便如果我没有数据在满足规范的存储库缓存中,我应该能够从规范中检索条件并调用 Web 服务来传递此条件的 Web 方法?
我想到的一些事情 -
- 如果我专门查找方法来采用具有属性/标准的专门规范,那么存储库 - 规范具有紧密耦合
- 如果我必须直接连接到数据库(我不是),那么我可以支持一种方法例如,返回一个 SQL。 LINQ 可能是一个选项等
I have a web service which provides an interface to query data. I am writing a WPF application using MVVM. I am working on creating a repository that my View Models can use to retrieve models. The repository will call the Web service to fetch data, when required.
I would require various Find methods in my repository that finds the data based on various criteria and not just one criteria like 'Id'.
In my Repository, I have created a Find method that takes a Specification as input
void IList<MyData> Find (ISpecification spec)
where a basic ISpecification interface is
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
}
A high level implemenation of the Find method would be as follows
- first searches the cache for candidates that satisfy specification
- If found return the list of candidates
- Else use the specification / criteria to call web service to fetch the candidates and return the list
I am confused about the Else scenario above - What is the correct way of designing the Specification so that if I have no data in repository cache that satisfies the specification, I should be able to retrieve the criteria from specification and call the web service passing the web method this criteria?
A few things on my mind-
- If I specialize Find methods to take specialized specifications which have properties / criterias then Repository - Specification has tight coupling
- If I would have to connect directly to DB (which I am not) then I could have supported a method that returns an SQL for example. LINQ could have been an option etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不使用 linq 表达式作为参数输入?
例如,
因此您可以像这样调用您的存储库:
如果您不想允许用户将任何类型放入通用参数中,您可以让您的模型全部继承自基类或实现接口,然后更改 find 方法到:
Why don't you use a linq expression as the parameter input?
e.g.
So you could then call your repository like so:
If you want to not allow the user to put any kind of type int the generic argument you could have your models all inherit from a base class or impelement an interface and then change the find method to: