/// <summary> /// Creates a new instance of this converter. This converter preserves order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public UriToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToString(), ToUri(), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. This converter preserves order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public CharToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => string.Format(CultureInfo.InvariantCulture, "{0}", v), v => v != null && v.Length >= 1 ? v[0] : (char)0, _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeOffsetToBytesConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => ToBytes(v), v => v == null ? default : FromBytes(v), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public GuidToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.ToString("D"), v => v == null ? Guid.Empty : new Guid(v), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public TimeSpanToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToString(), ToTimeSpan(), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeToTicksConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.Ticks, v => new DateTime(v), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public TimeSpanToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.ToString("c"), v => v == null ? default : TimeSpan.Parse(v, CultureInfo.InvariantCulture), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. This converter does not preserve order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public StringToEnumConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToEnum(), ToString(), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeToBinaryConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.ToBinary(), v => DateTime.FromBinary(v), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. This converter preserves order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public CharToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToString(), ToChar(), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public IPAddressToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToString(), ToIPAddress(), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeOffsetToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz"), v => v == null ? default : DateTimeOffset.Parse(v, CultureInfo.InvariantCulture), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public StringToDateTimeConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToDateTime(), ToString(), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public IPAddressToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( ToString(), ToIPAddress(), mappingHints) { }
/// <summary> /// <para> /// Creates a new instance of this converter. /// </para> /// <para> /// This converter does not preserve order because the ordering of bits in /// the standard binary representation of a GUID does not match the ordering /// in the standard string representation. /// </para> /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public GuidToBytesConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v.ToByteArray(), v => v == null ? Guid.Empty : new Guid(v), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public IPAddressToBytesConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v == null ? default : v.GetAddressBytes(), v => v == null ? default : new IPAddress(v), _defaultHints.With(mappingHints)) { }
/// <summary> /// Initializes a new instance of the <see cref="ValueConverter{TModel,TProvider}" /> class. /// </summary> /// <param name="convertToProviderExpression"> An expression to convert objects when writing data to the store. </param> /// <param name="convertFromProviderExpression"> An expression to convert objects when reading data from the store. </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public ValueConverter( [NotNull] Expression <Func <TModel, TProvider> > convertToProviderExpression, [NotNull] Expression <Func <TProvider, TModel> > convertFromProviderExpression, [CanBeNull] ConverterMappingHints mappingHints = null) : base(convertToProviderExpression, convertFromProviderExpression, mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public BytesToStringConverter( [CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v == null ? null : Convert.ToBase64String(v), v => v == null ? null : Convert.FromBase64String(v), mappingHints) { }
/// <summary> /// <para> /// Creates a new instance of this converter that will convert a <c>false</c> false /// to one value and a <c>true</c> to another. /// </para> /// <para> /// Use <see cref="BoolToZeroOneConverter{TProvider}" /> for converting a <see cref="bool" /> to zero/one. /// </para> /// </summary> /// <param name="falseValue"> The value to convert to for <c>false</c>. </param> /// <param name="trueValue"> The value to convert to for <c>true</c>. </param> /// <param name="fromProvider"> Optional custom translator from store. </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public BoolToTwoValuesConverter( [CanBeNull] TProvider falseValue, [CanBeNull] TProvider trueValue, [CanBeNull] Expression <Func <TProvider, bool> > fromProvider = null, [CanBeNull] ConverterMappingHints mappingHints = null) : base(ToProvider(falseValue, trueValue), fromProvider ?? ToBool(trueValue), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public StringToBoolConverter( [CanBeNull] ConverterMappingHints mappingHints = null) : base( v => Convert.ToBoolean(v), v => Convert.ToString(v), mappingHints) { }
/// <summary> /// Adds hints from the given object to this one. Hints that are already specified are /// not overridden. /// </summary> /// <param name="hints"> The hints to add. </param> /// <returns> The combined hints. </returns> public virtual ConverterMappingHints With([CanBeNull] ConverterMappingHints hints) => hints == null ? this : new ConverterMappingHints( hints.Size ?? Size, hints.Precision ?? Precision, hints.Scale ?? Scale, hints.IsUnicode ?? IsUnicode);
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public NumberToStringConverter( [CanBeNull] ConverterMappingHints mappingHints = null) : base( ToStringExpression(), ToIntegerExpression(), _defaultHints.With(mappingHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeOffsetToBinaryConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base( v => ((v.Ticks / 1000) << 11) | ((long)v.Offset.TotalMinutes & 0x7FF), v => new DateTimeOffset( new DateTime((v >> 11) * 1000), new TimeSpan(0, (int)((v << 53) >> 53), 0)), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="encoding"> The string encoding to use. </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public StringToBytesConverter( [NotNull] Encoding encoding, [CanBeNull] ConverterMappingHints mappingHints = null) : base( v => v == null ? null : encoding.GetBytes(v), v => v == null ? null : encoding.GetString(v), mappingHints) { }
/// <summary> /// Adds hints from the given object to this one. Hints that are already specified are /// not overridden. /// </summary> /// <param name="hints"> The hints to add. </param> /// <returns> The combined hints. </returns> public override ConverterMappingHints With(ConverterMappingHints hints) => hints == null ? this : new RelationalConverterMappingHints( hints.Size ?? Size, hints.Precision ?? Precision, hints.Scale ?? Scale, hints.IsUnicode ?? IsUnicode, (hints as RelationalConverterMappingHints)?.IsFixedLength ?? IsFixedLength, hints.ValueGeneratorFactory ?? ValueGeneratorFactory);
/// <summary> /// Creates a new instance of this converter. A case-insensitive first character test is used /// when converting from the store. /// </summary> /// <param name="falseValue"> The string to use for <c>false</c>. </param> /// <param name="trueValue"> The string to use for <c>true</c>. </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public BoolToStringConverter( [NotNull] string falseValue, [NotNull] string trueValue, [CanBeNull] ConverterMappingHints mappingHints = null) : base( Check.NotEmpty(falseValue, nameof(falseValue)), Check.NotEmpty(trueValue, nameof(trueValue)), FromProvider(trueValue), new ConverterMappingHints(size: Math.Max(falseValue.Length, trueValue.Length)).With(mappingHints)) { }
/// <summary> /// Initializes a new instance of the <see cref="ValueConverter" /> class. /// </summary> /// <param name="convertToProviderExpression"> /// The expression to convert objects when writing data to the store, /// exactly as supplied and may not handle /// nulls, boxing, and non-exact matches of simple types. /// </param> /// <param name="convertFromProviderExpression"> /// The expression to convert objects when reading data from the store, /// exactly as supplied and may not handle /// nulls, boxing, and non-exact matches of simple types. /// </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> protected ValueConverter( [NotNull] LambdaExpression convertToProviderExpression, [NotNull] LambdaExpression convertFromProviderExpression, [CanBeNull] ConverterMappingHints mappingHints = null) { Check.NotNull(convertToProviderExpression, nameof(convertToProviderExpression)); Check.NotNull(convertFromProviderExpression, nameof(convertFromProviderExpression)); ConvertToProviderExpression = convertToProviderExpression; ConvertFromProviderExpression = convertFromProviderExpression; MappingHints = mappingHints; }
/// <summary> /// Creates a new <see cref="ValueConverterInfo" /> instance. /// </summary> /// <param name="modelClrType"> The CLR type used in the EF model. </param> /// <param name="providerClrType"> The CLR type used when reading and writing from the database provider. </param> /// <param name="factory"> A factory to create the converter, if needed. </param> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public ValueConverterInfo( [NotNull] Type modelClrType, [NotNull] Type providerClrType, [NotNull] Func <ValueConverterInfo, ValueConverter> factory, [CanBeNull] ConverterMappingHints mappingHints = null) { _factory = factory; Check.NotNull(modelClrType, nameof(modelClrType)); Check.NotNull(providerClrType, nameof(providerClrType)); Check.NotNull(factory, nameof(factory)); ModelClrType = modelClrType; ProviderClrType = providerClrType; MappingHints = mappingHints; }
/// <summary> /// Creates a new instance of this converter. This converter preserves order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource" /> to create data types with appropriate /// facets for the converted data. /// </param> public BoolToZeroOneConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base(Zero(), One(), null, mappingHints) { }
/// <summary> /// Creates a new instance of this converter. This converter does not preserve order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the <see cref="ITypeMappingSource"/> to create data types with appropriate /// facets for the converted data. /// </param> public EnumToStringConverter([CanBeNull] ConverterMappingHints mappingHints = null) : base(v => v.ToString(), ToEnum(), mappingHints) { }