参考网络文章,并结合现有项目进行学习,熟悉框架结构及其配置。
现有项目和案例项目的结构有一点不同,但大致结构差不多,都包括:
Domain:系统领域,所有数据库实体的对象,数据载体,在其他各个层都要用到。
Persistence:持久层,持久化数据到数据库,其中包括Interface(接口)和mapperDao(实现)
Service:服务层,系统用到的所有业务方法,调用Persistence层,其中包括Implement(实现)和Interface(接口)
Web:页面层(最终与用户交互)
Common:系统公共层,包括公用方法和类
关于配置:
1. 首先需要配置web.config文件,供Castle使用。需要将web.config放在Web应用程序的根目录下,Castle会自动从web.config中读取。
<!--
在<configuration>节点下新值
-->
<
configSections
>
<
section
name
="castle"
type
="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"
/>
</
configSections
>
<
castle
>
<!--
扩展castle,将ibatisnet当作插件和castle相结合
-->
<
include
uri
="file://Config/Facilities.config"
/>
<!--
告诉castel哪个接口由哪个类实现
-->
<
include
uri
="file://Config/Services/Services.config"
/>
<!--
Persistence层的SqlMapDao
-->
<
include
uri
="file://Config/Persistence/Sample.config"
/>
</
castle
>
<
system.web
>
<!--
<pages/>
<httpHandlers>...</httpHandlers>
<httpModules>...</httpModules>
...很多配置
-->
</
system.web
>
2. Facilities.config文件的配置
<
configuration
>
<
facilities
>
<
facility
id
="logging"
type
="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging"
loggingApi
="log4net"
configfile
="log4net.config"
/>
<!--
为Castle 扩展 ibatisnet
-->
<
facility
id
="ibatis"
type
="Castle.Facilities.IBatisNetIntegration.IBatisNetFacility, Castle.Facilities.IBatisNetIntegration"
>
<
sqlMap
id
="SqlServerSqlMap"
config
="SqlMap.config"
/>
<!--
支持多数据源,但是需要配套的SqlMap.config文件和properties.config文件,并且需要在Persistence声明新的ISqlMapper,同时需要修改构造函数,使Castle能够通过IOC进行实例化
<sqlMap id="SqlMapLocal" config="SqlMapLocal.config" />
-->
</
facility
>
<
facility
id
="biztiminghandle"
type
="Castle.Facilities.BizTimingHandle.BizTimingHandleFacility, Castle.Facilities.BizTimingHandle"
/>
<!--
支持业务层事务,为Castle扩展事务处理功能
-->
<
facility
id
="transaction"
type
="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement"
/>
</
facilities
>
</
configuration
>
这样castle就可以自动将ibatisnet管理起来了,需要ibatisnet的时候会自动运行ibatisnet.
3. Service.config文件的配置
<
configuration
>
<
components
>
<
component
id
="YourSolution.AaBiz"
service
="YourSolution.Service.IAaBiz,YourSolution.Service"
type
="YourSolution.Service.AaBiz,YourSolution.Service"
/>
<
component
id
="YourSolution.BbBiz"
service
="YourSolution.Service.IBbBiz,YourSolution.Service"
type
="YourSolution.Service.BbBiz,YourSolution.Service"
/>
<!--
其他
-->
</
components
>
</
configuration
>
可以发现:服务(接口)IAaBiz由AaBiz类实现,Castle会自动装配,即在请求需要调用IAaBiz接口中的方法时,会自动实例化AaBiz类,并调用其中相应的方法。
4. iBatisNet的配置文件sqlmap.config的配置
<
sqlMapConfig
xmlns
="http://ibatis.apache.org/dataMapper"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
>
<!--
providers.config 文件的路径,Web Application的项目名称
-->
<
properties
embedded
="YourSolution.Web.Properties.config,YourSolution.Web"
/>
<
settings
>
<
setting
useStatementNamespaces
="false"
/>
</
settings
>
<
providers
resource
="Config/Providers.config"
/>
<
database
>
<!--
其中的参数在Properties.config中设置
-->
<
provider
name
="${provider}"
/>
<
dataSource
name
="DocumentSystem"
connectionString
="${connectionString}"
/>
</
database
>
<
sqlMaps
>
<
sqlMap
embedded
="${root}CnTypeGenMap.xml,${assembly}"
/>
<
sqlMap
embedded
="${root}CnTypeMap.xml,${assembly}"
/>
<!--
其他Map...
-->
</
sqlMaps
>
</
sqlMapConfig
>
以上设置iBatisNet对实体进行数据库操作的sqlmap.
5. dao.config文件的配置
咦?为什么在项目中没有找到dao.config,也没有找到dao的相关配置呢?
参考文章中的dao.config配置:
<
daoConfig
xmlns
="http://ibatis.apache.org/dataAccess"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
>
<
context
id
="SqlMapDao"
default
="true"
>
<
daoSessionHandler
id
="SqlMap"
>
<
property
name
="resource"
value
="sqlMap.config"
/>
</
daoSessionHandler
>
<
daoFactory
>
<
dao
interface
="ATS.Persistence.Interface.IEmployeesDao, ATS.Persistence"
implementation
="ATS.Persistence.MapperDao.EmployeesSqlMapDao, ATS.Persistence"
/>
</
daoFactory
>
</
context
>
</
daoConfig
>
主要是<daoFactory>节点的设置,dao运行时的接口对应的实现类,即:IEmployeesDao 由EmployeesSqlMapDao实现
主要参考:http://hi.baidu.com/tlq_1983/blog/item/28b4aef5edfd9920bc3109c1.html