For the complete documentation index, see llms.txt. This page is also available as Markdown.

Parameters

Parameters parameter packs, type keys, string keys, chain building and JSON serialization.

Parameters is a lightweight parameter package that can save parameters by string name or Type Source. It is commonly used for command invocations, service contexts, data access options, event parameters, and passing small amounts of context data across layers.

Type Relationship

Type
Description

Parameters

Parameter container, implementing IDictionary<object, object> Source.

Parameters.Json

JSON converter implementation for Parameters.

ParametersUtility

Provides extension methods for adding parameters in a chained manner.

String Keys and Concurrent Initialization

String keys ignore case. null names are converted to empty strings.

Source: framework/Zongsoft.Core/test/Collections/ParametersTest.cs (excerpt; see source for context).

ParametersTest.cs
public async Task GetOrAdd_ConcurrentSameName_CreatesSingleValueAsync()
{
	const int COUNT = 64;
	var parameters = new Parameters();
	var factoryCalls = 0;
	var start = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
	var tasks = new Task<object>[COUNT];

	for(var index = 0; index < tasks.Length; index++)
	{
		tasks[index] = Task.Run(async () =>
		{
			await start.Task;
			return parameters.GetOrAdd("Shared", _ =>
			{
				Interlocked.Increment(ref factoryCalls);
				return new object();
			});
		});
	}

	start.SetResult();
	var results = await Task.WhenAll(tasks);

	Assert.Equal(1, factoryCalls);
	Assert.Single(parameters);
	Assert.Same(results[0], parameters.GetValue("shared"));
	Assert.All(results, result => Assert.Same(results[0], result));
}

TryGetValue<TValue>(string name, out TValue value) attempts to convert the original value to the target type.

Type Key and Value Reuse

When saving parameters by type, they can be read directly through generics. If an exact matching type key is not found when reading, Parameters will look for an assignable type key.

Source: framework/Zongsoft.Core/test/Collections/ParametersTest.cs (excerpt; see source for context).

When setting parameters by type, the value must be assignable to the declared type; if the declared type is a non-null value type, it cannot be set to null.

Chain Building

ParametersUtility allows existing parameter packages to continue adding parameters in a chain.

PostService.OnInsert of Discussions reads the associated object named Thread in options.Parameters, which is used to distinguish topic content posts from ordinary replies. Callers of this branch must explicitly pass in the associated object; the presence of the Post composite property in the mapping itself does not automatically populate Parameters. See post write.

Append can merge entries from another Parameters into the current instance.

JSON Serialization

The following is an excerpt from TestJson in the same test file. Parameters comes from the parameter package constructed in the first half of the method, covering named parameters, type keys, time values, byte arrays and IPerson test models; it is not an independent top-level program. ParameterMarkers in type key tests are also defined in the same file.

Parameters.Json provides a JSON converter for Parameters. String keys are output as attribute names, and type keys are output using the $ prefix plus type alias.

Source: framework/Zongsoft.Core/test/Collections/ParametersTest.cs (excerpt; see source for context).

Complex objects are saved as objects containing $type and $value, and the values are restored by type alias when read.

Last updated