开发自定义的Provider

asp.net 2.0在很多地方都用到Provider Pattern,比如Personalization、MemberShip等都用到了该模式,其可插拔的特性给开发人员极大的便利,开发人员可以更据自己的需求来开发自己的需要

.Net 2.0中给我们已经定义了一个ProviderBase的抽象类,继承自它我们可以方便的开发自定义的Provider

首先我们写一个抽象类,用来定义好我们需要的抽象方法和接口,为了方便调用,我们为这个类定义了一个静态的构造器,用来实例化该抽象类调用的真正的Provider的实现,并通过Instance()返回其实例化后的对象,我们就可以通过BlogDataProvider.Instance()来调来所用到的实例了

 
   
None.gif      public   abstract    class  BlogDataProvider:ProviderBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static BlogDataProvider _provider = null;
InBlock.gif        
private static CusProviderCollection<BlogDataProvider> _providers = null;
InBlock.gif        
private static object _lock = new object();
InBlock.gif
InBlock.gif        
static BlogDataProvider()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CreateDefaultCommonProvider();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public static BlogDataProvider Instance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return _provider;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void CreateDefaultCommonProvider()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_provider ==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock (_lock)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (_provider == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        ProvidersConfigSection section 
= (ProvidersConfigSection)ConfigurationManager.GetSection("system.web/dataService");
InBlock.gif
InBlock.gif                        
//ProviderSettingsCollection Settings = (ProviderSettingsCollection)section["providers"];
InBlock.gif

InBlock.gif                        _providers 
= new CusProviderCollection<BlogDataProvider>();
InBlock.gif
InBlock.gif                        ProvidersHelper.InstantiateProviders(section.Providers, _providers, 
typeof(BlogDataProvider));
InBlock.gif
InBlock.gif                        _provider 
= _providers["SqlBlogDataProvider"];
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public abstract void AddBlog(DHBlog blog);
InBlock.gif        
public abstract void UpdateBlog(DHBlog blog);
InBlock.gif
InBlock.gif        
public abstract void DeleteBlog(int id);
InBlock.gif
ExpandedBlockEnd.gif    }

由于我们用的是ProviderHelper.InstatiateProviders来调用配置文件中定义的类,所以我们还要实现其两个参数定义的类,一个是继承自ConfigurationSection用来读
取配置节点的ProvidersConfigSection

 
None.gif      public   class  ProvidersConfigSection : ConfigurationSection
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        [ConfigurationProperty(
"providers")]
InBlock.gif        
public ProviderSettingsCollection Providers
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (ProviderSettingsCollection)base["providers"];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

还有一个就是实现自ProviderCollection的一个Provider的集合类,因为我们要定义多种不同的Provider所以用一个泛型来实现

 
  
None.gif      public   class  CusProviderCollection < T > :ProviderCollection
None.gif        where T:ProviderBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
InBlock.gif        
public new T this[string name]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (T)base[name];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void Add(ProviderBase provider)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (provider == null)
InBlock.gif                
throw new ArgumentNullException("provider");
InBlock.gif            
if (!(provider is T))
InBlock.gif                
throw new ArgumentException
InBlock.gif                      (
"Invalid provider type""provider");
InBlock.gif            
base.Add(provider);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

然后是我们要求的类的功能的具体实现
None.gif   public   class  SqlBlogDataProvider:BlogDataProvider
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private string _applicationName;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public string ConnectonString dot.gifgetset; }
InBlock.gif
InBlock.gif        
public override void Initialize(string name, NameValueCollection config)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (config == null)
InBlock.gif                
throw new ArgumentException("config");
InBlock.gif
InBlock.gif            
if (String.IsNullOrEmpty(name))
InBlock.gif                name 
= "SqlBlogDataProvider";
InBlock.gif
InBlock.gif            
if (string.IsNullOrEmpty(config["description"]))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                config.Remove(
"description");
InBlock.gif                config.Add(
"description""SQL Blog provider");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.Initialize(name, config);
InBlock.gif
InBlock.gif            _applicationName 
= config["applicationName"];
InBlock.gif            
if (string.IsNullOrEmpty(_applicationName))
InBlock.gif                _applicationName 
= "/";
InBlock.gif
InBlock.gif            
string strconn = config["connectionStringName"];
InBlock.gif
InBlock.gif            
if (string.IsNullOrEmpty(strconn))
InBlock.gif                
throw new ProviderException ("Empty or missing connectionStringName");
InBlock.gif
InBlock.gif
InBlock.gif            config.Remove(
"connectionStringName");
InBlock.gif
InBlock.gif            
if (ConfigurationManager.ConnectionStrings[strconn] == null)
InBlock.gif                
throw new ProviderException("Missing connection string");
InBlock.gif            
this.ConnectonString = ConfigurationManager.ConnectionStrings[strconn].ConnectionString;
InBlock.gif
InBlock.gif            
if (String.IsNullOrEmpty(this.ConnectonString))
InBlock.gif                
throw new ProviderException("Empty connection string");
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void AddBlog(DHBlog blog)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataBaseHelper.Insert
<DHBlog>(blog);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void UpdateBlog(DHBlog blog)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override void DeleteBlog(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif}

Provider模式是能过配置文件来定义具体要实例化的类的,我们可以在我们的Web.config加入如下代码
None.gif < configuration >
None.gif  
< configSections >
None.gif    
< sectionGroup  name ="system.web" >
None.gif      
< section  name ="dataService"
None.gif               type
="MyDHServer.Compontents.DataProvider.ProvidersConfigSection,MyDHServer.Compontents"
None.gif               allowDefinition
="MachineToApplication"  
None.gif               restartOnExternalChanges
="true" />
None.gif    
</ sectionGroup >
None.gif
</ configSections >
None.gif
</ configuration >
None.gifdot.gifdot.gifdot.gifdot.gifdot.gif.
None.gif
< system .web >
None.gif
< dataService >
None.gif      
< providers >
None.gif        
< add  name ="SqlBlogDataProvider"
None.gif             type
="MyDHServer.DataProvider.SqlDataProvider.SqlBlogDataProvider,MyDHServer.DataProvider"
None.gif             connectionStringName
="MyDHServerConn"   />
None.gif      
</ providers >
None.gif    
</ dataService >
None.gif
</ system.web >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值