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

wordpress卡密网站源码wordpress 文章 新窗口

wordpress卡密网站源码,wordpress 文章 新窗口,凡科互动电话,网站开发培训教程在DDD中#xff0c;DTO(数据传输对象)-BO(业务对象)、BO(业务对象)-PO(持久化对象#xff0c;有的叫DO#xff0c;即和数据表映射的实体)等等情况要做转换#xff0c;这里提供以下转换方式 1、from或者try_from trait实现对象转换 需要转换对象满足接收对象的所有…在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: OptionString,// 验证码pub verify_code: OptionString,// 收货地址pub receive_address: VecReceiveAddress, }通过实现from trait可以从Model转换为Customer impl FromModel 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 FromModel 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: OptionString,// 验证码pub verify_code: OptionString,// 收货地址pub receive_address: VecReceiveAddress, } 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: OptionString) - Self {self.avatar avatar;self}pub fn verify_code(mut self, verify_code: OptionString) - Self {self.verify_code verify_code;self}pub fn receive_address(mut self, receive_address: VecReceiveAddress) - 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(123qq.com.to_string());println!({:?}, customer);//Customer { user_id: 123, username: 张三, email: 123qq.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 SetAuthority 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, SetAuthority 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: OptionString,// 验证码verify_code: OptionString,// 收货地址receive_address: VecReceiveAddress, } // 建造(者结构体包含一个需要构建的对象 #[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: OptionString) - Self {self.customer.avatar avatar;self}pub fn verify_code(mut self, verify_code: OptionString) - Self {self.customer.verify_code verify_code;self}pub fn receive_address(mut self, receive_address: VecReceiveAddress) - 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宏的存在会初始化默认值这种用法和第二种链式调用方式相比每次创建新对象对象无法修改只能创建新对象使用对象会消耗对象适合创建值对象、响应DTO、Event因为这些对象用完就会被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(123qq.com.to_string()).build();println!({:?}, customer);// Customer { user_id: , username: , email: 123qq.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: OptionString,// 验证码verify_code: OptionString,// 收货地址receive_address: VecReceiveAddress, } // 建造(者结构体包含一个需要构建的对象 #[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: OptionString) - mut Self {self.customer.avatar avatar;self}pub fn verify_code(mut self, verify_code: OptionString) - mut Self {self.customer.verify_code verify_code;self}pub fn receive_address(mut self, receive_address: VecReceiveAddress) - 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(123qq.com.to_string()).build(); println!({:?}, customer); //Customer { user_id: 123, username: 张三, email: 123qq.com, password: , avatar: Some(https://www.baidu.com), verify_code: None, receive_address: [] }
http://www.w-s-a.com/news/5803/

相关文章:

  • 网站百度屏蔽关键词杭州排名优化公司
  • h5响应式网站模板下载wordpress鼠标指针
  • 摄影作品投稿网站目前最好的引流推广方法
  • 资源站源码永久dede网站搬家 空间转移的方法
  • 网站建设销售的技巧话语it培训机构
  • 自建本地网站服务器wordpress南充房产网最新楼盘最近房价
  • 郑州代做网站天津哪里能做网站
  • 网站如何做排名网站建设项目的工作分解
  • 洛阳网络建站公司网站开发主流语言
  • 广州各区正在进一步优化以下措施seo值是什么意思
  • 滨州建网站公司京东云 wordpress
  • 网站视频背景怎么做免费的网络推广有哪些
  • 申请网站怎样申请广西壮族自治区专升本业务系统
  • 写作网站哪个网站做ic外单好
  • 苏州和城乡建设局网站撸撸撸做最好的导航网站
  • 网站被同行抄袭怎么办深圳中装建设集团
  • 建站及推广瓦房店 网站建设
  • 怎么查网站是在哪里备案的广州电力建设有限公司网站
  • 做网站自己申请域名还是对方wordpress管理地址
  • 专门做二手书网站或appwordpress首页显示特定分类文章
  • 无锡网站设计厂家一建十个专业含金量排名
  • 网站刷链接怎么做成都高度网站技术建设公司
  • flash网站模板怎么用xml网站地图生成
  • 英文网站优化群晖wordpress中文
  • saas建站平台源码济南品牌网站建设公司
  • 网站建设一般是用哪个软件网站百度
  • 企业建站的作用是什么南宁公司网站开发
  • 厦门网站建设及维护门户网站开发视频教学
  • 可以做兼职的网站有哪些自动点击器永久免费版
  • 建购物网站怎么建呀网站怎么做中英文交互