Skip to content

InMemoryRepository does not recognize un-settable properties in GetAll() #114

@punmechanic

Description

@punmechanic

InMemoryRepository will not recognise that some properties cannot be written to and will always treat every property as being serializable. Having a property that should not be serialized (for example, see the following) will cause the InMemoryRepository to throw an exception when it tries to deep-clone the repository upon calls to GetAll()

        .....

        public DateTime UnbanTime { get; set; }
        public bool Expired { get { return DateTime.Now >= UnbanTime; } }

I suggest the use of skipping properties without setters, or allowing users to specify properties as [NonSerializable] so that InMemoryRepository won't attempt to serialize it.

// Inside InMemoryRepositoryBase<T>
        private static IEnumerable<T> CloneDictionary(ConcurrentDictionary<TKey, T> list)
        {
            // when you Google deep copy of generic list every answer uses either the IClonable interface on the T or having the T be Serializable
            //  since we can't really put those constraints on T I'm going to do it via reflection

            var type = typeof (T);
            var properties = type.GetProperties();

            var clonedList = new List<T>(list.Count);

            foreach (var keyValuePair in list)
            {
                var newItem = new T();
                foreach (var propInfo in properties) // <- here is where the issue lies
                {
                    // v - will break if the property does not have a setter
                    propInfo.SetValue(newItem, propInfo.GetValue(keyValuePair.Value, null), null);
                }

                clonedList.Add(newItem);
            }

            return clonedList;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions