Studio
2024.10
False
Studio 用户指南
Last updated 2024年6月28日

最佳实践

为确保编码自动化高效、可维护且可扩展,您需要遵循其最佳实践。本节概述了在您开始利用代码构建自动化解决方案时要记住的关键最佳实践。采用这些最佳实践将帮助您设计编码自动化,尤其是在构建代码结构、实施错误处理策略或创建可重用组件时。

并行性

并行性提高了性能,并且在编码自动化中实现起来更加容易。“遍历”循环可以提高自动化的执行速度。

Nested classes

Coded automations (CS files) don't support using or invoking variables or arguments from nested classes within low-code automations (XAML files). If you try to invoke these type of arguments using an Invoke Workflow File activity, or to create such arguments in XAML files, then you'll receive errors. We recommend using variables and arguments from nested classes only within other CS files.

编码工作流继承

编码工作流继承可提高代码的可重用性和模块性,允许您创建类层次结构,其中子类可以继承并扩展父类的功能。此继承层次结构有助于组织代码,并避免在多个类中复制常见功能。此外,它还简化了维护和更新,因为对父类所做的更改会自动传递给其子类,从而减少了引入错误或不一致的机会。
我们来演示如何使用三个类进行继承:CodedWorkflowParentFile(继承 CodedWorkflow 并包含自定义方法的中间类)和 MyCustomWorkflow 以及 AnotherCustomWorkflow(继承 ParentFile 的派生类)。下表显示了这些类和文件之间的继承方式:
编码自动化描述代码
CodedWorkflow (read-only partial class) 此类是所有编码工作流的基础。它包含继承自 CodedWorkflowBase 类的所有工作流通用的基本方法和属性。在此示例中,所有其他工作流最终都继承自该类。
重要提示:您希望其他工作流继承的文件还必须继承 CodedWorkflow 类,并隐式继承 CodedWorkflowBase 类。这可确保所有工作流继承基本功能并按预期工作。
public partial class CodedWorkflow : CodedWorkflowBase
    {
        public CodedWorkflow()
        {
         //...

    }
}public partial class CodedWorkflow : CodedWorkflowBase
    {
        public CodedWorkflow()
        {
         //...

    }
}
ParentFile (code source file containing an intermediate class and a custom method) 此类继承自 CodedWorkflow,并添加自定义方法和功能。在此示例中,它包含一个名为 CustomMethod 的自定义方法,该方法执行特定操作,例如在 Orchestrator 中启动作业。
public class ParentFile : CodedWorkflow
{
    public void CustomMethod(string processName, string folderPath, 
    StartProcessDtoJobPriority jobPriority, bool resumeOnSameContext, 
    out string jobId)
    {
        // Enter your custom code here.
        // For example, use the StartJob API inside this custom method.
        jobId = system.StartJob(processName, folderPath, jobPriority, resumeOnSameContext);
    }
}public class ParentFile : CodedWorkflow
{
    public void CustomMethod(string processName, string folderPath, 
    StartProcessDtoJobPriority jobPriority, bool resumeOnSameContext, 
    out string jobId)
    {
        // Enter your custom code here.
        // For example, use the StartJob API inside this custom method.
        jobId = system.StartJob(processName, folderPath, jobPriority, resumeOnSameContext);
    }
}
MyCustomWorkflow and AnotherCustomWorkflow (coded workflows that inherit the code source file) 这些类继承自 ParentFile,并通过覆盖方法或提供不同的参数值来进一步自定义工作流。在此示例中,我们有 MyCustomWorkflowAnotherCustomWorkflow,两者都继承 ParentFile
public class MyCustomWorkflow : ParentFile
    {
        [Workflow]
        public void Execute()
        {
            // You can now call CustomMethod from the base class.
            string processName = "YourProcessName";
            string folderPath = "YourFolderPath";
            StartProcessDtoJobPriority jobPriority = StartProcessDtoJobPriority.Normal;
            bool resumeOnSameContext = false;
            string jobId;

            // Call the custom method from the base class.
            CustomMethod(processName, folderPath, 
            jobPriority, resumeOnSameContext, out jobId);
        }
    }public class MyCustomWorkflow : ParentFile
    {
        [Workflow]
        public void Execute()
        {
            // You can now call CustomMethod from the base class.
            string processName = "YourProcessName";
            string folderPath = "YourFolderPath";
            StartProcessDtoJobPriority jobPriority = StartProcessDtoJobPriority.Normal;
            bool resumeOnSameContext = false;
            string jobId;

            // Call the custom method from the base class.
            CustomMethod(processName, folderPath, 
            jobPriority, resumeOnSameContext, out jobId);
        }
    }
总之,由于 ParentFile 继承自 CodedWorkflow,因此任何继承自 ParentFile 的类都会间接继承 CodedWorkflow 的功能和方法。换句话说,MyCustomWorkflowAnotherCustomWorkflow 通过中间类 ParentFile 以及其他自定义类(例如 CustomMethodCodedWorkflow 部分类继承核心功能。

注册自定义服务

要使用自定义逻辑增强编码自动化,您可以注册自定义服务,以便稍后在编码自动化中使用。请访问注册自定义服务,了解如何注册您自己的自定义服务。

“前”和“后”上下文

在测试用例中,“前”和“后”上下文允许您在运行测试用例之前和之后执行某些操作。这些上下文通常用于设置和关闭资源、执行日志记录以及管理测试环境。访问“前”和“后”上下文,了解上下文的行为以及如何实现它们。

技巧和窍门

使用以下提示和技巧设计高效且可靠的编码自动化。这些见解集合可帮助您优化代码,避免错误并最大限度地提高性能。
  • 我们建议您不要对命名空间进行任何更改。
  • 将操作存储在类中,并在整个项目中重用该操作,从而避免重复代码。
  • 您可以删除在设计期间导入但不再需要的命名空间。
  • 如果您需要从多个应用程序获取数据,请将编码自动化的各个阶段分开,这样您就不会混合来自不同来源的数据。

此页面是否有帮助?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath 白色徽标
信任与安全
© 2005-2024 UiPath. All rights reserved.