/// <summary> /// Initializes a new instance of the <see cref="EntityDefinition"/> class. /// </summary> /// <param name="definitionProvider"> /// The definition provider. /// </param> /// <param name="entityReference"> /// The entity reference. /// </param> public EntityDefinition([NotNull] IEntityDefinitionProvider definitionProvider, [NotNull] EntityReference entityReference) { if (entityReference == null) { throw new ArgumentNullException(nameof(entityReference)); } this.DefinitionProvider = definitionProvider ?? throw new ArgumentNullException(nameof(definitionProvider)); this.allAttributes = new Lazy <List <EntityAttributeDefinition> >( () => new List <EntityAttributeDefinition>(this.DefinitionProvider.ResolveDefinitions(entityReference.EntityType))); // Do not include mapped attributes. this.returnableAttributes = new Lazy <List <EntityAttributeDefinition> >(this.allAttributes.Value.Where(x => x.IsMetadata == false).ToList); // Do not include related attributes. this.directAttributes = new Lazy <List <EntityAttributeDefinition> >(this.returnableAttributes.Value.Where(x => x.IsDirect).ToList); this.primaryKeyAttributes = new Lazy <List <EntityAttributeDefinition> >(this.directAttributes.Value.Where(x => x.IsPrimaryKey).ToList); this.rowIdentity = new Lazy <EntityAttributeDefinition?>(this.GetRowIdentity); // Do not include identity columns. this.insertableAttributes = new Lazy <List <EntityAttributeDefinition> >( this.directAttributes.Value.Where(definition => definition.IsIdentityColumn == false && definition.IsComputed == false).ToList); // Do not include primary keys. this.updateableAttributes = new Lazy <List <EntityAttributeDefinition> >(this.insertableAttributes.Value.Except(this.primaryKeyAttributes.Value).ToList); this.entityLocation = new Lazy <EntityLocation>(() => definitionProvider.GetEntityLocation(entityReference)); var type = entityReference.EntityType; this.defaultRelations = new Lazy <List <IEntityRelation> >( () => { if (typeof(IEntityAggregate).IsAssignableFrom(type) && type.GetConstructor(Array.Empty <Type>()) != null) { // We have to make an instance to get the relations. // TODO: Use T4 template in conjunction with property declarations to build this without instantiation var entity = Activator.CreateInstance(type); return(((IEntityAggregate)entity).EntityRelations.ToList()); } return(new List <IEntityRelation>()); }); }