Exemplo n.º 1
0
        /// <summary>
        /// Defines an object association.
        /// </summary>
        /// <param name="name">Name of forward association to create.</param>
        /// <param name="type">Type of this association.</param>
        /// <param name="info1">Describes object identifier 1 in an association.</param>
        /// <param name="info2">Describes object identifier 2 in an association.</param>
        /// <param name="inverse">(Optional) Name of backward association, if this is a two-way asymmetric one.</param>
        public void DefineAssociation(string name, DataAssociationType type,
            DataAssociationInfo info1, DataAssociationInfo info2, string inverse)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            string info1Alias = info1.Alias;
            string info2Alias = info2.Alias;

            CheckDataName(ref name);
            CheckDataName(ref info1Alias);
            CheckDataName(ref info2Alias);

            var info1Prop = new Dictionary<string, string> {{"alias", info1Alias}};
            AddParameter(info1Prop, "object_type", info1.ObjectType);
            if (info1.Unique) info1Prop.Add("unique", info1.Unique.ToString().ToLowerInvariant());

            var info2Prop = new Dictionary<string, string> {{"alias", info2Alias}};
            AddParameter(info2Prop, "object_type", info2.ObjectType);
            if (info2.Unique) info2Prop.Add("unique", info2.Unique.ToString().ToLowerInvariant());

            var query = new Dictionary<string, string>(6)
                            {
                                {"method", "facebook.data.defineAssociation"},
                                {"name", name},
                                {"assoc_type", ((int) type).ToString()},
                                {"assoc_info1", ToJsonAssociativeArray(info1Prop)},
                                {"assoc_info2", ToJsonAssociativeArray(info2Prop)}
                            };
            AddParameter(query, "inverse ", inverse);

            ExecuteApiCall(query, true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Defines an object association.
        /// </summary>
        /// <param name="name">Name of forward association to create.</param>
        /// <param name="type">Type of this association.</param>
        /// <param name="info1">Describes object identifier 1 in an association.</param>
        /// <param name="info2">Describes object identifier 2 in an association.</param>
        /// <param name="inverse">(Optional) Name of backward association, if this is a two-way asymmetric one.</param>
        public void DefineAssociation(string name, DataAssociationType type,
                                      DataAssociationInfo info1, DataAssociationInfo info2, string inverse)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            string info1Alias = info1.Alias;
            string info2Alias = info2.Alias;

            CheckDataName(ref name);
            CheckDataName(ref info1Alias);
            CheckDataName(ref info2Alias);

            var info1Prop = new Dictionary <string, string> {
                { "alias", info1Alias }
            };

            AddParameter(info1Prop, "object_type", info1.ObjectType);
            if (info1.Unique)
            {
                info1Prop.Add("unique", info1.Unique.ToString().ToLowerInvariant());
            }

            var info2Prop = new Dictionary <string, string> {
                { "alias", info2Alias }
            };

            AddParameter(info2Prop, "object_type", info2.ObjectType);
            if (info2.Unique)
            {
                info2Prop.Add("unique", info2.Unique.ToString().ToLowerInvariant());
            }

            var query = new Dictionary <string, string>(6)
            {
                { "method", "facebook.data.defineAssociation" },
                { "name", name },
                { "assoc_type", ((int)type).ToString() },
                { "assoc_info1", ToJsonAssociativeArray(info1Prop) },
                { "assoc_info2", ToJsonAssociativeArray(info2Prop) }
            };

            AddParameter(query, "inverse ", inverse);

            ExecuteApiCall(query, true);
        }
Exemplo n.º 3
0
        private static void AddNewType(Type interfaceType)
        {
            List<DataAssociationAttribute> attributes = interfaceType.GetCustomAttributesRecursively<DataAssociationAttribute>().ToList();

            if (attributes.Count == 0)
            {
                return;
            }

            var dataAssociationInfos = new Dictionary<Type, DataAssociationInfo>();

            foreach (DataAssociationAttribute attribute in attributes)
            {
                if (attribute.AssociationType == DataAssociationType.None) throw new ArgumentException(string.Format("The associationType on the attribute '{0}' on the interface type '{1}' may not be '{2}'", typeof(DataAssociationAttribute), interfaceType, DataAssociationType.None));
                if (attribute.AssociatedInterfaceType == null) throw new ArgumentNullException(string.Format("The associatedInterfaceType on the attribute '{0}' on the interface type '{1}' is null", typeof(DataAssociationAttribute), interfaceType));

                List<Type> associatedTypes;

                Type associatedInterface = attribute.AssociatedInterfaceType;

                if (_associatedTypes.TryGetValue(associatedInterface, out associatedTypes) == false)
                {
                    associatedTypes = new List<Type>();

                    _associatedTypes.Add(associatedInterface, associatedTypes);

                    DataEventSystemFacade.SubscribeToDataAfterUpdate(associatedInterface, OnAfterDataUpdated, false);
                }

                associatedTypes.Add(interfaceType);


                PropertyInfo propertyInfo =
                    (from pi in interfaceType.GetAllProperties()
                     where pi.Name == attribute.ForeignKeyPropertyName
                     select pi).FirstOrDefault();

                if (propertyInfo == null) throw new ArgumentException(string.Format("The foreign key property name '{0}' set on the attribute '{1}' does not exist on the interface '{2}'", attribute.ForeignKeyPropertyName, typeof(DataAssociationAttribute), interfaceType));

                

                var dataAssociationInfo = new DataAssociationInfo
                {
                    AssociatedInterfaceType = associatedInterface,
                    ForeignKeyPropertyName = attribute.ForeignKeyPropertyName,
                    ForeignKeyPropertyInfo = propertyInfo,
                    AssociationType = attribute.AssociationType,
                };

                Verify.IsFalse(dataAssociationInfos.ContainsKey(associatedInterface), "Failed to register interface '{0}'. Data association already exist, type: '{1}'".FormatWith(interfaceType, associatedInterface));
                dataAssociationInfos.Add(associatedInterface, dataAssociationInfo);

                if (!DataProviderRegistry.AllInterfaces.Contains(associatedInterface))
                {
                    Log.LogCritical("DataReferenceRegistry", string.Format("The one type '{0}' is associated to the non supported data type '{1}'", interfaceType, associatedInterface));
                }
            }

            _dataAssociations.Add(interfaceType, dataAssociationInfos);
        }
        private static void AddNewType(Type interfaceType)
        {
            List <DataAssociationAttribute> attributes = interfaceType.GetCustomAttributesRecursively <DataAssociationAttribute>().ToList();

            if (attributes.Count == 0)
            {
                return;
            }

            var dataAssociationInfos = new Dictionary <Type, DataAssociationInfo>();

            foreach (DataAssociationAttribute attribute in attributes)
            {
                if (attribute.AssociationType == DataAssociationType.None)
                {
                    throw new ArgumentException(string.Format("The associationType on the attribute '{0}' on the interface type '{1}' may not be '{2}'", typeof(DataAssociationAttribute), interfaceType, DataAssociationType.None));
                }
                if (attribute.AssociatedInterfaceType == null)
                {
                    throw new ArgumentNullException(string.Format("The associatedInterfaceType on the attribute '{0}' on the interface type '{1}' is null", typeof(DataAssociationAttribute), interfaceType));
                }

                List <Type> associatedTypes;

                Type associatedInterface = attribute.AssociatedInterfaceType;

                if (_associatedTypes.TryGetValue(associatedInterface, out associatedTypes) == false)
                {
                    associatedTypes = new List <Type>();

                    _associatedTypes.Add(associatedInterface, associatedTypes);

                    DataEventSystemFacade.SubscribeToDataAfterUpdate(associatedInterface, OnAfterDataUpdated, false);
                }

                associatedTypes.Add(interfaceType);


                PropertyInfo propertyInfo =
                    (from pi in interfaceType.GetAllProperties()
                     where pi.Name == attribute.ForeignKeyPropertyName
                     select pi).FirstOrDefault();

                if (propertyInfo == null)
                {
                    throw new ArgumentException(string.Format("The foreign key property name '{0}' set on the attribute '{1}' does not exist on the interface '{2}'", attribute.ForeignKeyPropertyName, typeof(DataAssociationAttribute), interfaceType));
                }



                var dataAssociationInfo = new DataAssociationInfo
                {
                    AssociatedInterfaceType = associatedInterface,
                    ForeignKeyPropertyName  = attribute.ForeignKeyPropertyName,
                    ForeignKeyPropertyInfo  = propertyInfo,
                    AssociationType         = attribute.AssociationType,
                };

                Verify.IsFalse(dataAssociationInfos.ContainsKey(associatedInterface), "Failed to register interface '{0}'. Data association already exist, type: '{1}'".FormatWith(interfaceType, associatedInterface));
                dataAssociationInfos.Add(associatedInterface, dataAssociationInfo);

                if (!DataProviderRegistry.AllInterfaces.Contains(associatedInterface))
                {
                    Log.LogCritical("DataReferenceRegistry", string.Format("The one type '{0}' is associated to the non supported data type '{1}'", interfaceType, associatedInterface));
                }
            }

            _dataAssociations.Add(interfaceType, dataAssociationInfos);
        }