Skip to content

Collaborative form

Two replicas of one form, there to make a single decision visible: which field type you pick determines what happens when two people edit the same thing at once - one value wins for a map entry, both additions survive in a nested set, and both edits survive in a nested text.

Two replicas of one form

Three map entries, one nested set and one nested text, all inside one BcrdtMap called form. Edit a field in either replica and watch the other follow. Under each field, a note says how it merges.

Opening two replicas…

The thing worth trying. Take Replica B offline. In A, set the title to one thing, tick urgent and add a sentence to the notes. In B, set the title to something else, tick blocked and add a different sentence to the notes. Bring B back online. One of the two titles wins outright and the other is lost - while both labels stay ticked and both sentences appear in the notes. Same two clients, same moment, same conflict; different outcome purely because of how the field was modelled.

Map entries, a nested set and a nested text

Most collaborative features are not documents. They are forms, settings panels and entity editors whose shape is already a C# type. This page is two replicas of one such form, and it exists to make a single decision visible: which field type you pick determines what happens when two people edit the same thing at once.

The three fields at the top are plain map entries. Different fields never conflict - one person can change the title while another changes the owner and both survive, which is already better than the usual save-the-whole-record approach. But two people editing the same map field concurrently resolve last-writer-wins: one value survives, deterministically but arbitrarily, and the other is gone.

The labels are a nested BcrdtSet: a member is a value, present or not. Labels ticked by two people at once are both kept, the same label ticked by both is kept once, and a label one person unticks while the other unticks and ticks it again stays ticked - an add wins over a concurrent remove. (A tick of a label that is already ticked writes nothing, so it cannot outvote a removal; the conflict lab runs both cases.)

The notes field at the bottom is a nested BcrdtText instead. Concurrent edits to it merge character by character, exactly as on the text page.

BcrdtMap form = document.GetMap("form");

form.Set("title", "...");                    // a map entry: one side wins
form.EnsureSet("labels").Add("urgent");      // a nested set: both additions stay
form.EnsureText("notes").Insert(0, "...");   // a nested text: both edits merge

EnsureSet and EnsureText create the nested type the first time and return it after that. Two replicas creating it at the same moment on an empty document race like any map entry - one of the two nested types wins, with whatever was written into the other - so a real application creates a document's structure once, when the document is first made.

A form that is already a C# class

When the form's shape is a type you already have, keep the type as the model. BcrdtObjectBinding<T> binds the properties of a plain object to the keys of a BcrdtMap, raises INotifyPropertyChanged when a collaborator changes one, and on save writes back only the properties you actually changed - so saving never reverts a field somebody else edited meanwhile.

using var binding = new BcrdtObjectBinding<Ticket>(document.GetMap("form"));

Ticket ticket = binding.ToObject();        // reads the form
ticket.Owner = "Ada";
binding.FromObject(ticket);                // writes only Owner

binding.Set(t => t.Status, "Approved");  // or one property at a time

For a value stored whole, map.SetObject("assignee", person) flattens an object onto the values a document can hold, and map.Get<Person>("assignee") reads it back - one value, last writer wins.

An unhandled error has occurred. Reload 🗙