介绍
为一个API进行定义和实现的分离。
示例
有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。根据配置文件中的配置来决定数据持久化方案是使用SqlServer数据库还是Xml文件。
![](https://i-blog.csdnimg.cn/blog_migrate/15e075fb7f2c36b02b1322857cad6153.png)
MessageModel
MessageProvider
SqlMessageProvider
XmlMessageProvider
MessageProviderCollection
MessageProviderConfigurationSection
Message
Web.config
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<?
xml version="1.0" encoding="utf-8"
?>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
configuration
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
configSections
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
section
name
="MessageProvider"
type
="Pattern.Provider.MessageProviderConfigurationSection, Pattern.Provider"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
configSections
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
MessageProvider
defaultProvider
="SqlMessageProvider"
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
providers
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
add
name
="XmlMessageProvider"
type
="Pattern.Provider.XmlMessageProvider, Pattern.Provider"
connectionStringName
="XmlConnection"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
add
name
="SqlMessageProvider"
type
="Pattern.Provider.SqlMessageProvider, Pattern.Provider"
connectionStringName
="SqlConnection"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
providers
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
MessageProvider
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
connectionStrings
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
add
name
="SqlConnection"
connectionString
="server=.;database=db;uid=sa;pwd=sa"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
add
name
="XmlConnection"
connectionString
="XmlPath"
/>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
connectionStrings
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
configuration
>
Test
运行结果
True
SQL方式,连接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44
OK
[源码下载]