Skip to content
Äkwav edited this page Aug 17, 2019 · 1 revision

List Attributes of Referenceables always have to have the commands

  • add
  • remove
  • removeAt
  • clear
  • addRange
  • removeRange
  • insert
  • insertRange

Since this is repetative we added the RemoteListCommandsHelper with one method AddCommands<ListContentType>.

Example, taken from Device

RemoteListCommandsHelper.AddCommands<Reference<CoflnetUser>>
	(commandController,
	nameof(Users),
	m=>m.GetTargetAs<Device>().Users,
	m=>new Reference<CoflnetUser>(m.GetAs<SourceReference>()));

This is how all of these commands are registered for the Device.Users attribute. The first argument is the controller to register the commands on.
The second is the name of the Attribute, any plural s will be removed for the commands (it is called AddUsers not AddUsers) The third is how the command gets the list from the MessageData passed to it. The forth is an optional converter in case the MessageData passed doesn't contain the exact type that the list has.

Generating Commands

The commands are now all registered for the list, but how do we send them efficiently? With the RemoteList<T> instead of a normal List<T>. It defines some new Methods for executing the actions remotely. It comes however with the minor drawback of needing to know which field it coresponds to in the parent Referenceable and that parent itself. Wich have to be set in the constructor and in the OnAfterDeserialize callback like this:

public void OnAfterDeserialize()
{
	Users.SetDetails(nameof(Users),this);
}

public Device()
{
	[...]
	Users = new RemoteList<Reference<CoflnetUser>>(nameof(Users),this);
}

After this is done you can use the list almost as normal from the outside

var device = GetDevice();
device.Users.RemoteAdd(user);

As you can see it only differs in using RemoteAdd instead of simple Add. If you don't want to update the resource and just want to read you simply use the normal List<T> functions.

Clone this wiki locally