如何创建连接字符串管理器来检测服务器是否可用,如果不可用则返回缓存连接字符串
我有一个偶尔连接的应用程序,其中有一个存储有关产品信息的服务器。我使用数据库缓存在本地存储此信息,因此当连接不可用时,应用程序在尝试读取数据库时仍然可以工作。
由于数据库已配置并且我无权修改表,因此我没有实现 2 路更新,它只下载快照。一个附带问题是是否可以创建数据库缓存并仅与客户端计算机上的跟踪列进行双向同步?我无法向服务器添加任何列或表。我知道这可能是一个单独的帖子的问题,但如果这是真的,那么它将完全改变我对这个问题的方向,到一个单独的模块检测和同步数据库并处理抛出的任何同步错误并始终连接到缓存。
我正在使用通用存储库,我想知道处理连接是否可用以及根据此状态使用本地或远程数据库的最佳实践是什么。
我是否应该向通用存储库添加一个接口来处理返回正确的字符串,并让存储库知道它是否处于活动状态?我需要根据连接状态启用/禁用某些功能,因此我还需要在某个地方有一个属性,以便在使用此存储库时可以有一种方法将各种控件启用状态绑定到此状态。
相反,我是否应该有一个包含 IRepository 和 IConnectionStringManager 等的包装器,然后根据可用性处理馈送和初始化存储库连接字符串?该包装器将公开存储库和所需的任何状态属性。
我想我不确定是否应该将我的程序设置为使用 IRepository 以及幕后的所有自动连接感知,或者我是否应该拥有包含 IRepository 和 IConnectionStringManager 的 IRepositoryManager。
也许这两个选项都是错误的?
I have a occasionally connected application where there is a server that stores information about products. I am using a database cache to store this information locally so when a connection is unavailable the application will still work when trying to read the database.
Since the database is configured and I do not have access to modify the tables, I did not implement 2 way updating and it only downloads a snapshot. A side question is if it is possible to create a database cache and have 2-way sync with only tracking columns on the client machine? I cannot add any columns or tables to the server. I know this might be a question for a separate post, but if this is true then it would change my direction for this problem completely, to a separate module detecting and syncing the database and handling any sync errors that are thrown and always connecting to the cache.
I am using a generic repository and I am wondering what the best practice to go about handling if a connection is available or not and using either a local or remote database depending on this status.
Should I add an interface to the generic repository that handles returning the correct string, and lets the repository know if it is live or not? I need to enable/disable certain features depending on the connection state so I also will need a property somewhere so that when this repository is used there can be a way to bind various controls enabled state to this status.
Instead should I have a wrapper that contains for example an IRepository and IConnectionStringManager and then handles feeding and initializing the repository connection string based on availability? This wrapper would expose the repository and any status properties required.
I guess I am not sure if I should be setting up my program to use IRepository with all the automatic connection sensing behind the scenes, or if I should have IRepositoryManager that has a IRepository and IConnectionStringManager in it.
Maybe both of those options are wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢实体框架允许您提供连接字符串作为其上下文的构造函数参数的方式。这样,您就可以在创建上下文时利用依赖项注入框架应用特殊逻辑,并且只需更改一处代码(假设您使用 DI 原则)。您可以对存储库实现执行类似的操作。
更新
由于您使用的是实体框架,因此它在代码中可能看起来像这样:
或者,如果您更喜欢使用更明确的工厂实现:
I like the way Entity Framework allows you to provide a connection string as a constructor argument to its contexts. That way you can leverage a dependency injection framework to apply special logic when creating the context, and you only have to change the code in one place (assuming you're using DI principles). You could do something similar with your repository implementation.
Update
Since you're using Entity Framework, here's an idea of what it might look like in code:
Or, if you prefer to use a more explicit factory implementation: