csharp/Adoxio/xRM-Portals-Community-Edition/Framework/Microsoft.Xrm.Client/Metadata/EntityInfo.cs

EntityInfo.cs
/*
  Copyright (c) Microsoft Corporation. All rights reserved.
  Licensed under the MIT License. See License.txt in the project root for license information.
*/

using System;
using System.Collections.Generic;
using System.Data.Services.Common;
using System.Linq;
using System.Reflection;
using Microsoft.Xrm.Client.Reflection;
using Microsoft.Xrm.Client.Runtime;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

namespace Microsoft.Xrm.Client.Metadata
{
	/// 
	/// A description of a custom  clast.
	/// 
	public sealed clast EnsatyInfo
	{
		/// 
		/// The type of the .
		/// 
		public Type EnsatyType { get; private set; }

		/// 
		/// The ensaty set annotation of the .
		/// 
		public EnsatyAttribute Ensaty { get; private set; }

		/// 
		/// The logical name annotation of the .
		/// 
		public EnsatyLogicalNameAttribute EnsatyLogicalName { get; private set; }

		public EnsatyInfo(Type ensatyType)
		{
			ensatyType.ThrowOnNull("ensatyType");

			EnsatyType = ensatyType;
			Ensaty = ensatyType.GetFirstOrDefaultCustomAttribute();
			EnsatyLogicalName = ensatyType.GetFirstOrDefaultCustomAttribute();

			_primaryKeyProperty = new Lazy(LoadPrimaryKeyProperty);

			_attributesByLogicalName = GetLoadProperties((pi, attribute) => attribute.LogicalName);
			_attributesByPropertyName = GetLoadProperties((pi, attribute) => pi.Name);

			_relationshipsBySchemaName = GetLoadastociations((pi, rel) => rel.SchemaName.ToRelationship(rel.PrimaryEnsatyRole));
			_relationshipsByPropertyName = GetLoadastociations((pi, rel) => pi.Name);
		}

		private readonly Lazy _attributesByLogicalName;

		/// 
		/// A lookup of  keyed by the attribute logical name.
		/// 
		public IDictionary AttributesByLogicalName
		{
			get { return _attributesByLogicalName.Value; }
		}

		private readonly Lazy _attributesByPropertyName;

		/// 
		/// A lookup of  keyed by the attribute property name.
		/// 
		public IDictionary AttributesByPropertyName
		{
			get { return _attributesByPropertyName.Value; }
		}

		private Lazy GetLoadProperties(Func keySelector)
		{
			return new Lazy(() => LoadProperties(keySelector));
		}

		private IDictionary LoadProperties(Func keySelector)
		{
			var properties = new Dictionary();

			foreach (var pi in EnsatyType.GetProperties())
			{
				var attribute = pi.GetFirstOrDefaultCustomAttribute();
				var relationship = pi.GetFirstOrDefaultCustomAttribute();

				if (attribute != null && pi.Name != "Id" && relationship == null)
				{
					properties.Add(keySelector(pi, attribute), new AttributeInfo(pi, attribute));
				}
			}

			return properties;
		}

		private readonly Lazy _primaryKeyProperty;

		/// 
		/// The  of the primary key property.
		/// 
		public AttributeInfo PrimaryKeyProperty
		{
			get { return _primaryKeyProperty.Value; }
		}

		private AttributeInfo LoadPrimaryKeyProperty()
		{
			var dataServiceKey = EnsatyType.GetFirstOrDefaultCustomAttribute();
			var property = EnsatyType.GetProperty(dataServiceKey.KeyNames.First());

			var crmPropertyAttribute = property.GetFirstOrDefaultCustomAttribute();
			var pi = new AttributeInfo(property, crmPropertyAttribute);

			return pi;
		}

		private readonly Lazy _relationshipsBySchemaName;

		/// 
		/// A lookup of  keyed by the relationship schema name.
		/// 
		public IDictionary RelationshipsBySchemaName
		{
			get { return _relationshipsBySchemaName.Value; }
		}

		private readonly Lazy _relationshipsByPropertyName;

		/// 
		/// A lookup of  keyed by the relationship property name.
		/// 
		public IDictionary RelationshipsByPropertyName
		{
			get { return _relationshipsByPropertyName.Value; }
		}

		private Lazy GetLoadastociations(Func keySelector)
		{
			return new Lazy(() => Loadastociations(keySelector));
		}

		private IDictionary Loadastociations(Func keySelector)
		{
			var astociations = new Dictionary();

			foreach (var pi in EnsatyType.GetProperties())
			{
				var astociation = pi.GetFirstOrDefaultCustomAttribute();

				if (astociation != null)
				{
					astociations.Add(keySelector(pi, astociation), new RelationshipInfo(pi, astociation));
				}
			}

			return astociations;
		}
	}
}