首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

大哥们帮忙看看这段没错的代码解决办法

2012-01-23 
大哥们帮忙看看这段没错的代码usingSystemusingSystem.ReflectionusingSystem.Collections.Specialized

大哥们帮忙看看这段没错的代码
using   System;
using   System.Reflection;
using   System.Collections.Specialized;
using   System.Configuration.Provider;
using   System.Data;
using   System.Web.Caching;
using   System.Web;
using   System.Web.Configuration;
using   System.Configuration;
namespace   Commerce.Providers   {

        public   abstract   class   CatalogProvider   :   System.Configuration.Provider.ProviderBase
        {

                static   CatalogProvider   _instance;

                static   object   padLock;
                public   static   CatalogProvider   Instance   {

                        get   {
                                CatalogProvider   tempInstance;
                                if   (_instance   ==   null)
                                        padLock   =   new   object();
                                lock   (padLock)   {
                                        tempInstance   =   LoadProvider();
                                        _instance   =   tempInstance;
                                }

                                return   _instance;
                        }
                }
                static   CatalogProvider   LoadProvider()   {

                        //   Get   the   names   of   the   providers
                        //   Use   the   cache   because   the   reflection   used   later   is   expensive
                        Cache   cache   =   System.Web.HttpRuntime.Cache;
                        string   cacheKey   =   null;

                        CatalogProvider   _instanceLoader;
                        CatalogProviderConfiguration   config   =   CatalogProviderConfiguration.GetConfig();
                        cacheKey   =   "CatalogProvider:: "   +   config.DefaultProvider;




                        object   oProvider   =   cache.Get(cacheKey);
                        if   (oProvider   !=   null)   {
                                _instanceLoader   =   (CatalogProvider)oProvider;
                        }   else   {

                                try   {

                                        //   Read   the   configuration   specific   information   for   this   provider
                                        Provider   CatalogProvider   =   (Provider)config.Providers[config.DefaultProvider];

                                        //   The   assembly   should   be   in   \bin   or   GAC
                                        Type   type   =   Type.GetType(CatalogProvider.Type);
                                        _instanceLoader   =   (CatalogProvider)Activator.CreateInstance(type);

                                        //   Initialize   the   provider   with   the   attributes.
                                        string   cStringName   =   CatalogProvider.Attributes[ "connectionStringName "];
                                        string   cString   =   System.Configuration.ConfigurationManager.ConnectionStrings[cStringName].ConnectionString;
                                        CatalogProvider.Attributes.Add( "connectionString ",   cString);
                                        _instanceLoader.Initialize(CatalogProvider.Name,   CatalogProvider.Attributes);

                                        //pop   it   into   the   cache   to   keep   out   site   from   running   into   the   ground   :)
                                        cache.Insert(cacheKey,   _instanceLoader);

                                }   catch   (Exception   e)   {


                                        throw   new   Exception( "Unable   to   load   provider ",   e);
                                }
                        }
                        return   _instanceLoader;
                }


///LoadProvider和Instance   各实现什么的功能?
这里为什么能使用CatalogProvider   ?

[解决办法]
public static CatalogProvider Instance {
的操作就是得到一个CatalogProvider 的对象

LoadProvider()是这个对象的 现实原理
这里应该是 用于CatalogProvider 对象的cache缓存的一个实现
[解决办法]
public abstract class CatalogProvider : System.Configuration.Provider.ProviderBase
{

static CatalogProvider _instance; //单件模式

static object padLock; //同步锁
public static CatalogProvider Instance { //单件模式

get {
CatalogProvider tempInstance;
if (_instance == null)
padLock = new object();
lock (padLock) {
tempInstance = LoadProvider();
_instance = tempInstance;
}

return _instance;
}
}
static CatalogProvider LoadProvider() {

// Get the names of the providers
// Use the cache because the reflection used later is expensive
Cache cache = System.Web.HttpRuntime.Cache;
string cacheKey = null;

CatalogProvider _instanceLoader;
CatalogProviderConfiguration config = CatalogProviderConfiguration.GetConfig();
cacheKey = "CatalogProvider:: " + config.DefaultProvider;


object oProvider = cache.Get(cacheKey); //试图从缓存中获取对象
if (oProvider != null) {
_instanceLoader = (CatalogProvider)oProvider;
} else {
//如果缓存中没有,就实例化一个
try {

// Read the configuration specific information for this provider
Provider CatalogProvider = (Provider)config.Providers[config.DefaultProvider];

// The assembly should be in \bin or GAC
Type type = Type.GetType(CatalogProvider.Type);
_instanceLoader = (CatalogProvider)Activator.CreateInstance(type);

// Initialize the provider with the attributes.
string cStringName = CatalogProvider.Attributes[ "connectionStringName "];
string cString = System.Configuration.ConfigurationManager.ConnectionStrings[cStringName].ConnectionString;
CatalogProvider.Attributes.Add( "connectionString ", cString);
_instanceLoader.Initialize(CatalogProvider.Name, CatalogProvider.Attributes);

//pop it into the cache to keep out site from running into the ground :)
cache.Insert(cacheKey, _instanceLoader);

} catch (Exception e) {
throw new Exception( "Unable to load provider ", e);


}
}
return _instanceLoader;
}

热点排行