UpdateTracker.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWeb / Server / System / Data / Services / UpdateTracker.cs / 1 / UpdateTracker.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides a class used to track updates for callbacks.
//  
// 
// @owner  [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services
{
    using System.Collections.Generic; 
    using System.Data.Services.Providers;
    using System.Diagnostics; 
    using System.Reflection; 

    /// Provides a class used to track updates for callbacks. 
    internal class UpdateTracker
    {
        #region Private fields.
 
        /// 
        /// A dictionary of containers mapping to the changes on those 
        /// containers, each of which consists of an element and the 
        /// action taken on it.
        ///  
        private Dictionary> items;

        /// Underlying data source instance.
        private IDataServiceProvider provider; 

        #endregion Private fields. 
 
        /// Initializes a new  instance.
        /// underlying data source instance. 
        private UpdateTracker(IDataServiceProvider provider)
        {
            this.provider = provider;
            this.items = new Dictionary>(); 
        }
 
        /// Fires the notification for a single action. 
        /// Service on which methods should be invoked.
        /// Object to be tracked. 
        /// Container in which object is changed.
        /// Action affecting target.
        internal static void FireNotification(object service, object target, ResourceContainer container, UpdateOperations action)
        { 
            Debug.Assert(service != null, "service != null");
            AssertActionValues(target, container); 
 
            MethodInfo[] methods = container.WriteAuthorizationMethods;
            if (methods != null) 
            {
                object[] parameters = new object[2];
                parameters[0] = target;
                parameters[1] = action; 
                for (int i = 0; i < methods.Length; i++)
                { 
                    try 
                    {
                        methods[i].Invoke(service, parameters); 
                    }
                    catch (TargetInvocationException exception)
                    {
                        ErrorHandler.HandleTargetInvocationException(exception); 
                        throw;
                    } 
                } 
            }
        } 

        /// 
        /// Create a new instance of update tracker
        ///  
        /// description about the request uri.
        /// underlying data source. 
        ///  
        /// Returns a new instance of UpdateTracker if the request is not targetted at open types; null otherwise.
        ///  
#if !ASTORIA_OPEN_OBJECT
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "description", Justification = "Required for open type support")]
#endif
        internal static UpdateTracker CreateUpdateTracker(RequestDescription description, IDataServiceProvider provider) 
        {
            // We don't support notifications for open types. Hence not creating a tracker for open types request. 
#if ASTORIA_OPEN_OBJECT 
            if (description.TargetKind != RequestTargetKind.OpenProperty &&
                description.TargetKind != RequestTargetKind.OpenPropertyValue) 
            {
#endif
                return new UpdateTracker(provider);
#if ASTORIA_OPEN_OBJECT 
            }
 
            return null; 
#endif
        } 

        /// Fires all notifications
        /// Service on which methods should be invoked.
        internal void FireNotifications(object service) 
        {
            Debug.Assert(service != null, "service != null"); 
 
            object[] parameters = new object[2];
            foreach (var item in this.items) 
            {
                MethodInfo[] methods = item.Key.WriteAuthorizationMethods;
                Debug.Assert(methods != null, "methods != null - should not have been tracking changes to the container otherwise.");
                foreach (var element in item.Value) 
                {
                    parameters[0] = this.provider.ResolveResource(element.Key); 
                    parameters[1] = element.Value; 
                    for (int i = 0; i < methods.Length; i++)
                    { 
                        try
                        {
                            methods[i].Invoke(service, parameters);
                        } 
                        catch (TargetInvocationException exception)
                        { 
                            ErrorHandler.HandleTargetInvocationException(exception); 
                            throw;
                        } 
                    }
                }

                // Make elements elegible for garbage collection. 
                item.Value.Clear();
            } 
 
            // Make dictionary elegible for garbage collection.
            this.items = null; 
        }

        /// 
        /// Tracks the specified  for a 
        /// given  on the .
        ///  
        /// Object to be tracked. 
        /// Container in which object is changed.
        /// Action affecting target. 
        /// 
        /// If  was already being tracked, the actions are OR'ed together.
        /// 
        internal void TrackAction(object target, ResourceContainer container, UpdateOperations action) 
        {
            AssertActionValues(target, container); 
            Debug.Assert(this.items != null, "this.items != null - otherwise FireNotification has already been called"); 

            // If it won't be necessary for us to fire authorizatio methods, 
            // skip tracking altogether.
            if (container.WriteAuthorizationMethods == null)
            {
                return; 
            }
 
            // Get the container for which the change has taken place. 
            Dictionary changedItems;
            if (!this.items.TryGetValue(container, out changedItems)) 
            {
                changedItems = new Dictionary();
                this.items.Add(container, changedItems);
            } 

            UpdateOperations existingAction; 
            if (changedItems.TryGetValue(target, out existingAction)) 
            {
                if ((action | existingAction) != existingAction) 
                {
                    changedItems[target] = action | existingAction;
                }
            } 
            else
            { 
                changedItems.Add(target, action); 
            }
        } 

        /// Asserts valid value for tracking update actions.
        /// Object to be tracked.
        /// Container in which object is changed. 
        [Conditional("DEBUG")]
        private static void AssertActionValues(object target, ResourceContainer container) 
        { 
            Debug.Assert(target != null, "target != null");
            Debug.Assert(container != null, "container != null"); 
        }
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides a class used to track updates for callbacks.
//  
// 
// @owner  [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services
{
    using System.Collections.Generic; 
    using System.Data.Services.Providers;
    using System.Diagnostics; 
    using System.Reflection; 

    /// Provides a class used to track updates for callbacks. 
    internal class UpdateTracker
    {
        #region Private fields.
 
        /// 
        /// A dictionary of containers mapping to the changes on those 
        /// containers, each of which consists of an element and the 
        /// action taken on it.
        ///  
        private Dictionary> items;

        /// Underlying data source instance.
        private IDataServiceProvider provider; 

        #endregion Private fields. 
 
        /// Initializes a new  instance.
        /// underlying data source instance. 
        private UpdateTracker(IDataServiceProvider provider)
        {
            this.provider = provider;
            this.items = new Dictionary>(); 
        }
 
        /// Fires the notification for a single action. 
        /// Service on which methods should be invoked.
        /// Object to be tracked. 
        /// Container in which object is changed.
        /// Action affecting target.
        internal static void FireNotification(object service, object target, ResourceContainer container, UpdateOperations action)
        { 
            Debug.Assert(service != null, "service != null");
            AssertActionValues(target, container); 
 
            MethodInfo[] methods = container.WriteAuthorizationMethods;
            if (methods != null) 
            {
                object[] parameters = new object[2];
                parameters[0] = target;
                parameters[1] = action; 
                for (int i = 0; i < methods.Length; i++)
                { 
                    try 
                    {
                        methods[i].Invoke(service, parameters); 
                    }
                    catch (TargetInvocationException exception)
                    {
                        ErrorHandler.HandleTargetInvocationException(exception); 
                        throw;
                    } 
                } 
            }
        } 

        /// 
        /// Create a new instance of update tracker
        ///  
        /// description about the request uri.
        /// underlying data source. 
        ///  
        /// Returns a new instance of UpdateTracker if the request is not targetted at open types; null otherwise.
        ///  
#if !ASTORIA_OPEN_OBJECT
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "description", Justification = "Required for open type support")]
#endif
        internal static UpdateTracker CreateUpdateTracker(RequestDescription description, IDataServiceProvider provider) 
        {
            // We don't support notifications for open types. Hence not creating a tracker for open types request. 
#if ASTORIA_OPEN_OBJECT 
            if (description.TargetKind != RequestTargetKind.OpenProperty &&
                description.TargetKind != RequestTargetKind.OpenPropertyValue) 
            {
#endif
                return new UpdateTracker(provider);
#if ASTORIA_OPEN_OBJECT 
            }
 
            return null; 
#endif
        } 

        /// Fires all notifications
        /// Service on which methods should be invoked.
        internal void FireNotifications(object service) 
        {
            Debug.Assert(service != null, "service != null"); 
 
            object[] parameters = new object[2];
            foreach (var item in this.items) 
            {
                MethodInfo[] methods = item.Key.WriteAuthorizationMethods;
                Debug.Assert(methods != null, "methods != null - should not have been tracking changes to the container otherwise.");
                foreach (var element in item.Value) 
                {
                    parameters[0] = this.provider.ResolveResource(element.Key); 
                    parameters[1] = element.Value; 
                    for (int i = 0; i < methods.Length; i++)
                    { 
                        try
                        {
                            methods[i].Invoke(service, parameters);
                        } 
                        catch (TargetInvocationException exception)
                        { 
                            ErrorHandler.HandleTargetInvocationException(exception); 
                            throw;
                        } 
                    }
                }

                // Make elements elegible for garbage collection. 
                item.Value.Clear();
            } 
 
            // Make dictionary elegible for garbage collection.
            this.items = null; 
        }

        /// 
        /// Tracks the specified  for a 
        /// given  on the .
        ///  
        /// Object to be tracked. 
        /// Container in which object is changed.
        /// Action affecting target. 
        /// 
        /// If  was already being tracked, the actions are OR'ed together.
        /// 
        internal void TrackAction(object target, ResourceContainer container, UpdateOperations action) 
        {
            AssertActionValues(target, container); 
            Debug.Assert(this.items != null, "this.items != null - otherwise FireNotification has already been called"); 

            // If it won't be necessary for us to fire authorizatio methods, 
            // skip tracking altogether.
            if (container.WriteAuthorizationMethods == null)
            {
                return; 
            }
 
            // Get the container for which the change has taken place. 
            Dictionary changedItems;
            if (!this.items.TryGetValue(container, out changedItems)) 
            {
                changedItems = new Dictionary();
                this.items.Add(container, changedItems);
            } 

            UpdateOperations existingAction; 
            if (changedItems.TryGetValue(target, out existingAction)) 
            {
                if ((action | existingAction) != existingAction) 
                {
                    changedItems[target] = action | existingAction;
                }
            } 
            else
            { 
                changedItems.Add(target, action); 
            }
        } 

        /// Asserts valid value for tracking update actions.
        /// Object to be tracked.
        /// Container in which object is changed. 
        [Conditional("DEBUG")]
        private static void AssertActionValues(object target, ResourceContainer container) 
        { 
            Debug.Assert(target != null, "target != null");
            Debug.Assert(container != null, "container != null"); 
        }
    }
}

// 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