业务逻辑层代码探索

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());
    }

}

 

还不成熟,请各位指教。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码评审(Code Review)是指对软件开过程中编写的代码进行检查和审查的过程。它是一种质量保证的方法,旨在发和纠正潜在的问题、提高质量和可维护性。 代码评审的目的是确保代码符合预期的功能需求、设计规范和最佳实践。通过评审,可以发现潜在的错误、漏洞、性能问题、安全隐患等,并提供改进建议。代码评审还有助于团队成员之间的知识共享和技术沟通,促进团队合作和提高整体开发水平。 代码评审可以采用不同的方法和工具,包括但不限于以下几种: 1. 面对面评审:开发人员之间进行实时的代码讨论和审查,可以通过会议、讨论或白板演示等形式进行。 2. 代码静态分析工具:使用自动化工具对代码进行静态分析,检查潜在的问题和违反规范的代码。 3. 代码审查工具:使用专门的代码审查工具,如GitHub、GitLab等版本控制系统中的Pull Request功能,可以方便地进行代码审查和评论。 4. 代码规范检查:使用代码规范检查工具,如ESLint、Checkstyle等,对代码进行规范性检查,确保代码符合一致的编码风格和最佳实践。 在进行代码评审时,可以关注以下几个方面: 1. 功能正确性:代码是否实现了预期的功能需求,是否满足业务逻辑和用户需求。 2. 代码质量:代码是否易读、易懂、易维护,是否符合编码规范和最佳实践。 3. 错误处理:代码是否处理了可能出现的错误情况,是否有适当的异常处理机制。 4. 性能和安全性:代码是否具有良好的性能和安全性,是否存在潜在的性能问题和安全隐患。 5. 可测试性:代码是否易于测试,是否具有良好的单元测试覆盖率。 6. 设计和架构:代码是否符合良好的设计原则和设计模式,是否具有良好的可扩展性和可维护性。 7. 文档和注释:代码是否有清晰的注释和文档,是否易于理解和使用。 通过代码评审,可以提高代码质量、减少错误和维护成本,并促进团队合作和技术交流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值