本文介绍了使用方面作为从应用程序逻辑中移除防御性检查的方法是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道你是否认为这是一个好主意,请执行以下操作。



而不是:

  public void buyItem(int itemId,int buyerId){ 
if(itemId< = 0){

throw new IlleglArgumentException(itemId must be positive);
}
if(buyerId< = 0){

throw new IlleglArgumentException(buyerId must be positive);
}
//购买逻辑
}

有类似的东西:

  @Defensive(isPositive(#itemId,#buyerId))
public void buyItem int itemId,int buyerId){
// buy logic
}

你认为这是好/可怕/太花哨/太慢?
如果你真的认为它的好,我在想使用来实现它,任何人都有更好/更轻/更快的东西吗?



谢谢,

语句(。



您似乎要引入自己的一组注释,项目中的其他开发人员必须接受并适应。但是,一旦您引入了注释,似乎常见的代理方法将是解决问题的更好的候选人。



总而言之:您描述的问题可以使用标准的java替代方案轻松解决,但在更复杂的情况下可能是合理的(请考虑 @Secured 在spring-security中),特别是如果您正在开发自己的框架


Kind of a long title, but that is generally the question.

I want to know if you think its a good idea to do the following.

Instead of:

public void buyItem(int itemId, int buyerId) {
    if (itemId <= 0) {

        throw new IlleglArgumentException("itemId must be positive");
    }
    if (buyerId <= 0) {

        throw new IlleglArgumentException("buyerId must be positive");
    }
    // buy logic
}

I want to have something like:

@Defensive("isPositive(#itemId, #buyerId)")
public void buyItem(int itemId, int buyerId) {
    // buy logic
}

Do you think this is good/terrible/too fancy/too slow ?If you actually think its good I was thinking of using SpEL to implement it, does anyone have something better/lighter/faster in mind ?

Thanks,

解决方案

It's not necessarily a bad thing however your simple case can well be solved with exception statements (rather than assertions which I initially mentioned).

It seems you're introducing your own set of annotations which other developers in your project must accept and adapt to. However, once you've introduced annotations, it seems like a regular Proxy approach would be a better candidate to solve the issue.

To sum up: The problem you described can easily be solved using standard java alternatives, although in a more complex case it might be justified (consider e.g., @Secured in spring-security), especially if you're developing your own framework.

这篇关于使用方面作为从应用程序逻辑中移除防御性检查的方法是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:35