Beispiel #1
0
        void ITransactedModelEvent.Rollback(ModelTransaction transaction)
        {
            Prepare(transaction);

            ModelEventScope.Perform(() =>
            {
                ModelContext context   = Instance.Type.Context;
                ModelInstanceList list = Instance.GetList(Property);

                if (Added != null)
                {
                    foreach (ModelInstance item in Added)
                    {
                        list.Remove(item);
                    }
                }
                if (Removed != null)
                {
                    foreach (ModelInstance item in Removed)
                    {
                        list.Add(item);
                    }
                }
            });
        }
        /// <summary>
        /// Enumerates over the set of instances represented by the current step.
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public IEnumerable <ModelInstance> GetInstances(ModelInstance instance)
        {
            // Exit immediately if the property is not valid for the specified instance
            if (!DeclaringType.IsInstanceOfType(instance))
            {
                throw new ArgumentException("The current property is not valid for the specified instance.");
            }

            // Return each instance exposed by a list property
            if (IsList)
            {
                ModelInstanceList children = instance.GetList(this);
                if (children != null)
                {
                    foreach (ModelInstance child in children)
                    {
                        yield return(child);
                    }
                }
            }

            // Return the instance exposed by a reference property
            else
            {
                ModelInstance child = instance.GetReference(this);
                if (child != null)
                {
                    yield return(child);
                }
            }
        }
Beispiel #3
0
        public IModelPropertySource GetSource(ModelInstance root, Func <ModelInstance, ModelReferenceProperty, int, bool> whenNull, Action <ModelInstance, ModelReferenceProperty, int, ModelInstance> whenNotNull)
        {
            // Return the source type for static paths
            if (IsStatic)
            {
                return(ModelContext.Current.GetModelType(SourceType));
            }

            //walk the path performing whenNull if an entity is null
            //if an entity is null and whenNull returns false
            //then exit out of SetValue returning false
            foreach (SourceStep step in this.steps.Take(this.steps.Length - 1))
            {
                ModelReferenceProperty stepProp = (ModelReferenceProperty)step.DeclaringType.Properties[step.Property];
                if (stepProp.IsList)
                {
                    ModelInstanceList list = root.GetList(stepProp);
                    if (list.Count < step.Index + 1 && (whenNull == null || !whenNull(root, stepProp, step.Index)))
                    {
                        return(null);
                    }

                    var item = list[step.Index];

                    if (whenNotNull != null)
                    {
                        whenNotNull(root, stepProp, step.Index, item);
                    }

                    root = item;
                }
                else
                {
                    if (root.GetReference(stepProp) == null && (whenNull == null || !whenNull(root, stepProp, step.Index)))
                    {
                        return(null);
                    }
                    else
                    {
                        //advance to the next step in the chain.
                        var child = root.GetReference(step.Property);

                        if (whenNotNull != null)
                        {
                            whenNotNull(root, stepProp, step.Index, child);
                        }

                        root = child;
                    }
                }
            }

            return(root);
        }
Beispiel #4
0
        /// <summary>
        /// Enumerates over the set of instances represented by the current step.
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public IEnumerable <ModelInstance> GetInstances(ModelInstance instance)
        {
            // Stop loading if the step is null or represents a value
            if (Property is ModelValueProperty || !((ModelReferenceProperty)Property).DeclaringType.IsInstanceOfType(instance))
            {
                yield break;
            }

            // Cast the property to the correct type
            var reference = (ModelReferenceProperty)Property;

            // Return each instance exposed by a list property
            if (reference.IsList)
            {
                ModelInstanceList children = instance.GetList(reference);
                if (children != null)
                {
                    foreach (ModelInstance child in instance.GetList(reference))
                    {
                        if (Filter == null || Filter.IsInstanceOfType(child))
                        {
                            yield return(child);
                        }
                    }
                }
            }

            // Return the instance exposed by a reference property
            else
            {
                ModelInstance child = instance.GetReference(reference);
                if (child != null && (Filter == null || Filter.IsInstanceOfType(child)))
                {
                    yield return(child);
                }
            }
        }