简体   繁体   English

具有多个数据源的应用程序的设计模式?

[英]Design patterns for applications with multiple data sources?

I'm currently working on windows form application that needs to operate both offline and online. 我目前正在处理需要离线和在线操作的Windows窗体应用程序。 The idea is that when the device running the application has an internet connection it will fetch from a web service to get its data, however when there is no connection it fetches from a local data source in the form of JSON in the local file system. 这个想法是,当运行该应用程序的设备具有Internet连接时,它将从Web服务获取以获取其数据,但是当没有连接时,它将从本地数据源中以本地文件系统中JSON的形式获取。

I would preferably like to use the Service and Manager design pattern (3-tier). 我最好使用服务和管理器设计模式(3层)。 However I am unsure which to give the responsibility to of deciding on the data source to use. 但是,我不确定应该由哪个来决定要使用的数据源。 Should the managers have access to two different services and each service looks at a different source and understands how to interact with each, or should the services be aware of how to interact with both data sources? 管理者应该访问两个不同的服务,而每个服务都在一个不同的源上并了解如何与之交互,或者服务应该知道如何与两个数据源进行交互?

I would suggest to use the 3 tier pattern with an IoC container, that way you can inject the right DAL Object as the data source of the app, according to the state of the application - In case it's online, inject an "OnlineDAL" which inherits from IDAL interface, otherwise, inject "OfflineDAL" which also inherits from IDAL interface. 我建议将3层模式与IoC容器一起使用,这样就可以根据应用程序的状态注入正确的DAL对象作为应用程序的数据源-如果在线,请注入“ OnlineDAL”继承自IDAL接口,否则,注入“ OfflineDAL”,其也继承自IDAL接口。

Remember to change the injected class whenever the app goes on/off (with an event listener or something) 请记住,每当应用程序打开/关闭(使用事件侦听器等)时,都要更改注入的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM