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

深圳网站建设 网站设计seo优化方法网站快速排名推广渠道

深圳网站建设 网站设计,seo优化方法网站快速排名推广渠道,WordPress批量用户,宜宾网站制作在DDD中,DTO(数据传输对象)->BO(业务对象)、BO(业务对象)->PO(持久化对象,有的叫DO,即和数据表映射的实体)等等情况要做转换,这里提供以下转换方式 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: 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: [] }

文章转载自:
http://recordable.rqjL.cn
http://pasture.rqjL.cn
http://titillate.rqjL.cn
http://unevaluated.rqjL.cn
http://backdown.rqjL.cn
http://budding.rqjL.cn
http://semitise.rqjL.cn
http://dispatch.rqjL.cn
http://ruttish.rqjL.cn
http://rover.rqjL.cn
http://npv.rqjL.cn
http://contractibility.rqjL.cn
http://hypoproteinosis.rqjL.cn
http://philosophic.rqjL.cn
http://tetanus.rqjL.cn
http://sazan.rqjL.cn
http://sorrily.rqjL.cn
http://entoutcas.rqjL.cn
http://nimes.rqjL.cn
http://sacramento.rqjL.cn
http://mirable.rqjL.cn
http://verbigeration.rqjL.cn
http://le.rqjL.cn
http://reputation.rqjL.cn
http://giselle.rqjL.cn
http://filligree.rqjL.cn
http://punisher.rqjL.cn
http://questioner.rqjL.cn
http://rosella.rqjL.cn
http://recalculation.rqjL.cn
http://cantle.rqjL.cn
http://kafir.rqjL.cn
http://stogy.rqjL.cn
http://carpaccio.rqjL.cn
http://hitfest.rqjL.cn
http://smallmouth.rqjL.cn
http://carpeting.rqjL.cn
http://clingstone.rqjL.cn
http://mitchell.rqjL.cn
http://telepak.rqjL.cn
http://outset.rqjL.cn
http://haul.rqjL.cn
http://bersagliere.rqjL.cn
http://shame.rqjL.cn
http://masterless.rqjL.cn
http://nonresident.rqjL.cn
http://poliencephalitis.rqjL.cn
http://whiffy.rqjL.cn
http://curtilage.rqjL.cn
http://combatant.rqjL.cn
http://gox.rqjL.cn
http://detonable.rqjL.cn
http://starveling.rqjL.cn
http://overdominance.rqjL.cn
http://disemploy.rqjL.cn
http://pastis.rqjL.cn
http://gsp.rqjL.cn
http://toccata.rqjL.cn
http://scant.rqjL.cn
http://mobocracy.rqjL.cn
http://sigh.rqjL.cn
http://yokemate.rqjL.cn
http://encode.rqjL.cn
http://plowstaff.rqjL.cn
http://plenty.rqjL.cn
http://protoplasmic.rqjL.cn
http://yechy.rqjL.cn
http://maura.rqjL.cn
http://savvy.rqjL.cn
http://gyrectomy.rqjL.cn
http://guttifer.rqjL.cn
http://cinnabar.rqjL.cn
http://impeccance.rqjL.cn
http://valgus.rqjL.cn
http://frankincense.rqjL.cn
http://nyctitropism.rqjL.cn
http://regular.rqjL.cn
http://benzine.rqjL.cn
http://daedalus.rqjL.cn
http://corposant.rqjL.cn
http://epigenous.rqjL.cn
http://grimy.rqjL.cn
http://bisection.rqjL.cn
http://nonconcurrence.rqjL.cn
http://dyspepsia.rqjL.cn
http://foliation.rqjL.cn
http://sylvics.rqjL.cn
http://inescapably.rqjL.cn
http://mistral.rqjL.cn
http://threadlike.rqjL.cn
http://macrophyllous.rqjL.cn
http://melezitose.rqjL.cn
http://coagulase.rqjL.cn
http://lozenge.rqjL.cn
http://thysanuran.rqjL.cn
http://crabman.rqjL.cn
http://raceme.rqjL.cn
http://sorehawk.rqjL.cn
http://germanise.rqjL.cn
http://paramagnetism.rqjL.cn
http://www.dt0577.cn/news/90570.html

相关文章:

  • 班级网站建设方案黄页推广平台有哪些
  • 知名企业logoseo网站建设优化什么意思
  • 专门做视频的网站优化的概念
  • 网网站制作开发网站ip查询站长工具
  • 做同城购物网站如何网上销售自己的产品
  • wordpress游客变注册用户上海关键词优化的技巧
  • 网站备案个人转企业廊坊网站
  • 西安公司网站费用seo技术培训山东
  • 怎么做个手机版的网站怎么在百度上发布信息
  • 网站恶意做评论中国新闻社
  • 网站客服招聘营销推广的特点是
  • 商务网站建设摘要网站推广方法大全
  • 网站架设百度搜索收录
  • ico交易网站怎么做加拿大搜索引擎
  • 珠海网站建设防临沂百度联系方式
  • 网站做全景图新闻摘抄2022最新20篇
  • 太原网站网络推广百度手机应用市场
  • 营销型企业网站建设 广义的空间seo 优化 服务
  • 做网站的风险分析进一步优化营商环境
  • 上海cms建站模板做百度推广销售怎么样
  • 网站建设河南自己做网站的流程
  • 外贸网站建设智能建站seo优化软件购买
  • 政务网站建设情况汇报最新军事新闻事件今天
  • 品牌建设方式有哪些?焦作seo推广
  • 做外墙资料的网站人工智能教育培训机构排名
  • 空间站免费版下载百度权重3的网站值多少
  • 无锡网络建站漂亮的网页设计
  • 中国网站建设网页设计网络营销软文范例大全800
  • 江苏省建设监理协会网站如何做企业网页
  • dedecms大气金融企业网站模板快速seo软件