Пример #1
0
        /// <summary>
        /// <para>Applies a previously registered action on a object. The contextCotainer object MUST contain any context objects required by the registered action.</para>
        /// <para>Registered actions on interfaces and base classes are also automaticaly applied in this order: Interface actions, Base Class (top to bottom) actions, Target type action.</para>
        /// </summary>
        /// <typeparam name="T">The target type</typeparam>
        /// <param name="item">The target instance</param>
        /// <param name="contextContainer">A context container created by LinqMapper.CreateContext() or null</param>
        /// <returns>The target instance</returns>
        public static T ApplyAfterMap <T>(T item, IObjectContext contextContainer = null)
        {
            if (item == null)
            {
                return(item);
            }

            contextContainer = contextContainer ?? LinqMapper.CreateContext();
            var runtimeInfo = InternalCache.GetRuntimeInfo(typeof(T));

            if (runtimeInfo != null)
            {
                //make sure to dispose tracker
                using (var tracker = new MaterializerTracker())
                {
                    if (Materializer.TreeTraversal == TreeTraversal.ChildrenFirst)
                    {
                        runtimeInfo.ApplyOn(item, contextContainer, tracker);
                    }
                    else
                    {
                        runtimeInfo.ApplyOnRootFirst(item, contextContainer, tracker);
                    }
                }
            }

            return(item);
        }
Пример #2
0
            public void ApplyOnRootFirst(object item, IObjectContext context, MaterializerTracker tracker)
            {
                if (item == null)
                {
                    return;
                }

                //Returns if object has already been processed in this context.
                if (!tracker.BeginTrackObject(item))
                {
                    return;
                }

                if (_actionInfo != null)
                {
                    _actionInfo.ApplyAction(item, context);
                }

                if (_runtimeCollectionElementInfo != null && item is IEnumerable)
                {
                    foreach (var element in (item as IEnumerable))
                    {
                        _runtimeCollectionElementInfo.ApplyOnRootFirst(element, context, tracker);
                    }
                }

                if (_runtimeMembers.Length > 0)
                {
                    foreach (var runtimeMember in _runtimeMembers)
                    {
                        var memberValue = runtimeMember.MemberInfo.GetFieldOrPropertyValue(item);
                        runtimeMember.RuntimeInfo.ApplyOnRootFirst(memberValue, context, tracker);
                    }
                }
            }