持久数据层框架设计(二)
在很久之前发布了一篇《持久数据层框架设计(一)》后,有不少人关注,如今,总算Release一个稍微完整的测试版出来,提供DLL,并附配置说明。欢迎体验并提出宝贵的建议。
下面将配置文件以例子形式整理如下:
1. 在App.config或Web.config里配置
<configSections>
<section name="Data" type="Zivsoft.Data.ConfigHandler,Zivsoft.Data,Version=3.5.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e"/>
</configSections>
<Data DbType="SQLServer">
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access,Version=3.5.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~\Db\SOAWeb.mdb"/>
<add key="AutoCreateTable" value="false"/>
<add key="AutoCreateDb" value="true"/>
</Access>
<SQLServer type="Zivsoft.Data.SQLServer.SqlServerDbOperator;Zivsoft.Data.SQLServer,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Data Source=Lihua-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Pooling=False"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="true"/>
</SQLServer>
<MySQL type="Zivsoft.Data.MySQL.MYSqlDbOperator;Zivsoft.Data.MYSQL,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="……"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="false"/>
</MySQL>
</Data>
<section name="Data" type="Zivsoft.Data.ConfigHandler,Zivsoft.Data,Version=3.5.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e"/>
</configSections>
<Data DbType="SQLServer">
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access,Version=3.5.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=~\Db\SOAWeb.mdb"/>
<add key="AutoCreateTable" value="false"/>
<add key="AutoCreateDb" value="true"/>
</Access>
<SQLServer type="Zivsoft.Data.SQLServer.SqlServerDbOperator;Zivsoft.Data.SQLServer,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Data Source=Lihua-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Pooling=False"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="true"/>
</SQLServer>
<MySQL type="Zivsoft.Data.MySQL.MYSqlDbOperator;Zivsoft.Data.MYSQL,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="……"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="false"/>
</MySQL>
</Data>
2. 单独文件中配置
如果在单独文件配置,你需要一个新建Data.xml文件在当前目录,或在App_Data目录里。格式如下:
<?xml version="1.0" encoding="utf-8" ?>
<Config>
<!--my area-->
<Data DbType="Access">
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db\SOAWeb.mdb"/>
<add key="AutoCreateTable" value="false"/>
<add key="AutoCreateDb" value="true"/>
</Access>
<SQLServer type="Zivsoft.Data.SQLServer.SqlServerDbOperator;Zivsoft.Data.SQLServer,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Data Source=Lihua-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Pooling=False"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="true"/>
</SQLServer>
</Data>
<Localization loc="en-us">
<en-us>App_Data\Localization\ResourceEn.xml</en-us>
<zh-cn>App_Data\Localization\ResourceCn.xml</zh-cn>
<zh-tw>App_Data\Localization\ResourceTw.xml</zh-tw>
<jp-wh>App_Data\Localization\ResourceJp.xml</jp-wh>
<ko-kr>App_Data\Localization\ResourceKo.xml</ko-kr>
<es-mx>App_Data\Localization\ResourceEs.xml</es-mx>
</Localization>
<Log level="DEBUG">
<LogFile>App_Data\log.log</LogFile>
</Log>
</Config>
<Config>
<!--my area-->
<Data DbType="Access">
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Db\SOAWeb.mdb"/>
<add key="AutoCreateTable" value="false"/>
<add key="AutoCreateDb" value="true"/>
</Access>
<SQLServer type="Zivsoft.Data.SQLServer.SqlServerDbOperator;Zivsoft.Data.SQLServer,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
<add key="ConnectionString" value="Data Source=Lihua-PC\SQLEXPRESS;Initial Catalog=Sample;Integrated Security=True;Pooling=False"/>
<add key="AutoCreateTable" value="true"/>
<add key="AutoCreateDb" value="true"/>
</SQLServer>
</Data>
<Localization loc="en-us">
<en-us>App_Data\Localization\ResourceEn.xml</en-us>
<zh-cn>App_Data\Localization\ResourceCn.xml</zh-cn>
<zh-tw>App_Data\Localization\ResourceTw.xml</zh-tw>
<jp-wh>App_Data\Localization\ResourceJp.xml</jp-wh>
<ko-kr>App_Data\Localization\ResourceKo.xml</ko-kr>
<es-mx>App_Data\Localization\ResourceEs.xml</es-mx>
</Localization>
<Log level="DEBUG">
<LogFile>App_Data\log.log</LogFile>
</Log>
</Config>
3. 配置主要以上两大类。另外需要提一下GAC。
由于程序集都是强命名,你可以将DLL都丢到GAC里面,但注意配置里必须指定完整的信息如下:
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access,Version=1.0.3295.29087,Culture=neutral, PublicKeyToken=3266f10ce3531d0e">
如果不需要扔到GAC里,配置可以简化如下:
<Access type="Zivsoft.Data.Access.AccessDbOperator;Zivsoft.Data.Access">
4. 关于扩展。
由于程序集中Zivsoft.Data.dll含有所有数据操作、ORM等等接口,你可以继承IDbOperator来扩展对其它数据库的支持,如DB2, MySQL, Oracle等。目前我只做了常用的SQLServer和Access。
5. 如何自动生成实体Entity。
程序包里有一个Zivsoft.Data.ORM.Setup.dll,你可以执行里面的接口,指定数据库,自动生成zivsoft.Data.ORM.Entity.dll。
至于细节的实体类(Entity)的如何调用,外键关联等,在这篇不多写了,大家先自己摸索,与所有ORM大同小异,我有空再来完善这篇随笔,希望长期关注,多提建议。哈哈!