开发者

Pattern or design for sync database with external provider? (best practice)

Your advice is needed! I'm just out for some sort of pseudo-code/idea of what way to go that are robust and reliable. Maybe there exist a usefull pattern for the purpose?

void AddDevice(string itemId);

I have a interface with some methods (above is one). In a new class, that implements the interface, there are an external provider involved which need to be informed of updates in the class.

The class itself is get/sets information to a sql server database. Some (not all) of the information must be pushed to the external provider.

This give me two scenarios (which I ask for help)

  1. WriteOnlytoDatabase = true / false I would like to use same method in both cases, without using a method bool parameter. Is that possible? Could a deleg开发者_如何学运维ate being used to switch between the difference? Please remember it's a interface here (that GUI talks to).

  2. Two transfers, how to track errors Because we do two transfers (database, external provider) there can be error that make one or other unavailable. If error on the external provider, I think of some sort of "undone actions queue" to handle..

Advices are welcome.. This could be solved on a dousin of ways but there are more or less good designs :)

[Reply from Matías below was wroted before my edit of question]


First of all, I would point to the fact that maybe there's some available synchronization mechanism in the market or open source community that can do it for you, outside your code. If this is the case, I'll suggest that I wouldn't implement my own way of synchronize such data. I'd prefer to let such tool do it for me.

Perhaps this isn't your case and your data couldn't be sync'd with an standard or known tool, we need to think about another solution.

I believe some entity isn't responsible of sync'ing with itself in another storage. That should be a task for the layer between business and that storage: the data layer.

Business access to the data without any detail of where to retrieve it. It just get or change business objects' states, or removes them from the store.

It's the business who in some case would require some argument like "ActionKind" - an enumeration - and, since business would rely on some data access layer, some code there would do something depending on the "ActionKind".

This "ActionKind" would let data access layer to choose an implementation of "how and where to store data".

That data access layer would have some "event" or "trigger" that would fire when some change has been made to one of underlying storage devices, and some handler(s) would manage to synchronize data in all other stores.

The "event" or "trigger" handler would be implemented directly in code (hard-coding) or with some interface like "IDataSynchronizer" (choose your own identifier, it's just an example) having a "Synchronize" method, that would be called when some data changed in any storage.

I believe using some approach like this one you'd have less problems with synchronization and you won't need to care about if "1st device has the data, 2nd no, so, I need to check blah blah..."! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜