⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applicationcontext.cs

📁 C# 版本的一个三层商业架构
💻 CS
字号:
using System;
using System.Threading;
using System.Collections.Specialized;

namespace CSLA
{
  /// <summary>
  /// Provides consistent context information between the client
  /// and server DataPortal objects. 
  /// </summary>
  public class ApplicationContext
  {
    /// <summary>
    /// Returns the application-specific context data provided
    /// by the client.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The return value is a HybridDictionary. If one does
    /// not already exist, and empty one is created and returned.
    /// </para><para>
    /// Note that data in this context is transferred from
    /// the client to the server. No data is transferred from
    /// the server to the client.
    /// <para>
    /// </remarks>
    public static HybridDictionary ClientContext 
    {
      get
      {
        System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot("CSLA.ClientContext");
        HybridDictionary ctx = (HybridDictionary)Thread.GetData(slot);
        if(ctx == null)
        {
          ctx = new HybridDictionary();
          Thread.SetData(slot, ctx);
        }
        return ctx;
      }
    }

    /// <summary>
    /// Returns the application-specific context data shared
    /// on both client and server.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The return value is a HybridDictionary. If one does
    /// not already exist, and empty one is created and returned.
    /// </para><para>
    /// Note that data in this context is transferred to and from
    /// the client and server. Any objects or data in this context
    /// will be transferred bi-directionally across the network.
    /// <para>
    /// </remarks>
    public static HybridDictionary GlobalContext
    {
      get
      {
        System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot("CSLA.GlobalContext");
        HybridDictionary ctx = (HybridDictionary)Thread.GetData(slot);
        if(ctx == null)
        {
          ctx = new HybridDictionary();
          Thread.SetData(slot, ctx);
        }
        return ctx;
      }
    }

    internal static HybridDictionary GetClientContext()
    {
      System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot("CSLA.ClientContext");
      return (HybridDictionary)Thread.GetData(slot);
    }

    internal static HybridDictionary GetGlobalContext() 
    {
      System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot("CSLA.GlobalContext");
      return (HybridDictionary)Thread.GetData(slot);
    }

    internal static void SetContext(object clientContext, object globalContext)
    {
      System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot("CSLA.ClientContext");
      Thread.SetData(slot, clientContext);

      slot = Thread.GetNamedDataSlot("CSLA.GlobalContext");
      Thread.SetData(slot, globalContext);
    }

    public static void Clear()
    {
      Thread.FreeNamedDataSlot("CSLA.ClientContext");
      Thread.FreeNamedDataSlot("CSLA.GlobalContext");
    }

    private ApplicationContext()
    {
      // prevent instantiation
    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -