ln.objects/ng/mappings/ReferenceMapping.cs

46 lines
1.3 KiB
C#

using System;
using ln.objects.catalog;
namespace ln.types.odb.ng.mappings
{
public class ReferenceMapping<T> : IODBMapping where T:class
{
public Type ReferencedType { get; private set; }
public String ReferencedCollectionName { get; set; }
public ReferenceMapping()
{
ReferencedType = typeof(T);
ReferencedCollectionName = ReferencedType.FullName;
}
public ODBValue MapValue(ODBMapper mapper, object value)
{
if (value == null)
{
return ODBNull.Instance;
}
else
{
ClassMapping classMapping = mapper.GetMapping(ReferencedType) as ClassMapping;
if (classMapping == null)
throw new NullReferenceException(String.Format("classMapping not found for Type {0}",ReferencedType));
object referenceID = classMapping.getID(value);
return mapper.MapValue(referenceID);
}
}
public object UnmapValue(ODBMapper mapper, ODBValue oval)
{
ClassMapping classMapping = mapper.GetMapping(ReferencedType) as ClassMapping;
if (classMapping == null)
throw new NullReferenceException(String.Format("classMapping not found for Type {0}", ReferencedType));
object referenceID = mapper.UnmapValue(classMapping.IDType, oval);
T referenced = mapper.GetCollection<T>(ReferencedCollectionName).Select(referenceID);
return referenced;
}
}
}