简体   繁体   English

如何使用同步实现 C# Winforms 数据库应用程序?

[英]How to implement a C# Winforms database application with synchronisation?

Background背景

I am developing a C# winforms application - currently up to about 11000 LOC and the UI and logic is about 75% done but there is no persistence yet.我正在开发一个 C# winforms 应用程序 - 目前高达大约 11000 LOC,UI 和逻辑完成了大约 75%,但还没有持久性。 There are hundreds of attributes on the forms.表单上有数百个属性。 There are 23 entities/data classes.有 23 个实体/数据类。

Requirement要求

The data needs to be kept in an SQL database.数据需要保存在 SQL 数据库中。 Most of the users operate remotely and we cannot rely on them having a connection so we need a solution that maintains a database locally and keeps it in synch with the central database.大多数用户远程操作,我们不能依赖他们有连接,因此我们需要一个解决方案,在本地维护数据库并使其与中央数据库保持同步。

Edit: Most of the remote users will only require a subset of the database in their local copy.编辑:大多数远程用户只需要本地副本中的数据库子集。 This is because if they don't have access permissions (as defined and stored in my application) to view other user's records, they will not receive copies of them during synchronisation.这是因为如果他们没有访问权限(如在我的应用程序中定义和存储的)来查看其他用户的记录,他们将不会在同步期间收到它们的副本。

How can I implement this?我该如何实施?

Suggested Solution建议的解决方案

I could use the Microsoft Entity Framework to create a database and the link between database and code.我可以使用 Microsoft Entity Framework 创建一个数据库以及数据库和代码之间的链接。 This would save a lot of manual work as there are hundreds of attributes.由于有数百个属性,这将节省大量手动工作。 I am new to this technology but have done a "hello world" project in it.我是这项技术的新手,但已经在其中完成了一个“hello world”项目。

For data synch, each entity would have an integer primary key ID.对于数据同步,每个实体都有一个整数主键 ID。 Additionally it would have a secondary ID column which relates to the central database.此外,它还有一个与中央数据库相关的辅助 ID 列。 This secondary column would contain nulls in the central database but would be populated in the local databases.此辅助列将包含中央数据库中的空值,但会填充到本地数据库中。

For synchronisation, I would write code which copies the records and assigns the IDs accordingly.为了同步,我会编写代码来复制记录并相应地分配 ID。 I would need to handle conflicts.我需要处理冲突。

Can anyone foresee any stumbling blocks to doing this?谁能预见到这样做的任何绊脚石? Would I be better off using one of the recommended solutions for data sychronisation, and if so would these work with the entity framework?我最好使用推荐的数据同步解决方案之一,如果是这样,它们是否可以与实体框架一起使用?

Synching data between relational databases is a pain.在关系数据库之间同步数据是一种痛苦。 Your best course of action is probably dependent on: how many users will there be?您最好的行动方案可能取决于:会有多少用户? How probably are conflicts (ie that the users will work offline on the same data).冲突的可能性有多大(即用户将离线处理相同的数据)。 Also possibly what kind of manpower do you have (do you have proper DBAs/Sql Server devs standing by to assist with the SQL part, or are you just .NET devs).也可能你有什么样的人力(你有合适的 DBA/Sql Server 开发人员随时准备协助 SQL 部分,或者你只是 .NET 开发人员)。

I don't envy you this task, it smells of trouble.我不羡慕你这个任务,它闻起来很麻烦。 I'd especially be worried about data corruption and spreading that corruption to all clients rapidly.我特别担心数据损坏并将这种损坏迅速传播给所有客户。 I'd put extreme countermeasures in place before any data in the remote DB gets updated.我会在远程数据库中的任何数据更新之前采取极端对策。

If you predict a lot of conflicts - the same chunk of data gets modified many times by multiple users - I'd probably at least consider creating an additional 'merge' layer to figure out, what is the correct order of operations to perform on the remote db.如果你预测到很多冲突——同一块数据被多个用户多次修改——我可能至少会考虑创建一个额外的“合并”层来弄清楚,在这些数据上执行的正确操作顺序是什么远程数据库。

One thought - it might be very wrong and crazy, but just the thing that popped in my mind - would be to use JSON Patch on the entities, be it actual domain objects or some configuration containers.一个想法 - 这可能是非常错误和疯狂的,但只是我想到的事情 - 将在实体上使用 JSON Patch,无论是实际的域对象还是某些配置容器。 All the changes the user makes are recorded as JSON Patch statements, then applied to the local db, and when the user is online - submitted - with timestamps!用户所做的所有更改都记录为 JSON Patch 语句,然后应用到本地数据库,并在用户在线时 - 提交 - 带有时间戳! - to merge provider. - 合并提供者。 The JSON Patch statements from different clients could be grouped by the entity id and sorted by timestamp, and user could get feedback on what other operations from different users are queued - and manually make amends to it.来自不同客户端的 JSON Patch 语句可以按实体 id 分组并按时间戳排序,用户可以获得关于来自不同用户的其他操作排队的反馈 - 并手动对其进行修改。 Those grouped statments could be even stored in a files in a git repo.这些分组的语句甚至可以存储在 git repo 的文件中。 Then at some pre-defined intervals, or triggered manually, the update would be performed on a server-side app and saved to the remote db.然后以一些预定义的时间间隔或手动触发,更新将在服务器端应用程序上执行并保存到远程数据库。 After this the users local copies would be refreshed from server.在此之后,用户本地副本将从服务器刷新。

It's just a rough idea, but I think that you need something with similar capability - it doesn't have to be JSON Patch + Git, you can do it in probably hundreds of ways.这只是一个粗略的想法,但我认为您需要具有类似功能的东西 - 它不必是 JSON Patch + Git,您可以通过数百种方式来实现。 I don't thing though, that you will get away with just going through the local/remote db and making updates/merges.不过,我不认为您只需通过本地/远程数据库并进行更新/合并就可以逃脱。 Imagine the scenario, where user updates some data (let's say, 20 fields) offline, another makes completely different updates to 20 fields, and 10 of those are common between the users.想象一下这样的场景,用户离线更新一些数据(比如 20 个字段),另一个对 20 个字段进行完全不同的更新,其中 10 个在用户之间是通用的。 Now, what should the synch process do?现在,同步过程应该做什么? Apply earlier and then latter changes?先应用然后再更改? I'm fairly certain that both users would be furious, because their input was 'atomic' - either everything is changed, or nothing is.我相当肯定这两个用户都会很生气,因为他们的输入是“原子的”——要么一切都改变了,要么什么都没有。 The latter 'commit' must be either rejected, or users should have an option to amend it in respect of the new data.后一个“提交”必须被拒绝,或者用户应该可以选择根据新数据对其进行修改。 That highly depends what your data is, and as I said - what will be number/behaviour of users.这在很大程度上取决于您的数据是什么,正如我所说 - 用户的数量/行为是什么。 Duh, even time-zones become important here - if you have users all in one time-zone you might get away with having predefined times of day when system synchs - but no way you'll convince people with many different business hours that the 'synch session' will happen at eg 11 AM, when they are usually giving presentation to management or sth ;)呃,即使时区在这里也变得很重要——如果你的用户都在一个时区,你可能会在系统同步时预先定义一天中的时间——但是你不可能说服许多不同工作时间的人“同步会话'将发生在例如上午 11 点,当他们通常向管理层或某事进行演示时;)

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

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