public KeyValueProviderWrapper(IReadOnlyKeyValueCollection inner, string prefix, string keyType) { Ensure.NotNull(inner, "inner"); this.inner = inner; this.prefix = prefix; this.keyType = keyType; }
private IEnumerable <XElement> SerializeMetadata(IReadOnlyKeyValueCollection metadata) { return(metadata.Keys.Select(key => new XElement( XName.Get(XmlNameDefinition.MetadataElementName, XmlNameDefinition.XmlnsUri), new XAttribute(XmlNameDefinition.MetadataKeyAttributeName, key), new XAttribute(XmlNameDefinition.MetadataValueAttributeName, metadata.Get <string>(key)) ))); }
public bool TryGetWithoutType <TKey>(IReadOnlyKeyValueCollection parameters, string keyType, out TKey key) where TKey : IKey { Ensure.NotNull(parameters, "parameters"); parameters = new KeyValueProviderWrapper(parameters, null, keyType); return(TryGet(parameters, out key)); }
public FieldDefinition(string identifier, Type fieldType, IKeyValueCollection metadata) { Ensure.NotNull(identifier, "identifier"); Ensure.NotNull(fieldType, "fieldType"); Ensure.NotNull(metadata, "metadata"); Identifier = identifier; FieldType = fieldType; Metadata = metadata; }
/// <summary> /// Loads <paramref name="payload"/> properties from the <paramref name="storage"/>. /// </summary> /// <param name="storage">The storage to load values from.</param> /// <param name="payload">The command payload to load.</param> public void Load(IReadOnlyKeyValueCollection storage, object output) { Command payload = output as Command; if (payload != null) { payload.Key = storage.Get <IKey>(Name.Key); } }
public ModelDefinition(string identifier, IEnumerable <IFieldDefinition> fields, IKeyValueCollection metadata) { Ensure.NotNull(identifier, "identifier"); Ensure.NotNull(fields, "fields"); Ensure.NotNull(metadata, "metadata"); Identifier = identifier; Fields = new List <IFieldDefinition>(fields); Metadata = metadata; }
/// <summary> /// Create a new instance. /// </summary> /// <param name="definition">A model definition of the item.</param> /// <param name="getter">A value getter of the time.</param> /// <param name="metadata">A collection of metadata from an envelope</param> public ObjectVisualization(IModelDefinition definition, IModelValueGetter getter, IReadOnlyKeyValueCollection metadata) { Ensure.NotNull(definition, "definition"); Ensure.NotNull(getter, "getter"); Ensure.NotNull(metadata, "metadata"); Definition = definition; Getter = getter; Metadata = metadata; }
/// <summary> /// Loads <paramref name="payload"/> properties from the <paramref name="storage"/>. /// </summary> /// <param name="storage">The storage to load values from.</param> /// <param name="payload">The event payload to load.</param> public void Load(IReadOnlyKeyValueCollection storage, object output) { Event payload = output as Event; if (payload != null) { payload.Key = storage.Get <IKey>(Name.Key); payload.AggregateKey = storage.Get <IKey>(Name.AggregateKey); payload.Version = storage.Get <int>(Name.AggregateVersion); } }
private static bool TryGetInt64Key(IReadOnlyKeyValueCollection parameters, out Int64Key key) { if (parameters.TryGet("Type", out string type) && parameters.TryGet("ID", out long id)) { key = Int64Key.Create(id, type); return(true); } key = null; return(false); }
private static bool TryGetStringKey(IReadOnlyKeyValueCollection parameters, out StringKey key) { if (parameters.TryGet("Type", out string type) && parameters.TryGet("Identifier", out string identifier)) { key = StringKey.Create(identifier, type); return(true); } key = null; return(false); }
private static bool TryGetGuidKey(IReadOnlyKeyValueCollection parameters, out GuidKey key) { if (parameters.TryGet("Type", out string type) && parameters.TryGet("Guid", out Guid guid)) { key = GuidKey.Create(guid, type); return(true); } key = null; return(false); }
private bool TryGet(IReadOnlyKeyValueCollection parameters, Type keyType, out IKey key) { OutFunc <IReadOnlyKeyValueCollection, IKey, bool> handler; if (!Definitions.parametersToKey.TryGetValue(keyType, out handler)) { throw new MissingParametersToKeyClassMappingException(keyType); } return(handler(parameters, out key)); }
/// <summary> /// Gets a key from the <paramref name="parameters"/> with <paramref name="prefix"/> and <see cref="IKey.Type"/> set to <paramref name="keyType"/> or throws <see cref="MissingKeyInParametersException"/>. /// </summary> /// <param name="converter">A key-parameters converter.</param> /// <param name="parameters">A collection of parameters.</param> /// <param name="prefix">A values prefix for <paramref name="parameters"/>.</param> /// <param name="keyType">A <see cref="IKey.Type"/> for key to create.</param> /// <returns>A key from the <paramref name="parameters"/> or throws <see cref="MissingKeyInParametersException"/>.</returns> public static IKey GetWithoutType(this KeyToParametersConverter converter, IReadOnlyKeyValueCollection parameters, string keyType, string prefix) { Ensure.NotNull(converter, "converter"); IKey key; if (converter.TryGetWithoutType(parameters, keyType, prefix, out key)) { return(key); } throw new MissingKeyInParametersException(); }
public bool TryGet <TKey>(IReadOnlyKeyValueCollection parameters, string prefix, out TKey key) where TKey : IKey { Ensure.NotNull(parameters, "parameters"); if (!string.IsNullOrEmpty(prefix)) { parameters = new KeyValueProviderWrapper(parameters, prefix, null); } return(TryGet(parameters, out key)); }
/// <summary> /// Gets a key from the <paramref name="parameters"/> or throws <see cref="MissingKeyInParametersException"/>. /// </summary> /// <param name="converter">A key-parameters converter.</param> /// <param name="parameters">A collection of parameters.</param> /// <returns>A key from the <paramref name="parameters"/> or throws <see cref="MissingKeyInParametersException"/>.</returns> public static IKey Get(this KeyToParametersConverter converter, IReadOnlyKeyValueCollection parameters) { Ensure.NotNull(converter, "converter"); IKey key; if (converter.TryGet(parameters, out key)) { return(key); } throw new MissingKeyInParametersException(); }
/// <summary> /// Reads the value of <paramref name="key"/> in <paramref name="collection"/>. /// If value is found and can be converted to <typeparamref name="T"/>, returns it. /// Otherwise throws <see cref="InvalidOperationException"/>. /// </summary> /// <param name="collection">Collection of key-value pairs.</param> /// <param name="key">Requested key.</param> /// <param name="defaultValue">Optional default value if is not found or not convertible.</param> /// <returns>Value of <paramref name="key"/> in <paramref name="collection"/>.</returns> public static T Get <T>(this IReadOnlyKeyValueCollection collection, string key) { Ensure.NotNull(collection, "collection"); T value; if (collection.TryGet(key, out value)) { return(value); } throw Ensure.Exception.InvalidOperation("Collection doesn't contain value of type '{0}' with key '{1}'.", typeof(T), key); }
/// <summary> /// Reads the value of <paramref name="key"/> in <paramref name="collection"/>. /// If value is found and can be converted to <typeparamref name="T"/>, returns it. /// Otherwise returns <paramref name="defaultValue"/> (if provided) or throws <see cref="InvalidOperationException"/>. /// </summary> /// <param name="collection">Collection of key-value pairs.</param> /// <param name="key">Requested key.</param> /// <param name="defaultValue">Optional default value if is not found or not convertible.</param> /// <returns>Value of <paramref name="key"/> in <paramref name="collection"/>.</returns> public static T Get <T>(this IReadOnlyKeyValueCollection collection, string key, T defaultValue) { Ensure.NotNull(collection, "collection"); T value; if (collection.TryGet(key, out value)) { return(value); } return(defaultValue); }
public bool TryGet(IReadOnlyKeyValueCollection parameters, out IKey key) { TKey rawKey; if (inner(parameters, out rawKey)) { key = rawKey; return(true); } key = null; return(false); }
/// <summary> /// Gets a key from the <paramref name="parameters"/> with <paramref name="prefix"/> or throws <see cref="MissingKeyInParametersException"/>. /// </summary> /// <typeparam name="TKey">A type of the key.</typeparam> /// <param name="converter">A key-parameters converter.</param> /// <param name="parameters">A collection of parameters.</param> /// <param name="prefix">A values prefix for <paramref name="parameters"/>.</param> /// <returns>A key from the <paramref name="parameters"/> or throws <see cref="MissingKeyInParametersException"/>.</returns> public static TKey Get <TKey>(this KeyToParametersConverter converter, IReadOnlyKeyValueCollection parameters, string prefix) where TKey : IKey { Ensure.NotNull(converter, "converter"); TKey key; if (converter.TryGet(parameters, prefix, out key)) { return(key); } throw new MissingKeyInParametersException(); }
/// <summary> /// Returns <c>true</c> if <paramref name="collection"/> contains <paramref name="key"/>. /// </summary> /// <param name="collection">Collection of key-value pairs.</param> /// <param name="key">Requested key.</param> /// <returns><c>true</c> if <paramref name="collection"/> contains <paramref name="key"/>; otherwise <c>false</c>.</returns> public static bool Has(this IReadOnlyKeyValueCollection collection, string key) { Ensure.NotNull(collection, "collection"); Ensure.NotNull(key, "key"); if (collection.Keys.Contains(key)) { return(true); } object value; return(collection.TryGet(key, out value)); }
public bool TryGetWithoutType(IReadOnlyKeyValueCollection parameters, string keyType, out IKey key) { Ensure.NotNull(parameters, "parameters"); Type type; if (!Definitions.keyTypeToClass.TryGetValue(keyType, out type)) { throw new MissingKeyTypeToKeyClassMappingException(keyType); } parameters = new KeyValueProviderWrapper(parameters, null, keyType); return(TryGet(parameters, type, out key)); }
// These required mapping from IKey.Type to C# class. public bool TryGet(IReadOnlyKeyValueCollection parameters, out IKey key) { Ensure.NotNull(parameters, "parameters"); string keyType = parameters.Get <string>("Type"); Type type; if (!Definitions.keyTypeToClass.TryGetValue(keyType, out type)) { throw new MissingKeyTypeToKeyClassMappingException(keyType); } return(TryGet(parameters, type, out key)); }
public void Load(IReadOnlyKeyValueCollection storage, object output) { // TODO: Not TryGet, UserKey is required! if (output is UserEvent payload) { if (storage.TryGet(Name.UserKey, out IKey userKey)) { payload.UserKey = userKey; } else { payload.UserKey = StringKey.Empty("User"); } } }
public bool TryGet <TKey>(IReadOnlyKeyValueCollection parameters, out TKey key) where TKey : IKey { Ensure.NotNull(parameters, "parameters"); Type keyType = typeof(TKey); IKey rawKey; if (TryGet(parameters, keyType, out rawKey)) { key = (TKey)rawKey; return(true); } key = default(TKey); return(false); }
/// <summary> /// For each found token, tries to read string value from <paramref name="tokenMapper"/> and replaces that token with returned value. /// </summary> /// <param name="tokenMapper">Token name to token value replacer.</param> /// <returns>Formatted string with replaced tokens.</returns> public string Format(IReadOnlyKeyValueCollection tokenMapper) { StringBuilder result = new StringBuilder(); foreach (TokenWriterItem item in items) { if (item.IsToken) { result.Append(tokenMapper.Get(item.Token.Fullname, "")); } else { result.Append(item.Text); } } return(result.ToString()); }
public bool TryGet(IReadOnlyKeyValueCollection parameters, string prefix, out IKey key) { Ensure.NotNull(parameters, "parameters"); if (!string.IsNullOrEmpty(prefix)) { parameters = new KeyValueProviderWrapper(parameters, prefix, null); } string keyType = parameters.Get <string>("Type"); Type type; if (!Definitions.keyTypeToClass.TryGetValue(keyType, out type)) { throw new MissingKeyTypeToKeyClassMappingException(keyType); } return(TryGet(parameters, type, out key)); }
/// <summary> /// Sets 'Metadata' to the metadata of the <paramref name="context"/>. /// </summary> /// <param name="context">The context which metadata to extend.</param> /// <param name="value">The vale of the 'Metadata'.</param> /// <returns><paramref name="envolope"/>(for fluency).</returns> public static ISerializerContext AddEnvelopeMetadata(this DefaultSerializerContext context, IReadOnlyKeyValueCollection value) { Ensure.NotNull(context, "context"); context.Metadata.Add("EnvelopeMetadata", value); return(context); }
/// <summary> /// Tries to read 'Metadata' from the metadata of the <paramref name="context"/>. /// </summary> /// <param name="context">The context to read the metadata from.</param> /// <param name="value">The value of the 'Metadata'.</param> /// <returns><c>true</c> if 'Metadata' can is contained in the <paramref name="context"/>; <c>false</c> otherwise.</returns> public static bool TryGetEnvelopeMetadata(this ISerializerContext context, out IReadOnlyKeyValueCollection value) { Ensure.NotNull(context, "context"); return(context.Metadata.TryGet("EnvelopeMetadata", out value)); }
/// <summary> /// Loads <paramref name="payload"/> properties from the <paramref name="storage"/>. /// </summary> /// <param name="storage">The storage to load values from.</param> /// <param name="payload">The event payload to load.</param> public void Load(IReadOnlyKeyValueCollection storage, Event payload) { payload.Key = storage.Get <IKey>(Name.Key); payload.AggregateKey = storage.Get <IKey>(Name.AggregateKey); payload.Version = storage.Get <int>(Name.AggregateVersion); }
public KeyValueCollection(IReadOnlyKeyValueCollection parentCollection) { Ensure.NotNull(parentCollection, "parentCollection"); this.parentCollection = parentCollection; }