using System; using System.Reflection; using System.Runtime.Serialization; using Umbraco.Core.Models.Entities; using Umbraco.Core.Persistence.Mappers; namespace Umbraco.Core.Models { /// /// Represents a Relation between two items /// [Serializable] [DataContract(IsReference = true)] public class Relation : EntityBase, IRelation { //NOTE: The datetime column from umbracoRelation is set on CreateDate on the Entity private int _parentId; private int _childId; private IRelationType _relationType; private string _comment; public Relation(int parentId, int childId, IRelationType relationType) { _parentId = parentId; _childId = childId; _relationType = relationType; } private static readonly Lazy Ps = new Lazy(); private class PropertySelectors { public readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo(x => x.ParentId); public readonly PropertyInfo ChildIdSelector = ExpressionHelper.GetPropertyInfo(x => x.ChildId); public readonly PropertyInfo RelationTypeSelector = ExpressionHelper.GetPropertyInfo(x => x.RelationType); public readonly PropertyInfo CommentSelector = ExpressionHelper.GetPropertyInfo(x => x.Comment); } /// /// Gets or sets the Parent Id of the Relation (Source) /// [DataMember] public int ParentId { get { return _parentId; } set { SetPropertyValueAndDetectChanges(value, ref _parentId, Ps.Value.ParentIdSelector); } } /// /// Gets or sets the Child Id of the Relation (Destination) /// [DataMember] public int ChildId { get { return _childId; } set { SetPropertyValueAndDetectChanges(value, ref _childId, Ps.Value.ChildIdSelector); } } /// /// Gets or sets the for the Relation /// [DataMember] public IRelationType RelationType { get { return _relationType; } set { SetPropertyValueAndDetectChanges(value, ref _relationType, Ps.Value.RelationTypeSelector); } } /// /// Gets or sets a comment for the Relation /// [DataMember] public string Comment { get { return _comment; } set { SetPropertyValueAndDetectChanges(value, ref _comment, Ps.Value.CommentSelector); } } /// /// Gets the Id of the that this Relation is based on. /// [IgnoreDataMember] public int RelationTypeId { get { return _relationType.Id; } } } }