2013-03-30 22:56:34 +01:00
|
|
|
/* Copyright (C) 2008 - 2011 Jordan Marr
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
2013-06-03 05:15:56 +02:00
|
|
|
using Marr.Data.Reflection;
|
2013-03-30 22:56:34 +01:00
|
|
|
|
|
|
|
namespace Marr.Data.Mapping
|
|
|
|
{
|
|
|
|
public class Relationship
|
|
|
|
{
|
|
|
|
|
|
|
|
public Relationship(MemberInfo member)
|
|
|
|
: this(member, new RelationshipInfo())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
public Relationship(MemberInfo member, IRelationshipInfo relationshipInfo)
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
Member = member;
|
2013-03-30 22:56:34 +01:00
|
|
|
|
2013-06-03 05:15:56 +02:00
|
|
|
MemberType = ReflectionHelper.GetMemberType(member);
|
2013-03-30 22:56:34 +01:00
|
|
|
|
|
|
|
// Try to determine the RelationshipType
|
|
|
|
if (relationshipInfo.RelationType == RelationshipTypes.AutoDetect)
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
if (typeof(System.Collections.ICollection).IsAssignableFrom(MemberType))
|
2013-03-30 22:56:34 +01:00
|
|
|
{
|
|
|
|
relationshipInfo.RelationType = RelationshipTypes.Many;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
relationshipInfo.RelationType = RelationshipTypes.One;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to determine the EntityType
|
|
|
|
if (relationshipInfo.EntityType == null)
|
|
|
|
{
|
|
|
|
if (relationshipInfo.RelationType == RelationshipTypes.Many)
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
if (MemberType.IsGenericType)
|
2013-03-30 22:56:34 +01:00
|
|
|
{
|
|
|
|
// Assume a Collection<T> or List<T> and return T
|
2013-06-03 05:15:56 +02:00
|
|
|
relationshipInfo.EntityType = MemberType.GetGenericArguments()[0];
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException(string.Format(
|
|
|
|
"The DataMapper could not determine the RelationshipAttribute EntityType for {0}.",
|
2013-06-03 05:15:56 +02:00
|
|
|
MemberType.Name));
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
relationshipInfo.EntityType = MemberType;
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 05:15:56 +02:00
|
|
|
RelationshipInfo = relationshipInfo;
|
2013-03-30 22:56:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-03 05:15:56 +02:00
|
|
|
Setter = MapRepository.Instance.ReflectionStrategy.BuildSetter(member.DeclaringType, member.Name);
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
|
2013-06-03 05:15:56 +02:00
|
|
|
public IRelationshipInfo RelationshipInfo { get; private set; }
|
|
|
|
|
|
|
|
public MemberInfo Member { get; private set; }
|
|
|
|
|
|
|
|
public Type MemberType { get; private set; }
|
|
|
|
|
2013-03-30 22:56:34 +01:00
|
|
|
public bool IsLazyLoaded
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-06-03 05:15:56 +02:00
|
|
|
return LazyLoaded != null;
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 05:15:56 +02:00
|
|
|
public ILazyLoaded LazyLoaded { get; set; }
|
|
|
|
|
|
|
|
public GetterDelegate Getter { get; set; }
|
|
|
|
public SetterDelegate Setter { get; set; }
|
2013-03-30 22:56:34 +01:00
|
|
|
}
|
|
|
|
}
|