当前位置: 首页 > news >正文

Rust 建造者模式

在DDD中,DTO(数据传输对象)->BO(业务对象)、BO(业务对象)->PO(持久化对象,有的叫DO,即和数据表映射的实体)等等情况要做转换,这里提供以下转换方式

1、from或者try_from trait实现对象转换

需要转换对象满足接收对象的所有字段

客户定义

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Customer {// uuidpub user_id: String,// 用户名pub username: String,// 邮件pub email: String,// 密码pub password: String,// 头像pub avatar: Option<String>,// 验证码pub verify_code: Option<String>,// 收货地址pub receive_address: Vec<ReceiveAddress>,
}

通过实现from trait,可以从Model转换为Customer

impl From<Model> for Customer {fn from(user: Model) -> Self {Customer {user_id: user.user_id,username: user.username,email: user.email,password: user.password,avatar: user.avatar,verify_code: user.verify_code,receive_address: user.receive_address,}}
}

实现了From trait默认自动实现Into trait,你可以通过以下两种方式实现对象转换,Try from trait用法一样,只是在转换失败时可以返回错误

// 使用from方法将Model实例转换为Customer实例
let customer_instance = Customer::from(model_instance);// 或者使用更简洁的into方法,它会自动调用对应的from方法
let another_customer_instance = model_instance.into();

但是这样不够优雅,很多时候DTO并不能满足领域对象的所有字段,数据对象也不能满足领域对象的所有字段,例如以上例子的验证码和收货地址,最初没有数据时需要设置默认值

// 转Bo
impl From<Model> for Customer {fn from(user: Model) -> Self {Customer {user_id: user.user_id,username: user.username,email: user.email,password: user.password,avatar: user.avatar,verify_code: None,receive_address: vec![],}}
}

当下一次从数据库中查到数据需要给收货地址赋值的情况下,这种方案就不适用了,可以使用以下建造者模式

2、链式调用

此时所有字段都是private的,通过builder去赋值

// 注意使用了Default,没有builder的值有默认值
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Customer {// uuidpub user_id: String,// 用户名pub username: String,// 邮件pub email: String,// 密码pub password: String,// 头像pub avatar: Option<String>,// 验证码pub verify_code: Option<String>,// 收货地址pub receive_address: Vec<ReceiveAddress>,
}
impl Customer {// new默认值pub fn new() -> Self {Self {user_id: String::new(),username: String::new(),email: String::new(),password: String::new(),avatar: None,verify_code: None,receive_address: Vec::new(),}}pub fn user_id(mut self, user_id: String) -> Self {self.user_id = user_id;self}pub fn username(mut self, username: String) -> Self {self.username = username;self}pub fn email(mut self, email: String) -> Self {self.email = email;self}pub fn password(mut self, password: String) -> Self {self.password = password;self}pub fn avatar(mut self, avatar: Option<String>) -> Self {self.avatar = avatar;self}pub fn verify_code(mut self, verify_code: Option<String>) -> Self {self.verify_code = verify_code;self}pub fn receive_address(mut self, receive_address: Vec<ReceiveAddress>) -> Self {self.receive_address = receive_address;self}
}

使用

    let customer = Customer::new().user_id("123".to_string()).username("张三".to_string()).email("<EMAIL>".to_string());let customer = customer.avatar(Some("https://www.baidu.com".to_string()));print!("{:?}\n", customer);//Customer { user_id: "123", username: "张三", email: "<EMAIL>", password: "", avatar: Some("https://www.baidu.com"), verify_code: None, receive_address: [] }// 修改原有对象let customer = customer.email("123@qq.com".to_string());println!("{:?}", customer);//Customer { user_id: "123", username: "张三", email: "123@qq.com", password: "", avatar: Some("https://www.baidu.com"), verify_code: None, receive_address: [] }

这种方式容易造成意外修改的传播,不推荐

3、建造者模式实现对象转换

在Java中很简单,加上@Builder注解即可

@Builder
public class User {private UserLastname lastname;private UserFirstname firstname;private UserEmail email;private UserPublicId userPublicId;private UserImageUrl imageUrl;private Instant lastModifiedDate;private Instant createdDate;private Set<Authority> authorities;private Long dbId;private UserAddress userAddress;private Instant lastSeen;public User(UserLastname lastname, UserFirstname firstname, UserEmail email, UserPublicId userPublicId, UserImageUrl imageUrl, Instant lastModifiedDate, Instant createdDate, Set<Authority> authorities, Long dbId, UserAddress userAddress, Instant lastSeen) {this.lastname = lastname;this.firstname = firstname;this.email = email;this.userPublicId = userPublicId;this.imageUrl = imageUrl;this.lastModifiedDate = lastModifiedDate;this.createdDate = createdDate;this.authorities = authorities;this.dbId = dbId;this.userAddress = userAddress;this.lastSeen = lastSeen;}
}

通过builder()使用,通过结尾的build()返回新对象

UserBuilder.email(user.getEmail().value()).firstName(user.getFirstname().value()).lastName(user.getLastname().value()).publicId(user.getUserPublicId().value()).authorities(RestAuthority.fromSet(user.getAuthorities())).build()

Rust实现(值传递)建造者模式

和直接链式调用相比,添加了一个build函数返回新对象

// 注意使用了Default,没有builder的值有默认值
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Customer {// uuiduser_id: String,// 用户名username: String,// 邮件email: String,// 密码password: String,// 头像avatar: Option<String>,// 验证码verify_code: Option<String>,// 收货地址receive_address: Vec<ReceiveAddress>,
}
// 建造(者结构体,包含一个需要构建的对象
#[derive(Default, Clone, Debug)]
pub struct CustomerBuilder {customer: Customer,
}
impl CustomerBuilder {pub fn new() -> Self {// 初始化默认值CustomerBuilder::default()}pub fn user_id(mut self, user_id: String) -> Self {self.customer.user_id = user_id;self}pub fn username(mut self, username: String) -> Self {self.customer.username = username;self}pub fn email(mut self, email: String) -> Self {self.customer.email = email;self}pub fn password(mut self, password: String) -> Self {self.customer.password = password;self}pub fn avatar(mut self, avatar: Option<String>) -> Self {self.customer.avatar = avatar;self}pub fn verify_code(mut self, verify_code: Option<String>) -> Self {self.customer.verify_code = verify_code;self}pub fn receive_address(mut self, receive_address: Vec<ReceiveAddress>) -> Self {self.customer.receive_address = receive_address;self}pub fn build(self) -> Customer {Customer {user_id: self.customer.user_id,username: self.customer.username,email: self.customer.email,password: self.customer.password,avatar: self.customer.avatar,verify_code: self.customer.verify_code,receive_address: self.customer.receive_address,}}
}

使用,没有建造的字段由于Default宏的存在会初始化默认值,这种用法和第二种链式调用方式相比每次创建新对象,对象无法修改,只能创建新对象,使用对象会消耗对象适合创建值对象响应DTOEvent(因为这些对象用完就会被Drop,创建后就不可变)

   let customer_builder = CustomerBuilder::new();let customer = customer_builder.clone().user_id("123".to_string()).username("张三".to_string()).email("<EMAIL>".to_string());let customer = customer.clone().avatar(Some("https://www.baidu.com".to_string()));let customer = customer.clone().build();print!("{:?}\n", customer);// Customer { user_id: "123", username: "张三", email: "<EMAIL>", password: "", avatar: Some("https://www.baidu.com"), verify_code: None, receive_address: [] }// 创建新对象let customer = customer_builder.clone().email("123@qq.com".to_string()).build();println!("{:?}", customer);// Customer { user_id: "", username: "", email: "123@qq.com", password: "", avatar: None, verify_code: None, receive_address: [] }

Rust实现(引用修改)建造者模式

如果不想消耗对象,可以将其字段都设置为&mut,使用clone()是为了返回的新对象是完全独立的副本

// 注意使用了Default,没有builder的值有默认值
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Customer {// uuiduser_id: String,// 用户名username: String,// 邮件email: String,// 密码password: String,// 头像avatar: Option<String>,// 验证码verify_code: Option<String>,// 收货地址receive_address: Vec<ReceiveAddress>,
}
// 建造(者结构体,包含一个需要构建的对象
#[derive(Default, Clone, Debug)]
pub struct CustomerBuilder {customer: Customer,
}
impl CustomerBuilder {pub fn new() -> Self {CustomerBuilder::default()}pub fn user_id(&mut self, user_id: String) -> &mut Self {self.customer.user_id = user_id;self}pub fn username(&mut self, username: String) -> &mut Self {self.customer.username = username;self}pub fn email(&mut self, email: String) -> &mut Self {self.customer.email = email;self}pub fn password(&mut self, password: String) -> &mut Self {self.customer.password = password;self}pub fn avatar(&mut self, avatar: Option<String>) -> &mut Self {self.customer.avatar = avatar;self}pub fn verify_code(&mut self, verify_code: Option<String>) -> &mut Self {self.customer.verify_code = verify_code;self}pub fn receive_address(&mut self, receive_address: Vec<ReceiveAddress>) -> &mut Self {self.customer.receive_address = receive_address;self}pub fn build(&self) -> Customer {Customer {user_id: self.customer.user_id.clone(),username: self.customer.username.clone(),email: self.customer.email.clone(),password: self.customer.password.clone(),avatar: self.customer.avatar.clone(),verify_code: self.customer.verify_code.clone(),receive_address: self.customer.receive_address.clone(),}}
}

使用,这里对象创建后不会消耗对象,可以通过.build()修改并返回新对象,适合创建领域模型如聚合对象

let mut binding = CustomerBuilder::new().clone();
let customer = binding.user_id("123".to_string()).username("张三".to_string()).email("<EMAIL>".to_string());
let customer = customer.avatar(Some("https://www.baidu.com".to_string()));
let customer = customer.build();
print!("{:?}\n", customer);
//Customer { user_id: "123", username: "张三", email: "<EMAIL>", password: "", avatar: Some("https://www.baidu.com"), verify_code: None, receive_address: [] }
// 修改原有对象
let customer = binding.email("123@qq.com".to_string()).build();
println!("{:?}", customer);
//Customer { user_id: "123", username: "张三", email: "123@qq.com", password: "", avatar: Some("https://www.baidu.com"), verify_code: None, receive_address: [] }

相关文章:

Rust 建造者模式

在DDD中&#xff0c;DTO(数据传输对象)->BO(业务对象)、BO(业务对象)->PO(持久化对象&#xff0c;有的叫DO&#xff0c;即和数据表映射的实体)等等情况要做转换&#xff0c;这里提供以下转换方式 1、from或者try_from trait实现对象转换 需要转换对象满足接收对象的所有…...

ANN DNN CNN SNN

这些缩写代表了不同类型的人工神经网络&#xff1a; • ANN&#xff08;Artificial Neural Network&#xff09;&#xff1a;人工神经网络&#xff0c;是模仿人脑神经元之间连接和交互方式的计算模型。它由节点&#xff08;或称为“神经元”&#xff09;组成的网络&#xff0c;…...

go语言进阶之并发模式

并发模式 并发模式是指在程序设计中同时处理多个任务或进程的方式&#xff0c;以提高效率和响应性 for select循环模式 for select循环模式通常用于处理并发操作&#xff0c;尤其是在需要等待多个通道时。 select的执行过程主要是以下几步 阻塞等待&#xff0c;直到其中一…...

Spring Cloud LoadBalancer:负载均衡的服务调用

在微服务系统中,有时候一个服务会部署多个实例,在我们调用这类实例时,如何实现负载均衡的调用呢?这时候就要用到Spring Cloud的负载均衡组件LoadBalancer了 LoadBalancer简介 LoadBalancer是Spring Cloud官方提供的负载均衡组件,通过它能使客户端在多个服务实例之间分发传…...

微信小程序之轮播图

效果图 实现 <swiper class"banner" indicator-dots"true" indicator-color"rgba(255,255,255,1)" indicator-active-color"#ff0000" autoplay"true" interval"100" circular"true"><swi…...

羲和数据集收集器1.3

为了实现所要求的功能,我们需要进一步完善代码,使其能够处理多种格式的输入文件,并生成符合要求的 JSON 格式的输出文件。具体来说,我们完善了以下内容: 增强 extract_qa_pairs_from_content 函数:使其能够识别和处理不同格式的 QA 对。 确保输出文件的格式正确:每个 Q…...

UE--IOS打包失败 AutomationTool exiting with ExitCode=9 (9)

[Remote] Executing build UATHelper: 打包 (IOS): Setting up bundled DotNet SDK UATHelper: 打包 (IOS): /Users/zyh/UE5/Builds/DESKTOP-FKKSVFQ/Y/UE/UE_5.2/Engine/Build/BatchFiles/Mac/../../../Binaries/ThirdParty/DotNet/6.0.302/mac-x64 UATHelper: 打包 (IOS)…...

第8章利用CSS制作导航菜单

8.1 水平顶部导航栏 水平菜单导航栏是应用范围最广的网站导航设计&#xff0c;一般位于页面顶部。它适用性强&#xff0c;几乎适用于所有类型的网站&#xff0c;且设计难度低。若导航过于普通&#xff0c;无法承载复杂信息结构&#xff0c;在内容模块较多时&#xff0c;则需结…...

UNIX网络编程-TCP套接字编程

概述 TCP客户端/服务器程序示例是执行如下步骤的一个回射服务器&#xff1a; 客户端从标准输入读入一行文本&#xff0c;并写给服务器。服务器从网络输入读入这行文本&#xff0c;并回射给客户端。客户端从网络输入读入这行回射文本&#xff0c;并显示在标准输出上。 TCP服务器…...

美团代付微信小程序 read.php 任意文件读取漏洞复现

0x01 产品描述&#xff1a; ‌ 美团代付微信小程序‌是美团点评旗下的一款基于微信小程序技术开发的应用程序功能&#xff0c;它允许用户方便快捷地请求他人为自己支付订单费用。通过微信小程序&#xff0c;用户可以轻松实现代付操作&#xff0c;无需跳转到其他应用或网页…...

centos7 node升级到node18

使用jenkins发布vue3项目提示node18安装失败 错误日志&#xff1a; /var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/Node18/bin/node: /lib64/libm.so.6: version GLIBC_2.27 not found (required by /var/lib/jenkins/tools/jenkins.plugins.node…...

使用Matlab建立随机森林

综述 除了神经网络模型以外&#xff0c;树模型及基于树的集成学习模型是较为常用的效果较好的预测模型。我们以下构建一个随机森林模型。 随机森林是一种集成学习方法&#xff0c;通过构建多个决策树并结合其预测结果来提高模型的准确性和稳定性。在MATLAB中&#xff0c;可以…...

【江苏-无锡】《无锡市市级政务信息化建设及运行维护项目支出预算标准(试行)》 -省市费用标准解读系列10

2023年3月28日&#xff0c;无锡市政府投资评审中心出台《无锡市市级政务信息化建设及运行维护项目支出预算标准&#xff08;试行&#xff09;》&#xff0c;此标准是为进一步提高财政评审效率&#xff0c;规范评审行为&#xff0c;保证评审质量&#xff0c;节约政府投资&#x…...

2024-11-12 问AI: [AI面试题] 您将如何设计一个人工智能系统来预测电信公司的客户流失?

文心一言 设计一个人工智能系统来预测电信公司的客户流失&#xff0c;是一个复杂但至关重要的任务&#xff0c;它涉及到多个步骤和关键要素。以下是一个详细的设计方案&#xff1a; 一、明确需求和目标 在设计智能系统之前&#xff0c;需要明确系统的需求和目标&#xff0c;…...

【数字静态时序分析】复杂时钟树的时序约束SDC写法

以上图为例&#xff0c;SoC芯片上往往存在几种不同的时钟源&#xff0c;有pll时钟、环振时钟、外部的晶振时钟&#xff0c;在SoC不同的模块或者不同的运行阶段使用的时钟也往往不同&#xff0c;所以在使用的时候&#xff0c;相同的模块会出现选择不同的时钟源的情况。上图的情形…...

springboot苍穹外卖实战:五、公共字段自动填充(aop切面实现)+新增菜品功能+oss

公共字段自动填充 不足 比起瑞吉外卖中的用自定义元数据类型mybatisplus的实现&#xff0c;这里使用的是aop切面实现&#xff0c;会麻烦许多&#xff0c;建议升级为mp。 定义好数据库操作类型 sky-common中已经定义好&#xff0c;OperationType。 自定义注解 AutoFill co…...

Go 语言中,golang结合 PostgreSQL 、MySQL驱动 开启数据库事务

Go 语言中&#xff0c;golang结合 PostgreSQL 、MySQL驱动 开启数据库事务 PostgreSQL代码说明&#xff1a; MySQL代码说明&#xff1a; PostgreSQL 在 Go 语言中&#xff0c;使用 database/sql 包结合 PostgreSQL 驱动&#xff08;如 github.com/lib/pq&#xff09;可以方便地…...

Git核心概念

目录 版本控制 什么是版本控制 为什么要版本控制 本地版本控制系统 集中化的版本控制系统 分布式版本控制系统 认识Git Git简史 Git与其他版本管理系统的主要区别 Git的三种状态 Git使用快速入门 获取Git仓库 记录每次更新到仓库 一个好的 Git 提交消息如下&#…...

网络安全技术在能源领域的应用

摘要 随着信息技术的飞速发展&#xff0c;能源领域逐渐实现了数字化、网络化和智能化。然而&#xff0c;这也使得能源系统面临着前所未有的网络安全威胁。本文从技术的角度出发&#xff0c;探讨了网络安全技术在能源领域的应用&#xff0c;分析了能源现状面临的网络安全威胁&a…...

这些场景不适合用Selenium自动化!看看你踩过哪些坑?

Selenium是自动化测试中的一大主力工具&#xff0c;其强大的网页UI自动化能力&#xff0c;让测试人员可以轻松模拟用户操作并验证系统行为。然而&#xff0c;Selenium并非万能&#xff0c;尤其是在某些特定场景下&#xff0c;可能并不适合用来自动化测试。本文将介绍Selenium不…...

LogExpert深度解析:企业级日志分析平台的架构设计与实战应用

LogExpert深度解析&#xff1a;企业级日志分析平台的架构设计与实战应用 【免费下载链接】LogExpert Windows tail program and log file analyzer. 项目地址: https://gitcode.com/gh_mirrors/lo/LogExpert LogExpert是一款面向Windows平台的专业级日志分析工具&#x…...

3步解锁Heightmapper:从地图到3D地形的终极转换指南

3步解锁Heightmapper&#xff1a;从地图到3D地形的终极转换指南 【免费下载链接】heightmapper interactive heightmaps from terrain data 项目地址: https://gitcode.com/gh_mirrors/he/heightmapper 还在为寻找真实地形数据而烦恼吗&#xff1f;还在为3D建模中的地形…...

生产级MLOps鲁棒性实战:从数据漂移到模型监控的五大平台对比

1. 项目概述&#xff1a;为什么生产级机器学习系统必须关注鲁棒性&#xff1f; 在机器学习项目从实验室走向生产环境的漫长旅途中&#xff0c;我们常常会经历一个“高开低走”的尴尬局面&#xff1a;在精心准备的测试集上表现优异的模型&#xff0c;一旦部署上线&#xff0c;性…...

Kubernetes服务发现与负载均衡机制:构建高效的服务通信网络

Kubernetes服务发现与负载均衡机制&#xff1a;构建高效的服务通信网络 一、服务发现概述 服务发现是微服务架构中服务之间相互定位和通信的核心机制。在Kubernetes中&#xff0c;服务发现通过Service资源实现&#xff0c;它为一组Pod提供稳定的网络标识和负载均衡能力。 1.…...

MacType 2025:3大突破性改进让Windows字体渲染焕然一新

MacType 2025&#xff1a;3大突破性改进让Windows字体渲染焕然一新 【免费下载链接】mactype Better font rendering for Windows. 项目地址: https://gitcode.com/gh_mirrors/ma/mactype 还在为Windows系统下字体模糊、边缘粗糙而烦恼吗&#xff1f;MacType 2025版本带…...

基于开源大模型的自动化定性分析:GATOS工作流实践指南

1. 项目概述&#xff1a;当定性研究遇上开源大模型如果你做过定性研究&#xff0c;比如分析访谈记录、开放式问卷反馈或者社交媒体评论&#xff0c;你肯定对“主题分析”和“编码”这两个词又爱又恨。爱的是&#xff0c;它能让你从海量文本中提炼出深刻的、人性化的洞察&#x…...

3分钟彻底清理Windows右键菜单!ContextMenuManager让你的效率提升200%

3分钟彻底清理Windows右键菜单&#xff01;ContextMenuManager让你的效率提升200% 【免费下载链接】ContextMenuManager &#x1f5b1;️ 纯粹的Windows右键菜单管理程序 项目地址: https://gitcode.com/gh_mirrors/co/ContextMenuManager 你是不是也遇到过这种情况&…...

基于RLHF的论据语言改写:用强化学习优化文本得体性

1. 项目概述与核心价值在互联网的公共讨论空间里&#xff0c;论据的质量直接决定了对话的深度与有效性。我们常常会遇到一些观点本身有价值&#xff0c;但表达方式充满攻击性、偏见或粗俗语言的文本。直接删除这些内容可能损害言论自由&#xff0c;但放任不管又会污染讨论环境&…...

e-cology单点登录token认证失败排查指南

1. 这不是账号被锁&#xff0c;而是认证链路上某个环节“失联”了“e-cology token认证时报错该账号存在异常&#xff0c;单点登录失败”——这句话我去年在客户现场听运维同事念了不下二十遍。它不像“密码错误”或“用户不存在”那样直白&#xff0c;也不像“系统繁忙请稍后再…...

量子机器学习与量子炼金术:加速化学空间探索的DFT数据驱动方法

1. 项目概述&#xff1a;当量子化学遇见机器学习在计算化学和材料科学的日常工作中&#xff0c;我们这些“算分子”的人&#xff0c;最核心也最头疼的任务之一&#xff0c;就是预测一个分子或材料的能量。这听起来简单&#xff0c;却是理解其稳定性、反应活性乃至所有物理化学性…...