public ExpenseTemplateModel ToModel() => new ExpenseTemplateModel(
     GuidKey.Create(Id, KeyFactory.Empty(typeof(ExpenseTemplate)).Type),
     Amount != null
         ? new Price(Amount.Value, Currency)
         : null,
     Description,
     CategoryId != null
         ? GuidKey.Create(CategoryId.Value, KeyFactory.Empty(typeof(Category)).Type)
         : GuidKey.Empty(KeyFactory.Empty(typeof(Category)).Type)
     );
Example #2
0
        /// <summary>
        /// Creates a new empty key instance.
        /// </summary>
        /// <param name="targetType">A type for which key is generated.</param>
        /// <returns>A new empty key instance.</returns>
        public static IKey Empty(Type targetType)
        {
            Ensure.NotNull(targetType, "targetType");

            if (emptyFactory != null)
            {
                return(emptyFactory(targetType));
            }

            string keyType = (keyTypeProvider ?? TypeNameMapper.Default).Get(targetType);

            return(GuidKey.Empty(keyType));
        }
Example #3
0
        public bool TryGet <T>(string key, out T value)
        {
            if (storage.TryGetValue(key, out object target))
            {
                if (target == null)
                {
                    value = default(T);
                    return(true);
                }

                log.Debug($"Get: Key: '{key}', RequiredType: '{typeof(T).FullName}', ActualType: '{target.GetType().FullName}'.");
                if (target is T targetValue)
                {
                    value = targetValue;
                    return(true);
                }

                if (typeof(T) == typeof(int) && target.GetType() == typeof(long))
                {
                    value = (T)(object)(int)(long)target;
                    return(true);
                }

                if (typeof(T) == typeof(decimal) && target.GetType() == typeof(double))
                {
                    value = (T)(object)(decimal)(double)target;
                    return(true);
                }

                if (typeof(T) == typeof(IKey) && target is JsonObject json)
                {
                    string type = (string)json["Type"];
                    if (json.TryGetValue("Guid", out object rawGuid))
                    {
                        if (rawGuid == null)
                        {
                            value = (T)(object)GuidKey.Empty(type);
                        }
                        else
                        {
                            value = (T)(object)GuidKey.Create(Guid.Parse((string)rawGuid), type);
                        }

                        return(true);
                    }
                    else if (json.TryGetValue("Identifier", out object rawIdentifier))
                    {
                        if (rawIdentifier == null)
                        {
                            value = (T)(object)StringKey.Empty(type);
                        }
                        else
                        {
                            value = (T)(object)StringKey.Create((string)rawIdentifier, type);
                        }

                        return(true);
                    }
                }

                if (typeof(T) == typeof(Color) && target is string rawColor)
                {
                    byte[] parts = rawColor.Split(new char[] { ';' }).Select(p => Byte.Parse(p)).ToArray();
                    value = (T)(object)Color.FromArgb(parts[0], parts[1], parts[2], parts[3]);
                    log.Debug($"Get: Color: '{value}'.");
                    return(true);
                }

                if (typeof(T) == typeof(Price) && target is JsonObject priceJson)
                {
                    log.Debug($"Get: Price value type: '{priceJson["Value"].GetType().FullName}'.");
                    decimal priceValue    = (decimal)(double)priceJson["Value"];
                    string  priceCurrency = (string)priceJson["Currency"];
                    value = (T)(object)new Price(priceValue, priceCurrency);
                    return(true);
                }

                if (typeof(T) == typeof(DateTime) && target is string rawDateTime)
                {
                    if (DateTime.TryParse(rawDateTime, out DateTime dateTime))
                    {
                        value = (T)(object)dateTime;
                        return(true);
                    }
                    else
                    {
                        log.Warning($"Get: Key: '{key}' not parseable to datetime from value '{rawDateTime}'.");
                        value = default(T);
                        return(false);
                    }
                }
            }

            log.Debug($"Get: Key: '{key}' NOT FOUND.");
            value = default(T);
            return(false);
        }
        public bool TryGet <T>(string key, out T value)
        {
            if (storage.TryGetValue(key, out object target))
            {
                if (target == null)
                {
                    value = default(T);
                    return(true);
                }

                log.Debug($"Get: Key: '{key}', RequiredType: '{typeof(T).FullName}', ActualType: '{target.GetType().FullName}'.");

                if (target is T targetValue)
                {
                    value = targetValue;
                    return(true);
                }

                JsonElement element = (JsonElement)target;

                if (typeof(T) == typeof(string))
                {
                    value = (T)(object)element.GetString();
                    return(true);
                }

                if (typeof(T) == typeof(int))
                {
                    value = (T)(object)element.GetInt32();
                    return(true);
                }

                if (typeof(T) == typeof(long))
                {
                    value = (T)(object)element.GetInt64();
                    return(true);
                }

                if (typeof(T) == typeof(decimal))
                {
                    value = (T)(object)element.GetDecimal();
                    return(true);
                }

                if (typeof(T) == typeof(double))
                {
                    value = (T)(object)element.GetDouble();
                    return(true);
                }

                if (typeof(T) == typeof(bool))
                {
                    value = (T)(object)element.GetBoolean();
                    return(true);
                }

                if (typeof(T) == typeof(IKey))
                {
                    if (element.ValueKind == JsonValueKind.Null)
                    {
                        value = default(T);
                        return(true);
                    }

                    string type = element.GetProperty("Type").GetString();
                    if (element.TryGetProperty("Guid", out JsonElement rawGuid))
                    {
                        string rawGuidValue = rawGuid.GetString();
                        if (rawGuidValue == null)
                        {
                            value = (T)(object)GuidKey.Empty(type);
                        }
                        else
                        {
                            value = (T)(object)GuidKey.Create(Guid.Parse(rawGuidValue), type);
                        }

                        return(true);
                    }
                    else if (element.TryGetProperty("Identifier", out JsonElement rawIdentifier))
                    {
                        string rawIdentifierValue = rawIdentifier.GetString();
                        if (rawIdentifierValue == null)
                        {
                            value = (T)(object)StringKey.Empty(type);
                        }
                        else
                        {
                            value = (T)(object)StringKey.Create(rawIdentifierValue, type);
                        }

                        return(true);
                    }
                }

                if (typeof(T) == typeof(Color))
                {
                    byte[] parts = element.GetString().Split(new char[] { ';' }).Select(p => Byte.Parse(p)).ToArray();
                    value = (T)(object)Color.FromArgb(parts[0], parts[1], parts[2], parts[3]);
                    log.Debug($"Get: Color: '{value}'.");
                    return(true);
                }

                if (typeof(T) == typeof(Price))
                {
                    log.Debug($"Get: Price value type: '{element.GetProperty("Value").GetType().FullName}'.");
                    decimal priceValue    = element.GetProperty("Value").GetDecimal();
                    string  priceCurrency = element.GetProperty("Currency").GetString();
                    value = (T)(object)new Price(priceValue, priceCurrency);
                    return(true);
                }

                if (typeof(T) == typeof(DateTime))
                {
                    string rawDateTime = element.GetString();
                    if (DateTime.TryParse(rawDateTime, out DateTime dateTime))
                    {
                        value = (T)(object)dateTime;
                        return(true);
                    }
                    else
                    {
                        log.Warning($"Get: Key: '{key}' not parseable to datetime from value '{rawDateTime}'.");
                        value = default(T);
                        return(false);
                    }
                }
            }

            log.Debug($"Get: Key: '{key}' NOT FOUND. Storage: '{JsonSerializer.Serialize(storage)}'.");
            value = default(T);
            return(false);
        }
Example #5
0
 /// <summary>
 /// Sets key factory to use <see cref="GuidKey"/> and type fullname with assembly name (without version and public key).
 /// </summary>
 public static void SetGuidKeyWithTypeFullNameAndAssembly()
 {
     keyFactory   = targetType => GuidKey.Create(Guid.NewGuid(), targetType.FullName + ", " + targetType.Assembly.GetName().Name);
     emptyFactory = targetType => GuidKey.Empty(targetType.FullName + ", " + targetType.Assembly.GetName().Name);
 }