3

我试图了解Repository在我的应用程序中实现它的模式。我在某种程度上被它困住了。

这是应用程序如何访问数据的简化算法:

  1. 该应用程序第一次没有数据。它需要连接到网络服务来获取这些数据。因此,与 Web 服务交互的所有低级逻辑都将隐藏在WebServiceRepository类后面。从 Web 服务传递到应用程序的所有数据都将被缓存。

  2. 下次当应用程序请求数据时,将在缓存中搜索此数据,然后再从 Web 服务请求它们。缓存将自身表示为一个数据库和 XML 文件,并将通过CacheRepository.

    缓存的数据可以处于三种状态:有效(可以显示给用户)、无效(旧数据无法显示)和部分有效(可以显示但必须尽快更新)。

    a)如果缓存的数据是有效的,那么在我们得到它们之后我们可以停止。

    b) 如果 chached 数据无效或部分有效,我们需要访问WebServiceRepository. 如果对 Web 服务的访问以成功结束,则请求的数据将被缓存,然后将显示给用户(我认为这必须作为对 的第二次调用来实现CacheRepository)。

    c) 所以数据访问的入口点是CacheRepository. 只有在没有完全有效的缓存时才会调用 Web 服务。

我不知道在哪里放置验证缓存的逻辑(有效/无效/部分有效)?在哪里拨打电话WebServiceRepository?我认为这个逻辑不能放在任何一个中Repositories,因为违反了 SOLID 的单一责任原则(SRP)。

我应该实现某种RepositoryService并将所有逻辑放入其中吗?或者也许有办法链接WebServiceRepositoryWebServiceRepository

实现它的模式和方法是什么?

另一个问题是如何从缓存中获取部分有效的数据,然后在一个方法的调用中请求 Web 服务?我认为使用代表和事件。还有其他方法吗?

请给个建议。链接上面列出的所有功能的正确方法是什么?

PS也许我描述的有点混乱。如果需要,我可以提供一些额外的说明。

PPS 下CacheRepository(和下WebServiceRepository)我的意思是一组存储库 - CustomerCacheRepositoryProductCacheRepository等等。感谢@hacktick 的评论。

4

1 回答 1

5

if your webservice gives you crud methods for different entities create a repository for every entityroot. if there are customers create a CustomerRepository. if there are documents with attachments as childs create a DocumentRepository that returns documents with attachments as a property.

a repository is only responsible for a specific type of entity (ie. customers or documents). repositories are not used for "cross cutting concerns" such as caching. (ie. your example of an CacheRepository)

inject (ie. StuctureMap) a IDataCache instance for every repository.

a call to Repository.GetAll() returns all entities for the current repository. every entity is registered in the cache. note the id of that object in the cache.

a call to Repository.FindById() checks the cache first for the id. if the object is valid return it.

notifications about invalidation of an object is routed to the cache. you could implement client-side invalidation or push messages from the server to the client for example via messagequeues.

information about the status whether an object is currently valid or not should not be stored in the entity object itself but rather only in the cache.

于 2012-08-08T11:36:33.370 回答