コード例 #1
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Try get property.
        /// </summary>
        /// <exception cref="Exception">
        ///  Thrown when an exception error condition occurs.
        /// </exception>
        /// <param name="propertyName">
        ///  Name of the property.
        /// </param>
        /// <returns>
        ///  An object.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public object TryGetProperty(string propertyName)
        {
            switch (propertyName)
            {
            case "Id":
                return(((IModelElement)this).Id);

            case "DomainModel":
                return(DomainModel);

            case "Schema":
                return(((IModelElement)this).SchemaInfo);

            case "Store":
                return(Store);
            }

            var property = ((IModelElement)this).SchemaInfo.GetProperty(propertyName);

            if (property == null)
            {
                object refer;
                if (!_references.TryGetValue(propertyName, out refer))
                {
                    // Find a relationship whith this name
                    var relationship = ((IModelElement)this).SchemaInfo.GetRelationships()
                                       .FirstOrDefault(r => IsMatchPropertyName(r, propertyName)) as ISchemaRelationship;
                    if (relationship == null)
                    {
                        throw new Hyperstore.Modeling.Metadata.PropertyDefinitionException(string.Format(ExceptionMessages.UnknownPropertyFormat, propertyName));
                    }

                    if (relationship.Cardinality == Cardinality.OneToOne || relationship.End.Id == ((IModelElement)this).SchemaInfo.Id) // Noeud terminal
                    {
                        refer = new ReferenceHandler(this, relationship, relationship.Cardinality != Cardinality.OneToOne);
                        _references.TryAdd(propertyName, refer);
                    }
                    else
                    {
                        // TODO create a proxy for enumerable to take into account extensions (convert First() to Enumerable.First(..)) - proxy for Observablecollection should implement inotifycollectionchanged
                        var isObservable = this is INotifyPropertyChanged && (((IModelElement)this).SchemaInfo.Schema.Behavior & DomainBehavior.Observable) != DomainBehavior.Observable;
                        if (isObservable)
                        {
                            refer = new ObservableModelElementCollection <IModelElement>(this, relationship);
                        }
                        else
                        {
                            refer = new ModelElementCollection <IModelElement>(this, relationship);
                        }
                        _references.TryAdd(propertyName, refer);
                    }
                }

                var handler = refer as ReferenceHandler;
                if (handler != null)
                {
                    return(handler.GetReference());
                }

                return(refer);
            }

            var pv = GetPropertyValue(property);

            return(pv.Value);
        }
コード例 #2
0
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Try set property.
        /// </summary>
        /// <exception cref="Exception">
        ///  Thrown when an exception error condition occurs.
        /// </exception>
        /// <param name="propertyName">
        ///  Name of the property.
        /// </param>
        /// <param name="value">
        ///  The value.
        /// </param>
        /// <returns>
        ///  An object.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public object TrySetProperty(string propertyName, object value)
        {
            var property = ((IModelElement)this).SchemaInfo.GetProperty(propertyName);

            if (property == null)
            {
                ReferenceHandler refer = null;
                object           obj;
                if (!_references.TryGetValue(propertyName, out obj))
                {
                    // Find a relationship whith this name
                    foreach (ISchemaRelationship relationship in ((IModelElement)this).SchemaInfo.GetRelationships())
                    {
                        if (relationship.StartPropertyName == propertyName)
                        {
                            if (relationship.Cardinality != Cardinality.OneToOne && relationship.Cardinality != Cardinality.ManyToOne)
                            {
                                throw new HyperstoreException(ExceptionMessages.InvalidValue);
                            }

                            refer = new ReferenceHandler(this, relationship, false);
                            break;
                        }

                        if (relationship.EndPropertyName == propertyName)
                        {
                            if (relationship.Cardinality != Cardinality.OneToMany && relationship.Cardinality != Cardinality.OneToOne)
                            {
                                throw new HyperstoreException(ExceptionMessages.InvalidValue);
                            }

                            refer = new ReferenceHandler(this, relationship, true);
                            break;
                        }
                    }

                    _references.TryAdd(propertyName, refer); // adding even refer is null
                    if (refer == null)
                    {
                        throw new Hyperstore.Modeling.Metadata.PropertyDefinitionException(string.Format(ExceptionMessages.UnknownPropertyFormat, propertyName));
                    }
                }
                else
                {
                    refer = obj as ReferenceHandler;
                    if (refer == null)
                    {
                        throw new HyperstoreException(ExceptionMessages.InvalidValue);
                    }
                }

                if (value != null && !(value is IModelElement))
                {
                    throw new HyperstoreException(ExceptionMessages.InvalidValue);
                }

                var mel = value as IModelElement;

                ((ReferenceHandler)refer).SetReference(mel);
                return(value);
            }

            SetPropertyValue(property, value);
            return(value);
        }