internal void SetFromAndToAssociationEnds(ResourceAssociationEnd fromEnd, ResourceAssociationEnd toEnd) { _fromEnd = fromEnd; _toEnd = toEnd; }
public virtual void InferAssociations() { //Pull off any assoications bound to the properties foreach (ResourceProperty p in this.Properties) { //Skip associations on properties on basetypes, we don't need duplicate associations /*if(this.BaseType!=null) * { * int basePropertyCount = this.BaseType.Properties.OfType<ResourceProperty>().Where(baseProp => baseProp.Name.Equals(p.Name)).Count(); * if (basePropertyCount > 0) * continue; * }*/ ResourceAssociation[] associations = p._other.OfType <ResourceAssociation>().ToArray(); ResourceAssociationEnd[] associationEnds = p._other.OfType <ResourceAssociationEnd>().ToArray(); ResourceAssociationEnd fromEnd = null; ResourceAssociationEnd toEnd = null; if (associationEnds.Count() == 2) { fromEnd = associationEnds[0]; toEnd = associationEnds[1]; } NodeType subtype = p.Type is CollectionType ? ((CollectionType)p.Type).SubType : p.Type; if (associations.Length == 0 && subtype is ResourceType) { //Inferred Association (ie: Navigation property), with no mappings //To specify mappings, explicitly use an association this.Relations.Add( Resource.Association(this.Name + p.Name, //AssociationName Resource.End(this.Name, this, this.Facets.Nullable ? Multiplicity.Zero : Multiplicity.One), Resource.End(p.Name, (ResourceType)subtype, p.Type is CollectionType ? Multiplicity.Many : (p.Facets.Nullable ? Multiplicity.Zero : Multiplicity.One)) ) ); } else { //Updated specfied with Associations, with any missing data foreach (ResourceAssociation a in associations) { //Name (ie: Default) if (a.Name == null) { a.Name = this.Name + p.Name; } //End1 ResourceAssociationEnd e1 = a.Source; e1.ResourceType = e1.ResourceType ?? this; e1.Name = e1.Name ?? this.Name; e1.Multiplicity = e1.Multiplicity ?? (this.Facets.Nullable ? Multiplicity.Zero : Multiplicity.One); //End2 ResourceAssociationEnd e2 = a.Target; e2.ResourceType = e2.ResourceType ?? (ResourceType)subtype; e2.Name = e2.Name ?? p.Name; e2.Multiplicity = e2.Multiplicity ?? (p.Type is CollectionType ? Multiplicity.Many : (p.Facets.Nullable ? Multiplicity.Zero : Multiplicity.One)); this.Relations.Add(a); } } if (fromEnd != null && toEnd != null) { p.SetFromAndToAssociationEnds(fromEnd, toEnd); } } }