引言
OneCode 3.0作为新一代低代码开发平台,其架构设计围绕"工程模块化"与"UI自主化"两大核心目标展开。本文将从底层接口到上层应用,全面解析OneCode 3.0的技术架构,包括核心工厂类、工程管理接口、数据仓库设计以及动态UI组件系统,并通过关键代码实现展示其设计思想与技术细。
一、架构总览:微内核与插件化设计
OneCode 3.0采用微内核+插件化架构,通过以下核心组件实现高内聚低耦合的系统设计:
- DSMFactory:领域模型接口系统的核心,负责工程元数据管理与多工厂协同ESDClient:工程模块管理的核心交互接口,定义标准化操作契约CustomViewFactory:自主UI系统的工厂类,处理动态视图组件的创建与加载CustomModuleComponent:UI组件的具体实现,支持数据绑定与事件处理RepositoryInst:数据仓库管理,维护实体与表的映射关系
二、核心工厂类:DSMFactory的设计与实现
2.1 单例模式与多工厂协同
DSMFactory采用双重校验锁单例模式,确保全局唯一实例,并初始化多个关键子工厂:
private DSMFactory() { projectCacheManager = ProjectCacheManager.getInstance(); viewManager = ViewManager.getInstance(); repositoryManager = RepositoryManager.getInstance(); aggregationManager = AggregationManager.getInstance(); buildFactory = BuildFactory.getInstance(); // 初始化其他工厂...}
2.2 工程生命周期管理
DSMFactory提供工程的创建、加载、编译等全生命周期管理:
public void reload() throws JDSException { // 清除缓存 projectCacheManager.clean(); // 重新加载工程 initESDProject(); initUserProject(); initDSMProject(); // 重新编译类 recompileClasses();}
三、工程模块管理接口:ESDClient的设计哲学
3.1 接口定位与核心职责
ESDClient作为工程模块管理的核心交互接口,定义了标准化操作契约,向上承接UI层请求,向下对接底层服务。
3.2 核心方法体系解析
3.2.1 工程生命周期管理
// 工程创建与版本控制Project createProject(String projectName, String desc, ProjectDefAccess type) throws JDSException;ProjectVersion createProcessVersion(String projectName) throws JDSException;void activateProjectVersion(String versionId) throws JDSException;// 工程配置管理void updateProjectConfig(String projectId, ProjectConfig config) throws JDSException;void updateDSMConfig(String projectId, DSMProjectConfig config) throws JDSException;
3.2.2 模块动态编译与加载
// 模块创建与编译EUModule createCustomModule(String versionName, String className, Map<String, ?> valueMap) throws JDSException;Set<EUModule> buildPackage(String versionName, String packageName, Map<String, ?> valueMap, ChromeProxy chrome) throws JDSException;// 运行期动态构建<T extends ModuleComponent> EUModule<T> buildDynCustomModule(Class<T> customClass, Map<String, ?> valueMap, boolean save) throws JDSException;
3.2.3 跨模块资源协同
// 多资源类型管理List<FontConfig> getFontByProject(String versionName) throws JDSException;StyleConfig getStyleConfig(String styleConfigId) throws JDSException;ImgConfig buildImgConfig(String projectName, String path) throws JDSException;// API服务发现List<APIPaths> getAPIPathsByProject(String versionName) throws JDSException;List<XUIAPIConfig> searchLocalService(String versionName, String pattern) throws JDSException;
3.3 架构设计亮点
- 接口隔离原则:通过
@MethodChinaName
注解实现业务语义与技术实现的解耦领域驱动设计:大量使用领域对象作为方法参数,确保业务规则内聚扩展性考量:预留ChromeProxy
等扩展点,支持高级特性四、自主UI系统:CustomViewFactory与动态视图生成
4.1 CustomViewFactory的核心能力
CustomViewFactory负责动态视图组件的创建与管理,其核心方法包括:
// 动态编译模块public EUModule dynBuild(String versionName, String className, String code, boolean isSave) throws JDSException { // 代码编译逻辑 JavaFileObject fileObject = new JavaSourceFromString(className, code); Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(fileObject); CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, fileObjects); boolean success = task.call(); // ...}// 创建模块组件public ModuleComponent createModuleComponent(EUModule module, MethodConfig methodAPIBean, Map<String, Object> valueMap) { return new CustomModuleComponent(module, methodAPIBean, valueMap);}
4.2 视图缓存与性能优化
CustomViewFactory实现了视图组件的缓存机制,避免重复创建:
private Map<String, ModuleComponent> componentCache = new ConcurrentHashMap<>();public ModuleComponent getCachedComponent(String key) { return componentCache.get(key);}public void cacheComponent(String key, ModuleComponent component) { componentCache.put(key, component);}
五、UI组件实现:CustomModuleComponent详解
5.1 类结构与核心字段
CustomModuleComponent继承自ModuleComponent,包含丰富的UI组件字段:
public class CustomModuleComponent extends ModuleComponent { private ESDClass esdClass; private Class<?> parentClass; private List<ESDFieldConfig> fieldList; private Component mainComponent; private ToolBar menuBar; private ToolBar bottomBar; private String dataUrl; private ToolBar customToolsBar; private ContextBar contextBar; private MethodConfig methodAPIBean; // ...}
5.2 构造函数与初始化流程
public CustomModuleComponent(EUModule module, MethodConfig methodAPIBean, Map<String, Object> valueMap) { super(module); this.methodAPIBean = methodAPIBean; this.valueMap = valueMap; this.esdClass = module.getEsdClass(); this.parentClass = esdClass.getClazz(); this.fieldList = esdClass.getFieldList(); // 处理模块样式 processModuleStyle(); // 初始化字段配置 initFieldConfig(); // 初始化事件 initModuleEvent();}
5.3 事件处理机制
CustomModuleComponent实现了丰富的事件处理能力:
public void initModuleEvent() { // 添加模块动作 addModuleAction(); // 添加模块事件 addModuleEvent(); // 初始化上下文菜单 fillContextAction(); // 初始化工具栏 fillToolBar();}public void addModuleAction() { if (methodAPIBean != null && methodAPIBean.getActionList() != null) { for (ActionConfig action : methodAPIBean.getActionList()) { addAction(action); } }}
六、数据仓库管理:RepositoryInst的设计与实现
6.1 类结构与核心功能
RepositoryInst继承自DSMInst,主要负责数据仓库管理,维护实体与表的映射关系:
public class RepositoryInst extends DSMInst implements Comparable<RepositoryInst> { public String schema = "fdt"; public String serverUrl = "http://api.radev.cn"; public DSMType dsmType = DSMType.REPOSITORY; public Map<String, TableRef> tableRefMap = new HashMap<>(); public Map<String, EntityRef> entityRefMap = new HashMap<>(); public Set<String> entityNames = new LinkedHashSet<>(); public Set<String> tableNames = new LinkedHashSet<>(); // ...}
6.2 实体与表的映射管理
RepositoryInst维护了实体与数据库表之间的映射关系:
@JSONField(serialize = false)public List<ESDClass> getEntityList() { List<JavaSrcBean> repositoryList = this.getRepositoryInst().getJavaEntities(); List<ESDClass> entityList = new ArrayList<>(); for (JavaSrcBean srcBean : repositoryList) { String className = srcBean.getClassName(); try { Class clazz = ClassUtility.loadClass(className); ESDEntity entity = (ESDEntity) clazz.getAnnotation(ESDEntity.class); if (entity != null) { ESDClass esdClass = BuildFactory.getInstance().getClassManager().getRepositoryClass(className, true); if (!entityList.contains(esdClass)){ entityList.add(esdClass); } } } catch (ClassNotFoundException e) { // 处理异常 } catch (JDSException e) { e.printStackTrace(); } } return entityList;}
6.3 聚合查询支持
RepositoryInst提供了基于注解的聚合查询能力:
@JSONField(serialize = false)public List<ESDClass> getAggBeans(UserSpace userSpace, AggregationType aggregationType) { List<JavaSrcBean> repositoryList = this.getRepositoryInst().getJavaEntities(); List<ESDClass> entityList = new ArrayList<>(); for (JavaSrcBean srcBean : repositoryList) { String className = srcBean.getClassName(); try { Class clazz = ClassUtility.loadClass(className); Aggregation aggregation = (Aggregation) clazz.getAnnotation(Aggregation.class); if (aggregation != null && aggregation.rootClass() != null && (aggregationType == null || aggregation.type().equals(aggregationType))) { // 处理聚合查询 // ... } } catch (ClassNotFoundException e) { // 处理异常 } } return entityList;}
七、核心组件协同关系
7.1 组件交互流程
OneCode 3.0各核心组件之间的交互流程如下:
工程加载流程:
- ESDClient接收工程加载请求调用DSMFactory初始化工程元数据RepositoryInst加载数据仓库配置CustomViewFactory创建初始UI视图
动态模块创建流程:
- ESDClient接收模块创建请求CustomViewFactory动态编译模块代码创建CustomModuleComponent实例缓存视图组件并返回给前端
7.2 组件关系图
graph TD A[ESDClient] -->|调用| B[DSMFactory] A -->|调用| C[CustomViewFactory] B -->|管理| D[ProjectCacheManager] B -->|管理| E[RepositoryManager] E -->|管理| F[RepositoryInst] C -->|创建| G[CustomModuleComponent] G -->|使用| H[ESDClass] C -->|缓存| I[ComponentCache]
八、工程实践与最佳实践
8.1 工程创建与管理
// 创建工程示例ESDClient client = ESDClientImpl.getInstance();ConnectInfo connInfo = new ConnectInfo();connInfo.setUsername("admin");connInfo.setPassword("password");client.connect(connInfo);// 创建新工程Project project = client.createProject("demo-project", "示例工程", ProjectDefAccess.PRIVATE);String projectId = project.getProjectId();// 创建工程版本ProjectVersion version = client.createProcessVersion(projectId);String versionId = version.getVersionId();// 激活工程版本client.activateProjectVersion(versionId);
8.2 动态模块创建
// 创建动态模块示例Map<String, Object> params = new HashMap<>();params.put("name", "user-list");params.put("title", "用户列表");params.put("dataUrl", "/api/users");EUModule module = client.createCustomModule(versionId, "UserListModule", params);// 构建模块UIModuleComponent component = client.getCustomModule("UserListModule", versionId, params);// 渲染组件String html = component.render();response.getWriter().write(html);
8.3 性能优化建议
- 合理使用缓存:利用CustomViewFactory的组件缓存机制批量操作:使用buildPackage方法批量编译模块按需加载:利用ESDClient的loadModules方法按需加载模块数据库连接池:配置合适的数据库连接池参数
九、总结与展望
OneCode 3.0通过微内核+插件化架构,结合动态编译、注解驱动、数据驱动等技术手段,构建了一个高度灵活和可扩展的低代码开发平台。核心工厂类DSMFactory协调各子系统,ESDClient提供标准化接口,CustomViewFactory和CustomModuleComponent实现自主UI系统,RepositoryInst管理数据仓库,共同构成了OneCode 3.0的技术基石。
未来,OneCode 3.0将在以下方向持续优化:
- 引入AI辅助开发,提升自动化程度增强微前端支持,实现更细粒度的模块拆分优化编译性能,缩短模块加载时间完善插件生态,支持更多行业场景
通过不断技术创新,OneCode 3.0将持续为开发者提供更高效、更灵活的低代码开发体验。