72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
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<object,object> _idGetter;
|
|
Dictionary<object, object> objectCache = new Dictionary<object, object>();
|
|
|
|
public IEnumerable<FieldDescriptor> FieldDescriptors => fieldDescriptors;
|
|
List<FieldDescriptor> fieldDescriptors = new List<FieldDescriptor>();
|
|
|
|
public RegisteredType(CRUDObjectContainer objectContainer,Type objectType,Func<object,object> 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);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|