Table of Contents

Class NotificationContext

Namespace
Dynamicweb.Extensibility.Notifications
Assembly
Dynamicweb.Core.dll

Use this object inside a Using statement to handle how notifications are handled. Can be used to suppress all notifications from objects and code run within this using block.

public class NotificationContext : IDisposable
Inheritance
NotificationContext
Implements
Inherited Members

Examples

#nullable enable
using System;
using System.Collections.Generic;
namespace Dynamicweb.Extensibility.Notifications
{

    /// <summary>
    /// Use this object inside a Using statement to handle how notifications are handled. Can be used to suppress all notifications from objects and code run within this using block.
    /// </summary>
    /// <example>
    /// <code description="Updating data in the database" source="NotificationContext.cs" lang="CS"></code>
    /// </example>
    public class NotificationContext : IDisposable
    {
        private NotificationState _state = NotificationState.Notify;
        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <value>The state.</value>
        public NotificationState State
        {
            get { return _state; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="NotificationContext"/> class.
        /// </summary>
        /// <param name="state">The state.</param>
        public NotificationContext(NotificationState state)
        {
            _state = state;
            AddContext();
        }

        /// <summary>
        /// Gets the suppressed notifications.
        /// </summary>
        /// <value>The suppressed notifications.</value>
        public static NotificationContext SuppressNotifications
        {
            get { return new NotificationContext(NotificationState.SuppressNotifications); }
        }

        /// <summary>
        /// Gets the NotificationContext.
        /// </summary>
        /// <value>The NotificationContext.</value>
        public static NotificationContext Notify
        {
            get { return new NotificationContext(NotificationState.Notify); }
        }

        /// <summary>
        /// 
        /// </summary>
        public enum NotificationState
        {
            /// <summary>
            /// Suppresses all notifications and makes sure that no observers (notification subscribers) are called within the using statement.
            /// </summary>
            SuppressNotifications,
            /// <summary>
            /// Default behavior. Makes all notifications being raised as expected. Calls all the observers (notification subscribers) raised by the objects within the using statement.
            /// </summary>
            Notify,
            /// <summary>
            /// Used by the loadbalancer to keep track of which updates need to be broadcast and which do not.
            /// </summary>
            LoadBalancing
        }

        /// <summary>
        /// Gets the current instance of the NotificationContext.
        /// </summary>
        /// <value>The current.</value>
        public static NotificationContext? Current
        {
            get { return Stack.Count > 0 ? Stack.Peek() : null; }
        }

        /// <summary>
        /// Gets the current state of the NotificationContext.
        /// </summary>
        /// <value>The state of the current.</value>
        public static NotificationState CurrentState
        {
            get { return Current?.State ?? NotificationState.Notify; }
        }

        #region "Internal logic"
        //since a NotificationContext is never shared between two states in the page request, this should be OK. 
        //Fx. Application_AuthenticateRequest and Application_BeginRequest cannot shared the same context, so threadstatic 
        //is not used to handle concurrency between states in the page request.
        [ThreadStatic()]
        private static Stack<NotificationContext>? _stack; 
        private static Stack<NotificationContext> Stack
        {
            get
            {
                if (_stack == null)
                {
                    _stack = new Stack<NotificationContext>();
                }
                return _stack;
            }
        }

        private void AddContext()
        {
            Stack.Push(this);
        }

        private void RemoveContext()
        {
            if (!ReferenceEquals(Current, this))
            {
                throw new Exception("Invalid notification context state");
            }
            Stack.Pop();
        }
        #endregion

        #region "IDisposable Support"
        // To detect redundant calls
        private bool disposedValue;

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    RemoveContext();
                }

            }
            disposedValue = true;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            // Do not change this code.  Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

    }


}

Constructors

NotificationContext(NotificationState)

Initializes a new instance of the NotificationContext class.

public NotificationContext(NotificationContext.NotificationState state)

Parameters

state NotificationContext.NotificationState

The state.

Properties

Current

Gets the current instance of the NotificationContext.

public static NotificationContext? Current { get; }

Property Value

NotificationContext

The current.

CurrentState

Gets the current state of the NotificationContext.

public static NotificationContext.NotificationState CurrentState { get; }

Property Value

NotificationContext.NotificationState

The state of the current.

Notify

Gets the NotificationContext.

public static NotificationContext Notify { get; }

Property Value

NotificationContext

The NotificationContext.

State

Gets the state.

public NotificationContext.NotificationState State { get; }

Property Value

NotificationContext.NotificationState

The state.

SuppressNotifications

Gets the suppressed notifications.

public static NotificationContext SuppressNotifications { get; }

Property Value

NotificationContext

The suppressed notifications.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Releases unmanaged and - optionally - managed resources.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

To top