internal void AddAssociation(AssociationDefinition associationDefinition) { var association = new Association(this, associationDefinition); // Defensive programming to prevent unexpected behavior caused by "missing" extensions if (_finalizedAggregateFullNameByEntityFullName.IsValueCreated) { throw new InvalidOperationException( $"Association '{association.FullName}' is being added after the domain model has been processed for extension entities."); } if (_associationByFullName.ContainsKey(association.FullName)) { throw new Exception($"Association '{association.FullName}' already exists."); } _associationByFullName.Add(association.FullName, association); _associations.Add(association); var primaryAssociation = new AssociationView(this, association, true); AddAssociationView(primaryAssociation, association.PrimaryEntityFullName); var secondaryAssociation = new AssociationView(this, association, false); AddAssociationView(secondaryAssociation, association.SecondaryEntityFullName); primaryAssociation.Inverse = secondaryAssociation; secondaryAssociation.Inverse = primaryAssociation; }
private static DomainModelBuilder CreateValidDomainModel() { var entityDefinitions = new[] { new EntityDefinition( "DefinedSchema", "Entity1", new[] { new EntityPropertyDefinition("KeyProperty1", new PropertyType(DbType.Int32), null, true) }, new[] { new EntityIdentifierDefinition( "PK", new[] { "KeyProperty1", "KeyProperty2" }, isPrimary: true) }, true) }; var associationDefinitions = new AssociationDefinition[] { }; var aggredateDefinitions = new[] { new AggregateDefinition( new FullName("DefinedSchema", "Entity1"), new FullName[] { }) }; // schema names do not match the names on the AggregateDefinition var schemaDefinition = new SchemaDefinition("logicalName", "DefinedSchema"); var modelDefinitions = new DomainModelDefinitions( schemaDefinition, aggredateDefinitions, entityDefinitions, associationDefinitions); var domainModelDefinitionsProvider = MockRepository.GenerateStub <IDomainModelDefinitionsProvider>(); domainModelDefinitionsProvider.Stub(x => x.GetDomainModelDefinitions()) .Return(modelDefinitions); DomainModelBuilder builder = new DomainModelBuilder(); builder.AddDomainModelDefinitionsList( new List <DomainModelDefinitions> { domainModelDefinitionsProvider.GetDomainModelDefinitions() }); return(builder); }
internal Association(DomainModel domainModel, AssociationDefinition associationDefinition) { if (associationDefinition.PrimaryEntityProperties.Length != associationDefinition.SecondaryEntityProperties.Length) { throw new Exception("The association definition is invalid because a different number of properties were provided for the primary and secondary ends of the association."); } _domainModel = domainModel; FullName = associationDefinition.FullName; var primaryEntityAssociationProperties = associationDefinition.PrimaryEntityProperties .Select(x => new AssociationProperty(x)) .ToArray(); // Identifying associations mean that the properties in the receiving entity must also be identifying if (associationDefinition.IsIdentifying) { // Coerce each secondary entity property definition to be identifying associationDefinition.SecondaryEntityProperties.ForEach(p => p.IsIdentifying = true); } var secondaryEntityAssociationProperties = associationDefinition.SecondaryEntityProperties .Select(x => new AssociationProperty(x)) .ToArray(); Cardinality = associationDefinition.Cardinality; PrimaryEntityFullName = associationDefinition.PrimaryEntityFullName; SecondaryEntityFullName = associationDefinition.SecondaryEntityFullName; _primaryEntityAssociationProperties = new Lazy <AssociationProperty[]>( () => { Entity primaryEntity; if (!_domainModel.EntityByFullName.TryGetValue(PrimaryEntityFullName, out primaryEntity)) { throw new Exception($"The primary entity '{PrimaryEntityFullName}' could not be found."); } primaryEntityAssociationProperties.ForEach(p => p.Entity = primaryEntity); return(primaryEntityAssociationProperties); }); _secondaryEntityAssociationProperties = new Lazy <AssociationProperty[]>( () => { Entity secondaryEntity; if (!_domainModel.EntityByFullName.TryGetValue(SecondaryEntityFullName, out secondaryEntity)) { throw new Exception($"The secondary entity '{SecondaryEntityFullName}' could not be found."); } secondaryEntityAssociationProperties.ForEach(p => p.Entity = secondaryEntity); return(secondaryEntityAssociationProperties); }); IsIdentifying = associationDefinition.IsIdentifying; IsRequired = associationDefinition.IsRequired; IsRequiredCollection = associationDefinition.Cardinality == Cardinality.OneToOneOrMore; ConstraintByDatabaseEngine = associationDefinition.ConstraintNames; }
/// <summary> /// Add an Association that will be built into the domain model /// </summary> /// <param name="associationDefinition"></param> public void AddAssociation(AssociationDefinition associationDefinition) { EnsureDomainModelIsNotNull(); _domainModel.AddAssociation(associationDefinition); }
protected override void Arrange() { _agDefinition = new AggregateDefinition( new FullName("schema", "Aggregate"), new[] { new FullName("schema", "AChild"), new FullName("schema", "SourceEntity"), new FullName("schema", "TargetEntity") }); _entityDefinition1 = new EntityDefinition( "schema", "Aggregate", new EntityPropertyDefinition[0], new EntityIdentifierDefinition[0]); _entityDefinition2 = new EntityDefinition( "schema", "AChild", new EntityPropertyDefinition[0], new EntityIdentifierDefinition[0]); _entityDefinition3 = new EntityDefinition( "schema", "SourceEntity", new [] { CreateInt32Property(isIdentifying: true) }, new [] { new EntityIdentifierDefinition("PK", new [] { "Property1" }, isPrimary: true) }); _entityDefinition4 = new EntityDefinition( "schema", "TargetEntity", new EntityPropertyDefinition[0], new EntityIdentifierDefinition[0]); _schemaDefinition = new SchemaDefinition("Logical Schema", "schema"); _associateDefinition = new AssociationDefinition( new FullName("schema", "association1"), Cardinality.OneToOneOrMore, new FullName("schema", "SourceEntity"), new[] { CreateInt32Property() }, new FullName("schema", "TargetEntity"), new[] { CreateInt32Property() }, false, false); _domainModelDefinitionsProvider = Stub <IDomainModelDefinitionsProvider>(); A.CallTo(() => _domainModelDefinitionsProvider.GetDomainModelDefinitions()) .Returns( new DomainModelDefinitions( _schemaDefinition, new[] { _agDefinition }, new[] { _entityDefinition1, _entityDefinition2, _entityDefinition3, _entityDefinition4 }, new[] { _associateDefinition })); }
private static DomainModelBuilder CreateFaultyDomainModel() { var entityDefinitions = new[] { new EntityDefinition( "DefinedSchema", "RootEntity", new[] { new EntityPropertyDefinition("KeyProperty1", new PropertyType(DbType.Int32), null, true) }, new[] { new EntityIdentifierDefinition( "PK", new[] { "KeyProperty1", "KeyProperty2" }, isPrimary: true) }, true), // The line that follows is causing the failure. The 1st value should be "schema1", not "schema2" new EntityDefinition( "UndefinedSchema", "ChildEntity", new[] { new EntityPropertyDefinition("KeyProperty1", new PropertyType(DbType.Int32), null, true) }, new[] { new EntityIdentifierDefinition( "PK", new[] { "KeyProperty1", "KeyProperty2" }, isPrimary: true) }, true) }; var associationDefinitions = new AssociationDefinition[0]; var aggregateDefinitions = new[] { new AggregateDefinition( new FullName("DefinedSchema", "RootEntity"), new[] { new FullName("UndefinedSchema", "ChildEntity") }) }; var schemaDefinition = new SchemaDefinition("logicalName", "DefinedSchema"); var modelDefinitions = new DomainModelDefinitions( schemaDefinition, aggregateDefinitions, entityDefinitions, associationDefinitions); var domainModelDefinitionsProvider = A.Fake <IDomainModelDefinitionsProvider>(); A.CallTo(() => domainModelDefinitionsProvider.GetDomainModelDefinitions()) .Returns(modelDefinitions); DomainModelBuilder builder = new DomainModelBuilder(); builder.AddDomainModelDefinitionsList( new List <DomainModelDefinitions> { domainModelDefinitionsProvider.GetDomainModelDefinitions() }); return(builder); }
protected override void Arrange() { DomainModelBuilder builder = new DomainModelBuilder(); var edfiSchemaDefinition = new SchemaDefinition( EdFiConventions.LogicalName, EdFiConventions.PhysicalSchemaName); var extensionSchemaDefinition = new SchemaDefinition( "ExtensionLogicalName", "ExtensionPhysicalSchemaName"); builder.AddSchema(edfiSchemaDefinition); builder.AddSchema(extensionSchemaDefinition); builder.AddAggregate( new AggregateDefinition( new FullName(edfiSchemaDefinition.PhysicalName, "EdFiEntity"), new[] { new FullName(extensionSchemaDefinition.PhysicalName, "AggregateExtensionEntity") })); builder.AddEntity( new EntityDefinition( edfiSchemaDefinition.PhysicalName, "EdFiEntity", new[] { new EntityPropertyDefinition( "USI", new PropertyType(DbType.Int32, 0, 10, 0, false), "EdFiEntity Identity Column", true, true) }, new EntityIdentifierDefinition[] { })); builder.AddEntity( new EntityDefinition( extensionSchemaDefinition.PhysicalName, "AggregateExtensionEntity", new EntityPropertyDefinition[] { }, new EntityIdentifierDefinition[] { })); var associationDefinition = new AssociationDefinition( new FullName(extensionSchemaDefinition.PhysicalName, "FK_AggregateExtensionEntity_EdFiEntity_USI"), Cardinality.OneToZeroOrMore, new FullName(edfiSchemaDefinition.PhysicalName, "EdFiEntity"), new[] { new EntityPropertyDefinition( "USI", new PropertyType(DbType.Int32, 0, 10, 0, false), "EdFiEntity Identity Column", true, true) }, new FullName(extensionSchemaDefinition.PhysicalName, "AggregateExtensionEntity"), new[] { new EntityPropertyDefinition( "USI", new PropertyType(DbType.Int32, 0, 10, 0, false), "", true, false) }, true, true); builder.AddAssociation(associationDefinition); var domainModel = builder.Build(); _aggregateExtensionEntity = domainModel.Entities.FirstOrDefault(e => e.Name == "AggregateExtensionEntity"); }