sharp-oodb/mapping/collections/ISetImplementation.cs

151 lines
4.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using oodb.attributes;
using oodb.index;
using System.Linq;
namespace oodb.mapping.collections
{
public class ISetImplementation<T> : ISet<T> where T: Persistent
{
public OODB OODB { get; }
public FieldInfo FieldInfo { get; }
public Persistent Owner { get; }
public FieldInfo ReferencedField { get; }
public Type ReferencedType { get; }
public SearchIndex SearchIndex { get; }
public ISetImplementation(OODB oodb,FieldInfo fieldInfo,Persistent owner)
{
OODB = oodb;
FieldInfo = fieldInfo;
Owner = owner;
ReferencedType = fieldInfo.FieldType.GetGenericArguments()[0];
ReferencedFieldAttribute referencedFieldAttribute = FieldInfo.GetCustomAttribute<ReferencedFieldAttribute>();
string refFieldName = referencedFieldAttribute != null ? referencedFieldAttribute.FieldName : FieldInfo.DeclaringType.Name;
ReferencedField = ReferencedType.GetField(refFieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
SearchIndex = OODB.StorageAdapter.GetSearchIndex(ReferencedField);
}
public IEnumerable<Guid> ElementPersistenceIDs => SearchIndex.QueryTextValue(Owner.PersistenceID.ToString());
public int Count => ElementPersistenceIDs.Count();
public bool IsReadOnly => false;
public bool Add(T item)
{
if (!Contains(item))
{
ReferencedField.SetValue(item, Owner);
OODB.Save(item);
return true;
}
return false;
}
public bool Remove(T item)
{
if (Contains(item))
{
ReferencedField.SetValue(item, null);
OODB.Save(item);
return true;
}
return false;
}
public void Clear()
{
foreach (T item in this)
Remove(item);
}
public bool Contains(T item)
{
Persistent persistentItem = item as Persistent;
foreach (Guid elementID in ElementPersistenceIDs)
if (elementID.Equals(persistentItem.PersistenceID))
return true;
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public void ExceptWith(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
return ElementPersistenceIDs.Select((id) => OODB.Load<T>(id)).GetEnumerator();
}
public void IntersectWith(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool IsProperSubsetOf(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool IsProperSupersetOf(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool IsSubsetOf(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool IsSupersetOf(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool Overlaps(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public bool SetEquals(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public void SymmetricExceptWith(IEnumerable<T> other)
{
throw new NotImplementedException();
}
public void UnionWith(IEnumerable<T> other)
{
throw new NotImplementedException();
}
void ICollection<T>.Add(T item)
{
Add(item);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}