业务逻辑层代码探索

web层编码中action的处理比较麻烦,特别是代码量变大,多应用,多人维护等情况下特别困难。

如何摸索下维护简单,复用性强的代码方式很重要。

 

上次写了一篇文章:http://guoba6688-sina-com.iteye.com/blog/747756,简单的处理。

 

这几天和同事讨论,他们提出更优雅的方式,我试着写了下,希望大家指教。

 

解决目标:

 

1、action层代码简单

2、复杂逻辑维护简单,可复用。

 

思路:

 

1、action层将数据封装为DTO

2、调有DTOUtil,传入DTO,DTOUtil会找到配置好的对应handler来处理

3、在handler中调用workstep逐步处理,返回forwardResult

 

包结构:

1、core包  主要是框架类

2、biz.app  点评的处理逻辑

3、app.biz.client  result解晰成forwardresult类,一般在action层

 

试验场景:

 

点评发布

 

主要代码:

 

biz.core 包

 

/**
 * 
 * 描述  在handler上打注释,让dtoutil类通过dto找到对应的handler处理
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.TYPE })
public @interface DTOHandlerAnnotation {

    Class<? extends IDTO>[] queryFor() default {};
}

 

/**
 * 
 * 描述 action 层传值接口,目前想不出什么样方法,暂做标识吧
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IDTO {

    
}

 

/**
 * 
 * 描述 具体的处理dto的类,如发布点评
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IDTOHandler {

    IResult execute(IDTO dto);
}

 

/**
 * 
 * 描述 根据dto找到具体的handler处理
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public class DTOUtil implements InitializingBean{

    
    private static Map<Class<? extends IDTO>,IDTOHandler> map = new HashMap<Class<? extends IDTO>,IDTOHandler>();
    
    private List<IDTOHandler> dtoHandlers;
    
    
    private void init() throws Exception{
        for(IDTOHandler dtoHandler : dtoHandlers){
            DTOHandlerAnnotation annotation 
                = dtoHandler.getClass().getAnnotation(DTOHandlerAnnotation.class);
            if(annotation == null || annotation.queryFor() == null || annotation.queryFor().length == 0){
                continue;
            }
            Class<? extends IDTO>[] dtoClaszs =  annotation.queryFor();
            for(Class<? extends IDTO> dtoClasz : dtoClaszs){
                if(map.containsKey(dtoClasz)){
                    throw new IllegalAccessException("repeat dtoClaszs");
                }
                map.put(dtoClasz, dtoHandler);
            }
        }
    }
    
    
    public static IForwardResult execute(IDTO dto,IParseResult parseResult){
        Assert.isTrue(dto != null);
        Assert.isTrue(map.get(dto.getClass()) != null);
        Assert.isTrue(parseResult != null);
        
        return parseResult.getForwardResult(map.get(dto.getClass()).execute(dto));
        
    }
    
    
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        if(dtoHandlers == null || dtoHandlers.size() == 0){
            throw new IllegalAccessException("daoHandlers list is null");
        }
        init();
    }

    


    public void setDtoHandlers(List<IDTOHandler> daoHandlers) {
        this.dtoHandlers = daoHandlers;
    }
    
    

    
}

  

/**
 * 
 * 描述  资源依赖注入
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IContextResource {

}

 

/**
 * 
 * 描述  将共用的逻辑放入,暂没有
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public abstract class AbstractDAO implements IDTO{

}

 

/**
 * 
 * 描述  返回action跳转result
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IForwardResult {

    String forword();
    
    IResult getResult();
}

 

/**
 * 
 * 描述  解晰result得到forwardResult的接口
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IParseResult {

    IForwardResult getForwardResult(IResult result);
}

 

/**
 * 
 * 描述  返回的数据接口
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IResult {

    Map<String,Object> getResult();
}

 

/**
 * 
 * 描述  分步接口,可以是链式执行
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IWorkStep {

    IResult execute();

    IWorkStep nextWorkStep();
}

 

biz.app 包 点评的应用逻辑

 

/**
 * 
 * 描述  规定处理点评的依赖类
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public interface IPostAppContextResource extends IContextResource{
    
    IAppraisementRepository getAppraisementRepository();

}

 

/**
 * 
 * 描述 发布点评的DTO
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public class PostAppDTO extends AbstractDAO{

    private Appraisement app;
    
    public PostAppDTO(Appraisement app){
        this.app = app;
    }
    
    public Appraisement getAppraisement(){
        return app;   
    }
}

 

其中Appraisement 属于 领域对象包层级

 

/**
 * 
 * 描述  发布点评的处理类
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
@DTOHandlerAnnotation(queryFor={PostAppDTO.class})//这个注释用于DTOUtil做关系映射
public class PostAppDtoHandler implements IDTOHandler,IPostAppContextResource{

    private IAppraisementRepository appraisementRepository;
    
 
    
    
    
    public PostAppDtoHandler(){
        
        
    }
    
    @Override
    public IResult execute(IDTO dto) {
        // TODO Auto-generated method stub
        Assert.isTrue(dto instanceof PostAppDTO);
        Appraisement app = ((PostAppDTO)dto).getAppraisement();
        
        IWorkStep workStep = new ValidateWorkStep(app,new SaveWorkStep(app, this));
        
        return workStep.execute();
    }

    @Override
    public IAppraisementRepository getAppraisementRepository() {
        // TODO Auto-generated method stub
        return appraisementRepository;
    }

    public void setAppraisementRepository(
            IAppraisementRepository appraisementRepository) {
        this.appraisementRepository = appraisementRepository;
    } 

    
}

 

/**
 * 
 * 描述  验证点评的步骤
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public class ValidateWorkStep implements IWorkStep{

    private Appraisement app;
    
    private IWorkStep nextWorkStep;
    
    public ValidateWorkStep(Appraisement app,IWorkStep nextWorkStep){
        this.app = app;
        this.nextWorkStep = nextWorkStep;
    }
    
    
    @Override
    public IResult execute() {
        // TODO Auto-generated method stub
        System.out.println("ValidateWorkStep " + app);
        return nextWorkStep().execute();
    }

    @Override
    public IWorkStep nextWorkStep() {
        // TODO Auto-generated method stub
        return nextWorkStep;
    }
    
    

}

 

/**
 * 
 * 描述 保存点评的步骤
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public class SaveWorkStep implements IWorkStep{

    private IPostAppContextResource postAppContextResource;
    
    private Appraisement app;
    
    public SaveWorkStep(Appraisement app,IPostAppContextResource postAppContextResource){
        this.app = app;
        this.postAppContextResource = postAppContextResource;
    }
    
    @Override
    public IResult execute() {
        // TODO Auto-generated method stub
        app = this.postAppContextResource.getAppraisementRepository().createAppraisement(app);
        
        return new IResult() {
            
            @Override
            public Map<String, Object> getResult() {
                // TODO Auto-generated method stub
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("appraisement", app);
                return map;
            }
        };
    }

    @Override
    public IWorkStep nextWorkStep() {
        // TODO Auto-generated method stub
        return null;
    }

    
}

 

app.biz.client 包  一般在action层编写

 

/**
 * 
 * 描述  发布点评ForwardResult解晰类
 * 通过result来跳转页面
 *
 * @author 锅巴
 * @version 1.0 2010-9-25
 */
public class PostAppParseResult implements IParseResult{

    @Override
    public IForwardResult getForwardResult(final IResult result) {
        // TODO Auto-generated method stub
        return new IForwardResult() {
            
            @Override
            public String forword() {
                // TODO Auto-generated method stub
                return "success";
            }

            @Override
            public IResult getResult() {
                // TODO Auto-generated method stub
                return result;
            }
            
            
        };
    }

    
}

 

 配置文件:app_biz.context.xml

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<bean class="com.my.biz.core.DTOUtil">
		<property name="dtoHandlers">
			<list>
				<bean class="com.my.biz.app.PostAppDtoHandler">
					<property name="appraisementRepository">
						<ref bean="appraisementRepository"/>
					</property>
				</bean>
			</list>
		</property>
	</bean>
	
	<bean id="appraisementRepository" class="com.my.infrastructure.persistence.AppraisementRepository"/>
	

</beans>

 

junit测试

 

public class AppBizTest {

    ApplicationContext context = null;
    
    
    
    @Before
    public void setUp() throws Exception {
        context = new ClassPathXmlApplicationContext(new String[]{"app_biz.context.xml"});
    }
    
    @Test
    public void testAppBiz(){
        Appraisement app = new Appraisement();
        app.setContent("test");
        IDTO dto = new PostAppDTO(app);
        IParseResult parseResult = new PostAppParseResult();
        IForwardResult forwardResult = DTOUtil.execute(dto, parseResult);
        System.out.println(forwardResult.forword());
    }

}

 

还不成熟,请各位指教。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值