返回首页

简介
本文包括一个用于创建一个简单的速度高速缓存管理器供应商,使用应用程序块软件工厂的配方。创建项目
要做的第一件事是创建项目。
如果没有您的计算机上安装的应用程序块软件工厂,然后您可以读取我写的,以安装它。
在VS2008中,选择指引软件包GT;应用程序块软件工厂的项目类型和选择供应商库模板,像下图中的??{A}
当创建新的供应商库的形式出现时,填写有关详情,然后按"完成"按钮:创建Velocity缓存管理器供应商
我们所产生的项目后,接下来要做的事情是创建Velocity缓存管理器供应商。首先添加一个缓存应用程序块的引用。之后,点击鼠标右键VelocityCacheProvider类库项目​​和应用程序块软件工厂菜单中选择新的供应商(类型)"菜单项:
这将打开新的提供向导。
填写以下细节:名称??VelocityCacheManager(提供者名称)提供程序接口??ICacheManager(提供者实现的接口)供应商基类吗??ICacheManager(供应商扩展的基类)配置的基类??CustomCacheManagerData(配置类,有助于创造一个新的VelocityCacheManager类)
这样做之后,向导应该看起来像:
按"完成"按钮创建供应商。Velocity缓存管理器的实现
CacheBaseLibrary和ClientLibary速度部署目录中发现的DLL添加引用。
以下是VelocityCacheManager实施:

using Microsoft.Data.Caching;

using Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;



namespace Microsoft.Practices.EnterpriseLibrary.Caching.Velocity

{

    [ConfigurationElementType(typeof(VelocityCacheManagerData))]

    public class VelocityCacheManager : ICacheManager

    {

        #region Consts



        private const string DefaultRegionName = "default";



        #endregion



        #region Members



        private DataCache _realCache;



        #endregion



        #region Ctor



        /// <summary>

        /// <para>Initializes a new instance of the <see

        /// cref="VelocityCacheManager"/>.</para>

        /// </summary>

        /// <param name="configuration">The configuration object

        /// used to set the runtime values</param>

        public VelocityCacheManager(VelocityCacheManagerData configuration)

        {

            var servers = CreateCacheEndpoints(configuration);

            var factory = new DataCacheFactory

		(servers, configuration.RoutingClient, configuration.LocalCache);

            _realCache = factory.GetCache(configuration.NamedCache);

        }



        #endregion



        #region Methods



        private static DataCacheServerEndpoint[] CreateCacheEndpoints(

           VelocityCacheManagerData configuration)

        {

            var servers = new DataCacheServerEndpoint[1];

            servers[0] = new DataCacheServerEndpoint(configuration.HostName,

		configuration.CachePort, configuration.CacheHostName);

            return servers;

        }



        #endregion



        #region ICacheManager Members



        public void Add(string key, object value, CacheItemPriority scavengingPriority,

	ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations)

        {

            // other parameters are currently ignored

            _realCache.Add(key, value);

        }



        public void Add(string key, object value)

        {

            _realCache.Add(key, value);

        }



        public bool Contains(string key)

        {

            object obj = _realCache.Get(key);

            return obj != null;

        }



        public int Count

        {

            get

            {

                int counter = 0;

                foreach (var item in _realCache.GetObjectsInRegion("default"))

                {

                    counter++;

                }

                return counter;

            }

        }



        public void Flush()

        {

            _realCache.ClearRegion(DefaultRegionName);

        }



        public object GetData(string key)

        {

            return _realCache.Get(key);

        }



        public void Remove(string key)

        {

            _realCache.Remove(key);

        }



        public object this[string key]

        {

            get

            {

                return _realCache.Get(key);

            }

        }



        #endregion

    }

}

以下是实施VelocityCacheManagerData:{C}创建VelocityCacheManager设计配置
执行VelocityCacheManager足够使用它,但你可能想使用Enterprise Library配置工具,它也。下面的步骤将帮助您创建企业库配置工具的设计配置。第1步
在创建VelocityCacheProvider.Configuration.Design类库,添加引用下列DLL:Microsoft.P​​ractices.EnterpriseLibrary.CachingMicrosoft.P​​ractices.EnterpriseLibrary.Caching.Configuration.DesignVelocityCacheProvider第2步
上VelocityCacheProvider.Configuration.Design按鼠标右键,并在应用程序块软件工厂菜单,选择箱子的提供者节点设计时菜单项:
在向导中,选择以下:节点名称??VelocityCacheManagerNode(在配置节点的名称)运行时配置类型??CacheManagerDataBase(类节点将在运行时)基地设计节点??CustomCacheManagerNode(设计节点的基类)父UI节点??CacheManagerCollectionNode(谁拥有我们创建的节点的节点)基数??单(启用只有在配置文件中的单个节点VelocityCacheManager可以多,如果我想有一个以上的节点)
下图显示了所选择的参数:
按Finish将创建所有相关数据和类。第3步
实现创建的类。
VelocityCacheManagerNode实施:
using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using System.Drawing.Design;

using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;

using Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.

	Configuration.Design.Properties;

using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration;



namespace Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.Design

{

    /// <summary>

    /// Represents a <see cref="Microsoft.Practices.EnterpriseLibrary.Caching.

    /// Velocity.Configuration.VelocityCacheManagerData"/> configuration element.

    /// </summary>

    public class VelocityCacheManagerNode : Microsoft.Practices.EnterpriseLibrary.

		Caching.Configuration.Design.CustomCacheManagerNode

    {

        /// <summary>

        /// Initialize a new instance of the <see cref="VelocityCacheManagerNode"/> class.

        /// </summary>

        public VelocityCacheManagerNode()

            : this(new VelocityCacheManagerData(Resources.VelocityCacheManagerNodeName,

		false, false, "localhost", 22233, "DistributedCacheService", "default"))

        {

        }



        /// <summary>

        /// Initialize a new instance of the <see cref="VelocityCacheManagerNode"/>

        /// class with a <see cref="Microsoft.Practices.EnterpriseLibrary.Caching.

        /// Velocity.Configuration.VelocityCacheManagerData"/> instance.

        /// </summary>

        /// <param name="data">A <see cref="Microsoft.Practices.

        /// EnterpriseLibrary.Caching.Velocity.Configuration.VelocityCacheManagerData"/>

        /// instance</param>

        public VelocityCacheManagerNode(VelocityCacheManagerData data)

        {

            if (null == data)

            {

                throw new ArgumentNullException("error in creating the data node -

					VelocityCacheManagerData is null");

            }



            Rename(data.Name);

            this.routingClient = data.RoutingClient;

            this.localCache = data.LocalCache;

            this.hostName = data.HostName;

            this.cachePort = data.CachePort;

            this.cacheHostName = data.CacheHostName;

            this.namedCache = data.NamedCache;

            Type = typeof(VelocityCacheManager).ToString();

        }



        /// <summary>

        /// Gets the <see cref="Microsoft.Practices.EnterpriseLibrary.Caching.

        /// Velocity.Configuration.VelocityCacheManagerData"/> this node represents.

        /// </summary>

        /// <value>

        /// The <see cref="Microsoft.Practices.EnterpriseLibrary.Caching.

        /// Velocity.Configuration.VelocityCacheManagerData"/> this node represents.

        /// </value>

        [Browsable(false)]

        public override CacheManagerDataBase CacheManagerData

        {

            get

            {

                VelocityCacheManagerData data = new VelocityCacheManagerData();

                data.Name = this.Name;

                data.RoutingClient = this.routingClient;

                data.LocalCache = this.localCache;

                data.HostName = this.hostName;

                data.CachePort = this.cachePort;

                data.CacheHostName = this.cacheHostName;

                data.NamedCache = this.namedCache;

                return data;

            }

        }



        /// <summary>

        /// Releases the unmanaged resources used by the

        /// <see cref="VelocityCacheManagerNode"/> and optionally releases

        /// the managed resources.

        /// </summary>

        /// <param name="disposing">

        /// <see langword="true"/> to release both managed and unmanaged resources;

        /// <see langword="false"/> to release only unmanaged resources.

        /// </param>

        protected override void Dispose(bool disposing)

        {

            if (disposing)

            {

            }

            base.Dispose(disposing);

        }



        private System.Boolean routingClient;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("RoutingClientDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.Boolean RoutingClient

        {

            get { return this.routingClient; }

            set { this.routingClient = value; }

        }



        private System.Boolean localCache;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("LocalCacheDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.Boolean LocalCache

        {

            get { return this.localCache; }

            set { this.localCache = value; }

        }



        private System.String hostName;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("HostNameDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.String HostName

        {

            get { return this.hostName; }

            set { this.hostName = value; }

        }



        private System.Int32 cachePort;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("CachePortDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.Int32 CachePort

        {

            get { return this.cachePort; }

            set { this.cachePort = value; }

        }



        private System.String cacheHostName;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("CacheHostNameDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.String CacheHostName

        {

            get { return this.cacheHostName; }

            set { this.cacheHostName = value; }

        }



        private System.String namedCache;



        /// <summary>

        ///

        /// </summary>

        /// <value>

        ///

        /// </value>

        [SRDescription("NamedCacheDescription", typeof(Resources))]

        [SRCategory("CategoryGeneral", typeof(Resources))]

        public System.String NamedCache

        {

            get { return this.namedCache; }

            set { this.namedCache = value; }

        }

    }

}

NodeMapRegistrar实施:
//===============================================================================

// Microsoft patterns & practices Enterprise Library

// Application Block Software Factory

//===============================================================================

// Copyright 漏 Microsoft Corporation.  All rights reserved.

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY

// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT

// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND

// FITNESS FOR A PARTICULAR PURPOSE.

//===============================================================================



using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

using Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.

	Design.Properties;



namespace Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.Design

{

    sealed class NodeMapRegistrar : Microsoft.Practices.EnterpriseLibrary.

	Configuration.Design.NodeMapRegistrar

    {

        public NodeMapRegistrar(IServiceProvider serviceProvider)

            : base(serviceProvider)

        {

        }



        public override void Register()

        {

            AddSingleNodeMap(Resources.VelocityCacheManagerNodeUICommandText,

               typeof(VelocityCacheManagerNode),

               typeof(VelocityCacheManagerData));

        }

    }

}

CommandRegistrar实施:
//===============================================================================

// Microsoft patterns & practices Enterprise Library

// Application Block Software Factory

//===============================================================================

// Copyright 漏 Microsoft Corporation.  All rights reserved.

// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY

// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT

// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND

// FITNESS FOR A PARTICULAR PURPOSE.

//===============================================================================



using System;

using System.Text;

using System.Collections.Generic;

using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

using Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.

	Design.Properties;



namespace Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.Design

{

    sealed partial class CommandRegistrar : Microsoft.Practices.

		EnterpriseLibrary.Configuration.Design.CommandRegistrar

    {

        public CommandRegistrar(IServiceProvider serviceProvider)

            : base(serviceProvider)

        {

        }



        public override void Register()

        {

            AddVelocityCacheManagerNodeCommand();

            AddDefaultCommands(typeof(VelocityCacheManagerNode));

        }

    }

}

CommandRegistrar.VelocityCacheManagerNode实施:
using System;

using Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.

	Configuration.Design.Properties;

using Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.Design;



namespace Microsoft.Practices.EnterpriseLibrary.Caching.Velocity.Configuration.Design

{

    sealed partial class CommandRegistrar

    {

        private void AddVelocityCacheManagerNodeCommand()

        {

            AddSingleChildNodeCommand(

                Resources.VelocityCacheManagerNodeUICommandText,

                Resources.VelocityCacheManagerNodeUICommandLongText,

                typeof(VelocityCacheManagerNode),

                typeof(CacheManagerCollectionNode));

        }

    }

}
第4步
编辑资源文件看起来像:
这就是它。
现在你只需要删除这两个生成的DLL配置工具的目录,你是清楚的去和使用工具来配置VelocityCacheManager。下图显示了它是如何看起来像里面的Enterprise Library配置工具:摘要
文章描述了如何创建一个简单的企业库缓存应用程序块的速度高速缓存管理器。我还介绍了如何集成的创建提供程序库,企业库的配置工具。从{A9},您可以下载供应商库。
你可以使用我写的和改变我的代码在您的实现提供的供应商。

回答

评论会员: 时间:2