Table of Contents

Class ItemEditContext

Namespace
Dynamicweb.Content.Items.Rendering
Assembly
Dynamicweb.dll

Use this object inside a Using statement to setup item editing context.

public class ItemEditContext : IDisposable
Inheritance
ItemEditContext
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

ItemEditContext(int)

Initializes a new instance of the ItemEditContext class.

public ItemEditContext(int pageId)

Parameters

pageId int

ItemEditContext(int, int)

Initializes a new instance of the ItemEditContext class.

public ItemEditContext(int pageId, int areaId)

Parameters

pageId int
areaId int

Properties

AreaId

Gets the area id.

public int AreaId { get; set; }

Property Value

int

The area id.

Current

Gets the current instance of the Item rendering context.

public static ItemEditContext Current { get; }

Property Value

ItemEditContext

The current.

PageId

Gets the page id.

public int PageId { get; set; }

Property Value

int

The pagee id.

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