/// <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(ConverterMappingHints mappingHints = default) : base( v => v.ToString("D"), v => v == null ? Guid.Empty : new Guid(v), mappingHints.With(_defaultHints)) { }
/// <summary> /// Creates a new instance of this converter. This converter preserves order. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public CharToStringConverter(ConverterMappingHints mappingHints = default) : base( v => string.Format(CultureInfo.InvariantCulture, "{0}", v), v => v != null && v.Length >= 1 ? v[0] : (char)0, mappingHints.With(_defaultHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public TimeSpanToStringConverter(ConverterMappingHints mappingHints = default) : base( v => v.ToString("c"), v => v == null ? default : TimeSpan.Parse(v, CultureInfo.InvariantCulture), mappingHints.With(_defaultHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeToTicksConverter(ConverterMappingHints mappingHints = default) : 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 DateTimeOffsetToBytesConverter(ConverterMappingHints mappingHints = default) : base( v => ToBytes(v), v => v == null ? default : FromBytes(v), mappingHints.With(_defaultHints)) { }
/// <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(ConverterMappingHints mappingHints = default) : base( v => v.ToByteArray(), v => v == null ? Guid.Empty : new Guid(v), mappingHints.With(_defaultHints)) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeOffsetToStringConverter(ConverterMappingHints mappingHints = default) : base( v => v.ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz"), v => v == null ? default : DateTimeOffset.Parse(v, CultureInfo.InvariantCulture), mappingHints.With(_defaultHints)) { }
/// <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(ConverterMappingHints mappingHints = default) : base( v => v.ToBinary(), v => DateTime.FromBinary(v), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public NumberToStringConverter( ConverterMappingHints mappingHints = default) : base( ToStringExpression(), ToIntegerExpression(), mappingHints.With(_defaultHints)) { }
/// <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 ConverterMappingHints With(ConverterMappingHints hints) => new ConverterMappingHints( Size ?? hints.Size, Precision ?? hints.Precision, Scale ?? hints.Scale, IsUnicode ?? hints.IsUnicode, IsFixedLength ?? hints.IsFixedLength, SizeFunction ?? hints.SizeFunction);
/// <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 ConverterMappingHints With(ConverterMappingHints hints) => new ConverterMappingHints( CalculateSize(hints), Precision ?? hints.Precision, Scale ?? hints.Scale, IsUnicode ?? hints.IsUnicode, IsFixedLength ?? hints.IsFixedLength, Size != null ? null : (SizeFunction ?? hints.SizeFunction));
/// <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{TStore}" /> 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="fromStore"> Optional custom translator from store. </param> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public BoolToTwoValuesConverter( [CanBeNull] TStore falseValue, [CanBeNull] TStore trueValue, [CanBeNull] Expression <Func <TStore, bool> > fromStore = null, ConverterMappingHints mappingHints = default) : base(ToStore(falseValue, trueValue), fromStore ?? 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 BytesToStringConverter( ConverterMappingHints mappingHints = default) : base( v => v == null ? null : Convert.ToBase64String(v), v => v == null ? null : Convert.FromBase64String(v), mappingHints.With(_defaultHints)) { }
/// <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 type mapper to create data types with appropriate /// facets for the converted data. /// </param> public StringToBytesConverter( [NotNull] Encoding encoding, ConverterMappingHints mappingHints = default) : base( v => v == null ? null : encoding.GetBytes(v), v => v == null ? null : encoding.GetString(v), mappingHints) { }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public DateTimeOffsetToBinaryConverter(ConverterMappingHints mappingHints = default) : 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. 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, ConverterMappingHints mappingHints = default) : base( Check.NotEmpty(falseValue, nameof(falseValue)), Check.NotEmpty(trueValue, nameof(trueValue)), FromStore(trueValue), mappingHints.With(new ConverterMappingHints(size: Math.Max(falseValue.Length, trueValue.Length)))) { }
/// <summary> /// Initializes a new instance of the <see cref="ValueConverter{TModel,TStore}" /> class. /// </summary> /// <param name="convertToStoreExpression"> An expression to convert objects when writing data to the store. </param> /// <param name="convertFromStoreExpression"> An expression to convert objects when reading data from the store. </param> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public ValueConverter( [NotNull] Expression <Func <TModel, TStore> > convertToStoreExpression, [NotNull] Expression <Func <TStore, TModel> > convertFromStoreExpression, ConverterMappingHints mappingHints = default) : base( SanitizeConverter(Check.NotNull(convertToStoreExpression, nameof(convertToStoreExpression))), SanitizeConverter(Check.NotNull(convertFromStoreExpression, nameof(convertFromStoreExpression))), convertToStoreExpression, convertFromStoreExpression, 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, ConverterMappingHints mappingHints = default) { _factory = factory; Check.NotNull(modelClrType, nameof(modelClrType)); Check.NotNull(providerClrType, nameof(providerClrType)); Check.NotNull(factory, nameof(factory)); ModelClrType = modelClrType; ProviderClrType = providerClrType; MappingHints = mappingHints; }
private int?CalculateSize(ConverterMappingHints hints) { var size = Size ?? hints.Size; if (size != null) { var sizeFunc = SizeFunction ?? hints.SizeFunction; if (sizeFunc != null) { return(sizeFunc(size.Value)); } } return(size); }
/// <summary> /// Initializes a new instance of the <see cref="ValueConverter" /> class. /// </summary> /// <param name="convertToStore"> /// The function to convert objects when writing data to the store, /// setup to handle nulls, boxing, and non-exact matches of simple types. /// </param> /// <param name="convertFromStore"> /// The function to convert objects when reading data from the store, /// setup to handle nulls, boxing, and non-exact matches of simple types. /// </param> /// <param name="convertToStoreExpression"> /// 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="convertFromStoreExpression"> /// 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 type mapper to create data types with appropriate /// facets for the converted data. /// </param> protected ValueConverter( [NotNull] Func <object, object> convertToStore, [NotNull] Func <object, object> convertFromStore, [NotNull] LambdaExpression convertToStoreExpression, [NotNull] LambdaExpression convertFromStoreExpression, ConverterMappingHints mappingHints = default) { Check.NotNull(convertToStore, nameof(convertToStore)); Check.NotNull(convertFromStore, nameof(convertFromStore)); Check.NotNull(convertToStoreExpression, nameof(convertToStoreExpression)); Check.NotNull(convertFromStoreExpression, nameof(convertFromStoreExpression)); ConvertToStore = convertToStore; ConvertFromStore = convertFromStore; ConvertToStoreExpression = convertToStoreExpression; ConvertFromStoreExpression = convertFromStoreExpression; MappingHints = mappingHints; }
/// <summary> /// Creates a new instance of this converter. /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public TimeSpanToTicksConverter(ConverterMappingHints mappingHints = default) : base(v => v.Ticks, v => new TimeSpan(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 BoolToZeroOneConverter(ConverterMappingHints mappingHints = default) : base(Zero(), One(), null, mappingHints) { }
/// <summary> /// <para> /// Creates a new instance of this converter. /// </para> /// <para> /// This converter supports <see cref="double" />, <see cref="float" />, <see cref="decimal" />, /// <see cref="int" />, <see cref="long" />, <see cref="short" />, <see cref="byte" />, /// <see cref="uint" />, <see cref="ulong" />, <see cref="ushort" />, <see cref="sbyte" />, /// and <see cref="char" />. /// </para> /// </summary> /// <param name="mappingHints"> /// Hints that can be used by the type mapper to create data types with appropriate /// facets for the converted data. /// </param> public NumberToBytesConverter(ConverterMappingHints mappingHints = default) : base(ToBytes(), ToNumber(), mappingHints.With(_defaultHints)) { }
/// <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 type mapper to create data types with appropriate /// facets for the converted data. /// </param> public EnumToStringConverter(ConverterMappingHints mappingHints = default) : base(v => v.ToString(), ToEnum(), mappingHints.With(_defaultHints)) { }