/// <summary> /// Returns typed partial data if present. /// </summary> /// <param name="dataSource">Composite metadata source.</param> /// <param name="data">Variable to return metadata.</param> /// <returns> /// <c>true</c> if metadata of the specified type was present, <c>false</c> otherwise. /// </returns> public static bool TryGetMetadata <TMetadata>(this ICompositeMetadata dataSource, out TMetadata data) where TMetadata : IMetadata { RuntimeAssert.ArgumentNotNull(dataSource, nameof(dataSource)); if (dataSource.TryGetMetadata(typeof(TMetadata), out var obj)) { data = (TMetadata)obj; return(true); } data = default(TMetadata); return(false); }
/// <summary> /// Determines whether unicode flag has been set on the target composite data. /// </summary> /// <param name="compositeData">Target composite data.</param> /// <returns> /// <c>true</c> if unicode flag has been set, <c>false</c> otherwise. /// </returns> public static bool?Unicode(this ICompositeMetadata compositeData) => compositeData.TryGetMetadata <Unicode>(out var u) ? (bool?)u.Value : null;
/// <summary> /// Returns either value of single key/value partial data defined by <typeparamref name="T" /> or default /// value. /// </summary> /// <param name="data">Composite data source.</param> /// <param name="defaultValue">Optional default value.</param> /// <returns> /// Either value of single key/value partial data defined by <typeparamref name="T" /> or default value. /// </returns> public static int GetInt32OrDefault <T>(this ICompositeMetadata data, int defaultValue = default(int)) where T : MetadataValue <int> => data.TryGetMetadata <T>(out var value) ? value.Value : defaultValue;
/// <summary> /// Returns either value of single key/value partial data defined by <typeparamref name="T" /> or default /// value. /// </summary> /// <param name="data">Composite data source.</param> /// <param name="defaultValue">Optional default value.</param> /// <returns> /// Either value of single key/value partial data defined by <typeparamref name="T" /> or default value. /// </returns> public static string GetStringOrDefault <T>(this ICompositeMetadata data, string defaultValue = default(string)) where T : MetadataValue <string> => data.TryGetMetadata <T>(out var value) ? value.Value : defaultValue;
/// <summary> /// Returns either value of single key/value partial data defined by <typeparamref name="T" /> or default /// value. /// </summary> /// <param name="data">Composite data source.</param> /// <param name="defaultValue">Optional default value.</param> /// <returns> /// Either value of single key/value partial data defined by <typeparamref name="T" /> or default value. /// </returns> public static bool GetBooleanOrDefault <T>(this ICompositeMetadata data, bool defaultValue = default(bool)) where T : MetadataValue <bool> => data.TryGetMetadata <T>(out var value) ? value.Value : defaultValue;
public static Maybe <IMetadata> TryInitialize(MetadataDescriptor descriptor, IServiceProvider serviceProvider, ICompositeMetadata target) => _cache.GetOrAdd(descriptor.MetadataType, mtype => (BoxedMatcher)Activator.CreateInstance(typeof(Impl <>).MakeGenericType(mtype), true)) .TryInitializeImpl(descriptor, serviceProvider, target);
public abstract Maybe <IMetadata> TryInitializeImpl(MetadataDescriptor descriptor, IServiceProvider serviceProvider, ICompositeMetadata target);
/// <summary> /// Retrieves precision and scale values if set. /// </summary> /// <param name="compositeData">Composite metadata source.</param> /// <returns>Either precision and scale or <c>null</c></returns> public static (byte, byte)? Precision(this ICompositeMetadata compositeData) => compositeData.TryGetMetadata <Precision>(out var precision) ? ((byte, byte)?)(precision.PrecisionValue, precision.Scale) : null;
public override Maybe <IMetadata> TryInitializeImpl(MetadataDescriptor descriptor, IServiceProvider serviceProvider, ICompositeMetadata target) { var desc = (MetadataDescriptor <T>)descriptor; return(desc.Match( onFactory: factory => ((IMetadata)factory(serviceProvider, target)).AsMaybe(), onValue: _ => Maybe.Empty )); }
internal override IMetadata Initialize(IServiceProvider serviceProvider, ICompositeMetadata metadata) => Factory(serviceProvider, metadata);
internal override IMetadata Initialize(IServiceProvider serviceProvider, ICompositeMetadata metadata) => Value;
internal abstract IMetadata Initialize(IServiceProvider serviceProvider, ICompositeMetadata metadata);
/// <summary> /// Determines whether read-only flag has been set on the target composite data. /// </summary> /// <param name="compositeData">Target composite data.</param> /// <returns> /// <c>true</c> if read-only flag has been set, <c>false</c> otherwise. /// </returns> public static bool ReadOnly(this ICompositeMetadata compositeData) => compositeData.TryGetMetadata(out ReadOnlyFlag flag) && flag.Value;
/// <summary> /// Retrieves value of maximum string length constraint if any. /// </summary> /// <param name="compositeData">Composite metadata source.</param> /// <returns>Either maximum string length constraint or <c>null</c></returns> public static int?MaxLength(this ICompositeMetadata compositeData) => compositeData.TryGetMetadata <StringLength>(out var stringLength) ? (int?)stringLength.MaxLength : null;