使用IBatisNet + Castle 开发DotNet软件

        本文主要讲述我在项目中如何使用IBatisNet + Castle的结构来进行开发的。软件的主框架如下图:
main.jpg    

       我将系统分为五层,分别是Domain,Persistence,Service,Component,Web。系统的分层参考了NPetShop2的结构并稍加简化。下面简单说明一下每一层的功能:

Domain -- 业务实体层
Persistence -- 持久层,负责数据访问
Service -- 服务层,调用持久层方法并实现业务逻辑
Web -- 最终展现给客户的Web界面,调用Service层方法
Componet -- 主要包括一些通用的方法等

      系统使用了Castle.Facilities,Castle.IOC以及IBatisNet,IBatisNet的DataMapper是通过Castle来管理的,下面分别介绍每一部分的配置及使用:

一、IBatisNet的配置
        参见: IBatisNet1.5--配置篇

二、IBatisNet Facility 和 AutomaticTransactionManagement Facility配置
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif
None.gif  
< facilities >
None.gif
None.gif    
< facility  id ="ibatis"  type ="Castle.Facilities.IBatisNetIntegration.IBatisNetFacility, Castle.Facilities.IBatisNetIntegration"   >
None.gif      
< sqlMap  id ="sqlServerSqlMap"  config ="SqlMap.config"   />
None.gif    
</ facility >
None.gif
None.gif    
< facility  id ="transaction"  type ="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement"   />
None.gif
None.gif  
</ facilities >
None.gif
None.gif
</ configuration >

三、IOC的配置
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif
None.gif  
< components >
None.gif    
< component
None.gif            
id ="insititution"
None.gif            service
="FES.Service.Interfaces.IInstitutionService, FES.Service"
None.gif            type
="FES.Service.InsititutionService, FES.Service"   />
None.gif  
</ components >
None.gif
None.gif
</ configuration >

目前IOC的配置还可支持泛型,详细请参考:
福 娃    [Castle]Castle也范型

当这些配置文件都写好之后,我们还需将其加载到Castle容器当中,具体的代码可写在Global.asax文件。
ContractedBlock.gif ExpandedBlockStart.gif Global
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Web;
None.gif
using System.Web.Security;
None.gif
using System.Web.SessionState;
None.gif
None.gif
using Castle.Windsor;
None.gif
using Castle.Core.Resource;
None.gif
using Castle.Windsor.Configuration.Interpreters;
None.gif
None.gif
namespace FES.Web
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Global : System.Web.HttpApplication, IContainerAccessor
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static WindsorContainer container;
InBlock.gif
InBlock.gif        
protected void Application_Start(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            container 
= new WindsorContainer(new XmlInterpreter(new ConfigResource()));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void Application_End(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IContainerAccessor Members#region IContainerAccessor Members
InBlock.gif
InBlock.gif        
public IWindsorContainer Container
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn container; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

这样我们的配置才算完成,然后可以在代码中使用这些组件了。

四、将asp.net页面加入到Castle容器中
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Reflection;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  Castle.Core;
None.gif
using  Castle.Windsor;
None.gif
using  Castle.Windsor.Configuration.Interpreters;
None.gif
None.gif
namespace  FES.Component
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 为将ASP.NET页面添加到Castle容器中而建立的类,所有页面必须继承此类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class PageBase : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected BindingFlags BINDING_FLAGS_SET
InBlock.gif                    
= BindingFlags.Public
InBlock.gif                    
| BindingFlags.SetProperty
InBlock.gif                    
| BindingFlags.Instance
InBlock.gif                    
| BindingFlags.SetField
InBlock.gif                    ;
InBlock.gif
InBlock.gif        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IWindsorContainer container 
= ObtainContainer();
InBlock.gif
InBlock.gif            Type type 
= this.GetType();
InBlock.gif
InBlock.gif            PropertyInfo[] properties 
= type.GetProperties(BINDING_FLAGS_SET);
InBlock.gif
InBlock.gif            
foreach (PropertyInfo propertie in properties)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string pname = propertie.Name;
InBlock.gif
InBlock.gif                
if (container.Kernel.HasComponent(pname))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    propertie.SetValue(
this, container[pname], null);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public IWindsorContainer ObtainContainer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            IContainerAccessor containerAccessor 
=
InBlock.gif
InBlock.gif            HttpContext.Current.ApplicationInstance 
as IContainerAccessor;
InBlock.gif            
if (containerAccessor == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IWindsorContainer container 
= containerAccessor.Container;
InBlock.gif            
if (container == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("HttpApplication 得不到容器的实例");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return container;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
 
五、从Castle容器中得到ISqlMapper的实例
       本系统将IBatisNet交给Castle来管理,所以ISqlMapper的实例也必须从Castle容器中获取,这样Castle才能真正的管理IBatisNet。
       首先我们需要一个得到容器实例的方法:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// Obtain the Windsor container.
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   static  IWindsorContainer GetContainer()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            IContainerAccessor containerAccessor 
= HttpContext.Current.ApplicationInstance as IContainerAccessor;
InBlock.gif
InBlock.gif            
if (containerAccessor == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IWindsorContainer container 
= containerAccessor.Container;
InBlock.gif
InBlock.gif            
if (container == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("HttpApplication 得不到容器的实例");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return container;
ExpandedBlockEnd.gif        }

从容器中获得ISqlMapper实例的方法:
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif        
/// IsqlMapper实例,从Castle容器中获取
InBlock.gif        
/// </summary>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          public   static  ISqlMapper sqlMap  =  (ContainerAccessorUtil.GetContainer())[ " sqlServerSqlMap " as  ISqlMapper;
None.gif        

其中"sqlServerSqlMap"即是在我们配置IBatisNet Facility时指定的。

五、在Service层中使用事务处理
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  FES.Persistence;
None.gif
using  FES.Model;
None.gif
using  FES.Service.Interfaces;
None.gif
None.gif
using  Castle.Services.Transaction;
None.gif
using  Castle.Facilities.IBatisNetIntegration;
None.gif
None.gif
namespace  FES.Service
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [Transactional]
InBlock.gif    [UsesAutomaticSessionCreation]
InBlock.gif    
public class InsititutionService : IInstitutionService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IInstitutionService Members#region IInstitutionService Members
InBlock.gif        
private InstitutionMapDao _institution;
InBlock.gif        
public InsititutionService(InstitutionMapDao institution)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _institution 
= institution;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Transaction(TransactionMode.Requires)]
InBlock.gif        
public void NewDepartmentAndEmployee(Employee emp,int did)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _institution.NewEmployee(emp,did);
InBlock.gif            _institution.NewDepartment(
null);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


写的很简单,希望高手指点。。。
      

转载于:https://www.cnblogs.com/pw/archive/2007/01/04/608524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值