using System; using System.Collections.Generic; namespace ln.http.rest { public abstract partial class CRUDObjectContainer { class RegisteredType { public CRUDObjectContainer ObjectContainer { get; } public Type ObjectType { get; } Func _idGetter; Dictionary objectCache = new Dictionary(); public IEnumerable FieldDescriptors => fieldDescriptors; List fieldDescriptors = new List(); public RegisteredType(CRUDObjectContainer objectContainer,Type objectType,Func idGetter) { ObjectContainer = objectContainer; ObjectType = objectType; _idGetter = idGetter; } public bool TryGetObjectID(object objectInstance,out object objectIdentifier) { try { objectIdentifier = _idGetter(objectInstance); } catch (Exception e) { objectIdentifier = null; return false; } return true; } public void AddCachedObject(object objectInstance) { Type objectType = objectInstance.GetType(); if (!ObjectType.Equals(objectType)) throw new ArgumentException(); if (!TryGetObjectID(objectInstance, out object objectIdentifier)) throw new ArgumentOutOfRangeException(); objectCache.Add(objectIdentifier, objectInstance); } public void RemoveCachedObject(object objectInstance) { Type objectType = objectInstance.GetType(); if (!ObjectType.Equals(objectType)) throw new ArgumentException(); if (!TryGetObjectID(objectInstance, out object objectIdentifier)) throw new ArgumentOutOfRangeException(); objectCache.Remove(objectIdentifier); } } } }