开发者

write xml file - pattern for multiple users needed

开发者 https://www.devze.com 2023-02-15 06:40 出处:网络
My web site writes to XML files. This is an example of the write to: a task xml . /// <summary> /// The name of the xml file containing the tasks data.

My web site writes to XML files.

This is an example of the write to: a task xml .

/// <summary>
/// The name of the xml file containing the tasks data.
/// </summary>
private string tasksFile;

/// <summary>
/// The Xml Document that stores the contents of the tasks
/// xml file in memory once loaded.
/// </summary>
private XDocument tasksXml;

........

/// <summary>
/// Writes the xml task file to the disk
/// </summary>
private void SaveTask()
{
    this.tasksXml.Save(this.tasksFile);
}

The tasksXml XDocument is loaded, then processed and only then saved using the Save method.

As more than one user can try to write to the file at the same time, user A might override changes p开发者_如何学Goerformed by user B.

Edit: Trying to avoid User A opens, user B opens, both edit, then both save their changes (so only one wins)

  • A. How do I ensure that multiple users do not overwrite each other's work?

  • B. Would the term singleton be correct - if so how is it implemented?


Hum,

I cheat, my xml contains a date time, and locked node reference. To avoid deadlocks and overwrites it uses these elements. If it is locked it returns as "read-only". All edits have date time stamps applied. There are numerous flaws not the least of which is the fact that edits are at the record level not down to the field.

Now a bit of explanation as to my application. It is a system for managing office attendance. It monitors all the doors for badge activity, recording the in's and out's. This is a custom solution with low latency and active. So in operation, deadlocks are not in high occurrence. Data integrity beyond 99% is a bonus. So it functions well within these parameters. If one was to induce higher transactional pressures it probably would need a subsystem that would act as the traffic cop.

Anyway this works for me:)


The only option that I have found that is inline with how I think it should be done is explained in the following link on SO. It uses a temporary file to indicate whether the XML file is "locked".


A singleton is for an object you only want to exist once. In this case, you want an object which may only be accessed by one user at a time.

Does B want to be sure it gets the updates A wrote out before it begins processing?

Chances are, you don't need to be reading and writing this document to the filesystem constantly. If you want to make sure the users access it in order, a Queue might be your answer, and you could create a singleton object which maintains a reference to the XML in memory and provides access to it in order (writing it out as needed).

A singleton is an easy pattern; you make the constructor private and create a "getter" that returns a reference to it. There's a good description here: http://www.dofactory.com/Patterns/PatternSingleton.aspx

My suggestion is that you create a class whose responsibility is to control access to the files. You don't make this a singleton, because you need an instance for each file your application edits. It would keep a reference to the file and a Queue of update requests. You then create a singleton "library" which keeps a collection of these document objects. You need that, because without it you could instantiate two instances pointing to the same file. The application interacts only with the library, not with the file access objects. The application tells the library what content it wants to put in what file. The library looks for an instance handling that file, and if it doesn't exist, the library creates it. Then it adds the request into the queue for that object, where it will come up in FIFO order.

0

精彩评论

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