AX中自定义复用代码

     经常和志同道合的朋友们抱怨AX集成开发环境做的太丑,太不智能。和VS比起来简直天壤之别。为什么同是微软的产品,差别怎么那么大呢。
     但是今天和朋友张先生一起探讨AX开发环境的时候,他却给我深深的上了一课。。。原来AX还可以这样灵活。其实很多工具性的功能都是可以自己定义的。
     好了,言规正传,不知道大家在撰写代码的时候用过AX的Scripts功能没有。在Scripts中,AX提供了类似与代码模板的功能。里面提供了一部分我们常用的代码模板,比如dowhile,ifelse等等,在开发中为我们提供了很多方便。那么我们能不能自己定义一部分常用的代码模板来简化我们的工作呢,答案当然是肯定。
    我们在做ForM和Report开发时,经常会动态的定义QUERY。由于Query定义和描述比较麻烦(变量多,字段长)。所以如果自己做个代码模板,那么我们将省去很多事情。
   研究代码我们发现,Scripts 功能的实现主要封装在两个类中:EditorScripts和xppSource。EditorScripts实现和用户交互,xppSource则实现代码生成。
   我们在xppSource中实现方法
Source addQuery()
{
    ;

    source 
+= strfmt('  Query query;');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryRun queryrun;');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryBuildDataSource QueryBS;');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryBuildRange QueryBR;');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  ;');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  Query = New query();');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryBS = query.addDataSource(TableNum());');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryBR = QueryBS.addRange(fieldNum(,));');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  QueryBR.Value();');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  queryrun = new QueryRun(query);');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  While (queryrun.next())');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  {');
    source 
+= '\n';
    source 
+= this.indent();

    source 
+= strfmt('  }');
    source 
+= '\n';
    source 
+= this.indent();

    
return source;

}
   在 EditorScripts中调用上面的方法
void Create_method_addQuery(Editor editor)
{
    xppSource xppSource 
= new xppSource(editor.columnNo());
    ;
    editor.insertLines(xppSource.addQuery());

}
   当我们在使用Scripts 功能是,就可以发现我们新增的代码模板已经加入到该功能中了。
   测试新加功能:
    1 :新建JOB
    2 :使用Scripts->Create->method->addQuery

    3 系统自动生成代码
     
   4 剩下的工作就是我们去填充代码实现功能了。

  好了,今天就先写到这里了,其实AX还有很多有用的功能还等我们去发觉,这里我要特别感谢张同学给我的思路。我们一起向他学习。呵呵






posted on 2008-07-14 21:36  dan.li  阅读(233)  评论(0)    收藏  举报

导航