Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed property bag dictionaries to prevent boxing #1319

Open
GeertvanHorrik opened this issue May 6, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@GeertvanHorrik
Copy link
Member

commented May 6, 2019

At the moment all properties are stored in a single (object) dictionary. We should investigate whether it's better to use multiple (static?) dictionaries per type or a single one with boxing.

@GeertvanHorrik GeertvanHorrik self-assigned this May 6, 2019

@GeertvanHorrik GeertvanHorrik added this to the 5.11.0 milestone May 6, 2019

@GeertvanHorrik

This comment has been minimized.

Copy link
Member Author

commented May 6, 2019

Looks like we need boxing when we want to return from a generic method. Here is the current research project:

using System;
using System.Collections.Generic;

namespace CTL1319
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
    }

    public interface IPropertyBag
    {
        void SetValue<TValue>(string name, TValue value);
        TValue GetValue<TValue>(string name);
    }

    public class NonTypedPropertyBag
    {
        private readonly Dictionary<string, object> _values;

        public void SetValue<TValue>(string name, TValue value)
        {
            _values[name] = value;
        }

        public TValue GetValue<TValue>(string name)
        {
            return (TValue)_values[name];
        }
    }

    public class TypedPropertyBag
    {
        private readonly Dictionary<string, int> _intValues = new Dictionary<string, int>();
        private readonly Dictionary<string, bool> _boolValues = new Dictionary<string, bool>();
        private readonly Dictionary<string, short> _shortValues = new Dictionary<string, short>();
        private readonly Dictionary<string, long> _longValues = new Dictionary<string, long>();
        private readonly Dictionary<string, object> _referenceValues = new Dictionary<string, object>();

        public void SetValue<TValue>(string name, TValue value)
        {
            var targetValue = typeof(TValue);
            if (targetValue == typeof(int))
            {
                SetValue(name, (int)value);
            }
            else if (targetValue == typeof(bool))
            {

            }
            else if (targetValue == typeof(short))
            {

            }
            else if (targetValue == typeof(long))
            {

            }
            else
            {
                SetValue(name, (object)value);
            }
        }

        public void SetValue(string name, int value)
        {
            _intValues[name] = value;
        }

        public void SetValue(string name, bool value)
        {
            _boolValues[name] = value;
        }

        public void SetValue(string name, short value)
        {
            _shortValues[name] = value;
        }

        public void SetValue(string name, long value)
        {
            _longValues[name] = value;
        }

        public void SetValue(string name, object value)
        {
            _referenceValues[name] = value;
        }

        public TValue GetValue<TValue>(string name)
        {
            var targetValue = typeof(TValue);
            if (targetValue == typeof(int))
            {
                return (TValue)GetIntValue(name);
            }
            else if (targetValue == typeof(bool))
            {

            }
            else if (targetValue == typeof(short))
            {

            }
            else if (targetValue == typeof(long))
            {

            }
            else
            {
                return (TValue)GetValue(name);
            }

            return (TValue)_values[name];
        }

        private int GetIntValue(string name)
        {
            return _intValues[name];
        }

        private bool GetBoolValue(string name)
        {
            return _boolValues[name];
        }

        private bool GetShortValue(string name)
        {
            return _boolValues[name];
        }

        private bool GetLongValue(string name)
        {
            return _boolValues[name];
        }
    }

    public class TestType
    {
        private readonly IPropertyBag _propertyBag;

        public TestType(IPropertyBag propertyBag)
        {
            _propertyBag = propertyBag;
        }

        public int IntValue
        {
            get { return _propertyBag.GetValue<int>("int"); }
            set { _propertyBag.SetValue("int", value); }
        }

        public bool BoolValue
        {
            get { return _propertyBag.GetValue<bool>("bool"); }
            set { _propertyBag.SetValue("bool", value); }
        }

        public short ShortValue
        {
            get { return _propertyBag.GetValue<short>("short"); }
            set { _propertyBag.SetValue("short", value); }
        }

        public long LongValue
        {
            get { return _propertyBag.GetValue<long>("long"); }
            set { _propertyBag.SetValue("long", value); }
        }

        public object ReferenceValue
        {
            get { return _propertyBag.GetValue<object>("reference"); }
            set { _propertyBag.SetValue("reference", value); }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.