WPF MVVM - 存储库模式查询

发布于 2024-09-09 10:04:37 字数 806 浏览 7 评论 0原文

我有一个网络服务,它提供了查询数据的接口。我正在使用 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 技术交流群。

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

发布评论

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

评论(1

枯叶蝶 2024-09-16 10:04:37

为什么不使用 linq 表达式作为参数输入?

例如,

public class MyModel
{
  public int Prop1 {get;set;}
  public string Prop2 {get;set;}
}

public interface IRepository
{
  T Find<T>(Expression<Func<T,object>> expression);
}

public class MyRepository : IRepository
{
  public  T Find<T>(Expression<Func<T,object>> expression) where T : class
  {
    //Implement your caching/ calling your web service here
  }
}

因此您可以像这样调用您的存储库:

MyRepository repository = new MyRepository();
var model = repository.Find<MyModel>(a=> a.Prop1 == 5);

如果您不想允许用户将任何类型放入通用参数中,您可以让您的模型全部继承自基类或实现接口,然后更改 find 方法到:

public  T Find<T>(Expression<Func<T,object>> expression) where T : IMyModelInterface //or whatever base class you want

Why don't you use a linq expression as the parameter input?

e.g.

public class MyModel
{
  public int Prop1 {get;set;}
  public string Prop2 {get;set;}
}

public interface IRepository
{
  T Find<T>(Expression<Func<T,object>> expression);
}

public class MyRepository : IRepository
{
  public  T Find<T>(Expression<Func<T,object>> expression) where T : class
  {
    //Implement your caching/ calling your web service here
  }
}

So you could then call your repository like so:

MyRepository repository = new MyRepository();
var model = repository.Find<MyModel>(a=> a.Prop1 == 5);

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:

public  T Find<T>(Expression<Func<T,object>> expression) where T : IMyModelInterface //or whatever base class you want
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文