Exemplo n.º 1
0
        public void RelateEntities <T1, T2>(
            T1 entity1,
            T2 entity2,
            string entity1NavigationPropertyName,
            string entity2NavigationPropertyName,
            RelationshipCardinality cardinality)
        {
            switch (cardinality)
            {
            case RelationshipCardinality.OneToOne:
                this.SetLink(entity1, entity1NavigationPropertyName, entity2);
                this.SetLink(entity2, entity2NavigationPropertyName, entity1);
                break;

            case RelationshipCardinality.OneToMany:
                this.AddLink(entity1, entity1NavigationPropertyName, entity2);
                this.SetLink(entity2, entity2NavigationPropertyName, entity1);
                break;

            case RelationshipCardinality.ManyToOne:
                this.SetLink(entity1, entity1NavigationPropertyName, entity2);
                this.AddLink(entity2, entity2NavigationPropertyName, entity1);
                break;

            case RelationshipCardinality.ManyToMany:
                this.AddLink(entity1, entity1NavigationPropertyName, entity2);
                this.AddLink(entity2, entity2NavigationPropertyName, entity1);
                break;
            }
        }
        // PUBLIC CONSTRUCTORS //////////////////////////////////////////////
        #region Constructors
        public RelationshipInfoBuilder(string rel, Type toClrType, RelationshipCardinality toCardinality)
        {
            Contract.Requires(String.IsNullOrWhiteSpace(rel) == false);
            Contract.Requires(toClrType != null);

            var relationshipInfoFactory = CreateRelationshipInfoFactory(rel, toClrType, toCardinality);

            this.RelationshipInfoFactory = relationshipInfoFactory;
        }
        public IRelationshipInfoBuilder <TResource> ToOneRelationship <TToResource>(string rel)
            where TToResource : class
        {
            Contract.Requires(String.IsNullOrWhiteSpace(rel) == false);

            var toClrType = typeof(TToResource);
            const RelationshipCardinality toCardinality = RelationshipCardinality.ToOne;

            return(this.CreateRelationshipInfoBuilder(rel, toClrType, toCardinality));
        }
Exemplo n.º 4
0
        public ChildRelationshipAttribute(Type childType, RelationshipCardinality cardinality = RelationshipCardinality.OneToMany, string childIdentifyingPropertyName = "", object childIdentifyingPropertyValue = null)
        {
            // childType must implement IBlueSphereEntity
            //Contract.Requires<ArgumentException>(typeof(IBlueSphereEntity).GetTypeInfo().IsAssignableFrom(childType.GetTypeInfo()),
            //                                    "ChildRelationshipAttribute should only apply to types that implement IBluesphereEntity");

            ChildType = childType;
            ChildIdentifyingPropertyName  = childIdentifyingPropertyName;
            ChildIdentifyingPropertyValue = childIdentifyingPropertyValue;
            Cardinality = cardinality;
        }
        // PUBLIC CONSTRUCTORS //////////////////////////////////////////////
        #region Constructors
        public RelationshipInfo(string rel, string apiRelPathSegment, Type toClrType, RelationshipCardinality toCardinality, RelationshipCanonicalRelPathMode toCanonicalRelPathMode)
        {
            Contract.Requires(String.IsNullOrWhiteSpace(rel) == false);
            Contract.Requires(String.IsNullOrWhiteSpace(apiRelPathSegment) == false);
            Contract.Requires(toClrType != null);

            this.Rel = rel;
            this.ApiRelPathSegment      = apiRelPathSegment;
            this.ToClrType              = toClrType;
            this.ToCardinality          = toCardinality;
            this.ToCanonicalRelPathMode = toCanonicalRelPathMode;
        }
        private IRelationshipInfoBuilder <TResource> CreateRelationshipInfoBuilder(string rel, Type toClrType, RelationshipCardinality toCardinality)
        {
            Contract.Requires(String.IsNullOrWhiteSpace(rel) == false);
            Contract.Requires(toClrType != null);

            this.RelationshipInfoBuilderDictionary = this.RelationshipInfoBuilderDictionary ?? new Dictionary <string, RelationshipInfoBuilder <TResource> >();
            this.RelationshipInfoBuilderOrder      = this.RelationshipInfoBuilderOrder ?? new List <string>();

            RelationshipInfoBuilder <TResource> relationshipInfoConfiguration;

            if (this.RelationshipInfoBuilderDictionary.TryGetValue(rel, out relationshipInfoConfiguration))
            {
                return(relationshipInfoConfiguration);
            }

            relationshipInfoConfiguration = new RelationshipInfoBuilder <TResource>(rel, toClrType, toCardinality);
            this.RelationshipInfoBuilderDictionary.Add(rel, relationshipInfoConfiguration);
            this.RelationshipInfoBuilderOrder.Add(rel);

            return(relationshipInfoConfiguration);
        }
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private static Func <RelationshipInfo> CreateRelationshipInfoFactory(string rel, Type toClrType, RelationshipCardinality toCardinality)
        {
            Contract.Requires(String.IsNullOrWhiteSpace(rel) == false);
            Contract.Requires(toClrType != null);

            return(() => new RelationshipInfo(rel, rel, toClrType, toCardinality, default(RelationshipCanonicalRelPathMode)));
        }