ActivityInstanceMap.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / System.Activities / System / Activities / Runtime / ActivityInstanceMap.cs / 1305376 / ActivityInstanceMap.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities.Runtime 
{
    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime; 
    using System.Runtime.Serialization;
    using System.Collections.ObjectModel;

    [DataContract(Name = XD.Runtime.ActivityInstanceMap, Namespace = XD.Runtime.Namespace)] 
    class ActivityInstanceMap
    { 
        // map from activities to (active) associated activity instances 
        IDictionary instanceMapping;
        InstanceList[] rawDeserializedLists; 

        internal ActivityInstanceMap()
        {
        } 

        [SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode, 
            Justification = "Called by serialization")] 
        [DataMember(EmitDefaultValue = false)]
        InstanceList[] SerializedInstanceLists 
        {
            get
            {
                if (this.instanceMapping == null || this.instanceMapping.Count == 0) 
                {
                    return this.rawDeserializedLists; 
                } 
                else
                { 
                    InstanceList[] lists = new InstanceList[this.instanceMapping.Count];
                    int index = 0;
                    foreach (KeyValuePair entry in this.instanceMapping)
                    { 
                        entry.Value.ActivityId = entry.Key.QualifiedId.AsByteArray();
                        lists[index] = entry.Value; 
                        index++; 
                    }
 
                    return lists;
                }
            }
            set 
            {
                Fx.Assert(value != null, "We don't serialize the default value."); 
 
                this.rawDeserializedLists = value;
            } 
        }

        IDictionary InstanceMapping
        { 
            get
            { 
                if (this.instanceMapping == null) 
                {
                    this.instanceMapping = new Dictionary(); 
                }

                return this.instanceMapping;
            } 
        }
 
        public void AddEntry(IActivityReference reference, bool skipIfDuplicate) 
        {
            Activity activity = reference.Activity; 

            InstanceList mappedInstances;
            if (this.InstanceMapping.TryGetValue(activity, out mappedInstances))
            { 
                mappedInstances.Add(reference, skipIfDuplicate);
            } 
            else 
            {
                this.InstanceMapping.Add(activity, new InstanceList(reference)); 
            }
        }

        public void AddEntry(IActivityReference reference) 
        {
            AddEntry(reference, false); 
        } 

        public void LoadActivityTree(Activity rootActivity, ActivityInstance rootInstance, List secondaryRootInstances, ActivityExecutor executor) 
        {
            Fx.Assert(this.rawDeserializedLists != null, "We should always have deserialized some lists.");

            this.instanceMapping = new Dictionary(this.rawDeserializedLists.Length); 

            for (int i = 0; i < this.rawDeserializedLists.Length; i++) 
            { 
                InstanceList list = this.rawDeserializedLists[i];
                Activity activity; 
                if (!QualifiedId.TryGetElementFromRoot(rootActivity, list.ActivityId, out activity))
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ActivityInstanceFixupFailed));
                } 
                this.instanceMapping.Add(activity, list);
                list.Load(activity, this); 
            } 

            // We need to null this out once we've recreated the dictionary to avoid 
            // having out of [....] data
            this.rawDeserializedLists = null;

            // then walk our instance list, fixup parent references, and perform basic validation 
            Func processInstanceCallback = new Func(OnActivityInstanceLoaded);
 
            rootInstance.FixupInstance(null, this, executor); 
            ActivityUtilities.ProcessActivityInstanceTree(rootInstance, executor, processInstanceCallback);
 
            if (secondaryRootInstances != null)
            {
                foreach (ActivityInstance instance in secondaryRootInstances)
                { 
                    instance.FixupInstance(null, this, executor);
                    ActivityUtilities.ProcessActivityInstanceTree(instance, executor, processInstanceCallback); 
                } 
            }
        } 

        bool OnActivityInstanceLoaded(ActivityInstance activityInstance, ActivityExecutor executor)
        {
            return activityInstance.TryFixupChildren(this, executor); 
        }
 
        public bool RemoveEntry(IActivityReference reference) 
        {
            if (this.instanceMapping == null) 
            {
                return false;
            }
 
            Activity activity = reference.Activity;
 
            InstanceList mappedInstances; 
            if (!this.InstanceMapping.TryGetValue(activity, out mappedInstances))
            { 
                return false;
            }

            if (mappedInstances.Count == 1) 
            {
                this.InstanceMapping.Remove(activity); 
            } 
            else
            { 
                mappedInstances.Remove(reference);
            }

            return true; 
        }
 
        [DataContract] 
        class InstanceList : HybridCollection
        { 
            public InstanceList(IActivityReference reference)
                : base(reference)
            {
            } 

            [SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode, 
                Justification = "Called by serialization")] 
            [DataMember]
            public byte[] ActivityId 
            {
                get;
                set;
            } 

            [OnSerializing] 
            [SuppressMessage(FxCop.Category.Usage, FxCop.Rule.ReviewUnusedParameters)] 
            void OnSerializing(StreamingContext context)
            { 
                base.Compress();
            }

            public void Add(IActivityReference reference, bool skipIfDuplicate) 
            {
                Fx.Assert(this.Count >= 1, "instance list should never be empty when we call Add"); 
 
                if (skipIfDuplicate)
                { 
                    if (base.SingleItem != null)
                    {
                        if (base.SingleItem == reference)
                        { 
                            return;
                        } 
                    } 
                    else
                    { 
                        if (base.MultipleItems.Contains(reference))
                        {
                            return;
                        } 
                    }
                } 
 
                Add(reference);
            } 

            public void Load(Activity activity, ActivityInstanceMap instanceMap)
            {
                Fx.Assert(this.Count >= 1, "instance list should never be empty on load"); 
                if (base.SingleItem != null)
                { 
                    base.SingleItem.Load(activity, instanceMap); 
                }
                else 
                {
                    for (int i = 0; i < base.MultipleItems.Count; i++)
                    {
                        base.MultipleItems[i].Load(activity, instanceMap); 
                    }
                } 
            } 
        }
 
        public interface IActivityReference
        {
            Activity Activity { get; }
            void Load(Activity activity, ActivityInstanceMap instanceMap); 
        }
    } 
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK