当前位置:网站首页>Review background management system practice: two packaging styles of request parameters
Review background management system practice: two packaging styles of request parameters
2022-07-22 09:24:00 【InfoQ】
Background



Code abstraction
// Set a global table object of the page
const ruleForm = {
type:"fe",
time:"20200721",
title:"day day up",
status:"draft",
weight:999,
comment:"great",
balabala:" I am redundant parameter 1",
bilibilib:" I am redundant parameter 2"
}
- Create a new operation page parameter
const {type,time,.title,status.weight,comment} = ruleForm // Deconstruct to get value
const getCreateParam = function(){
// ... Omit a series of operations
return {only: "create"}
}
const createParam = getCreateParam() // Get special transmission parameters
const param = {
// Public reference
type,
time,
title,
status,
weight,
comment,
// Special biography
createParam
}
createRequest(param).then(res=> ... )
- Modify operation page parameters
const {type,time,.title,status.weight,comment} = ruleForm
const getEditParam1= function(){
// ...
return {only1: "edit1"}
}
const editParam1 = getEditParam1()
const getEditParam2= function(){
// ...
return {only2: "edit2"}
}
const editParam2 = getEditParam2()
const param = {
// Public reference
type,
time,
title,
status,
weight,
comment,
// Special biography
editParam1,
editParam2
}
editRequest(param).then(res=> ... )
- Audit operation page parameters
const {type,time,.title,status.weight,comment} = ruleForm
const getReviewParam1= function(){
// ...
return {only1: "review1"}
}
const reviewParam1= getReviewParam1()
const getReviewParam2= function(){
// ...
return {only2: "review2"}
}
const reviewParam2= getReviewParam2()
const getReviewParam3= function(){
// ...
return {only3: "review3"}
}
const reviewParam3= getReviewParam3()
const param = {
// Public reference
type,
time,
title,
status,
weight,
comment,
// Special biography
reviewParam1,
reviewParam2,
reviewParam3
}
reviewRequest(param).then(res => ...)
1. Object oriented encapsulation
class CommonParam {
constructor(){
this.ruleForm = ruleForm
}
get(){
const {type,time,title,status,weight,comment} = this.ruleForm
return{type,time,title,status,weight,comment}
}
}
class CreateParam extends CommonParam {
getCreateParam(){
// ... Omit a series of operations
return {only: "create"}
}
get(){
const para = super.get()
return {...para,...this.getCreateParam()}
}
}
let creatParam = new CreateParam().get()
createRequest(creatParam).then(res=> ... )
class EditParam extends CommonParam {
getEditParam1(){
return {only1: "edit1"}
}
getEditParam2(){
return {only2: "edit2"}
}
get(){
const para = super.get()
return {...para,...this.getEditParam1(),...this.getEditParam2()}
}
}
let editParam = new EditParam().get()
editRequest(editParam).then(res=> ... )
class ReviewParam extends CommonParam {
getReviewParam1(){
return {only1: "review1"}
}
getReviewParam2(){
return {only2: "review2"}
}
getReviewParam3(){
return {only3: "review3"}
}
get(){
const para = super.get()
return {...para,...this.getReviewParam1(),...this.getReviewParam2(),...this.getReviewParam3()}
}
}
let reviewParam = new ReviewParam().get()
reviewRequest(reviewParam).then(res=> ... )
- Code style is consistent ;
- class One more benefit , The code presents a block ,Declaration and invocation are separate, This is important ;
- Of course, it is also conducive to subsequent expansion 、 reform . Top down procedural code is written too much , It will be difficult to read and debugger.
2. Functional programming encapsulation
- Easy version compose The function code is as follows :
export const compose = function(...fns) {
return function composed(result) {
// Copy an array of stored functions
var list = fns.slice()
while (list.length > 0) {
// Take the last function from the end of the list and execute it
result = list.pop()(result)
if (result === 'breakCompose') { // * Any step renturn "breakCompose" You can end the process in advance ;
break
}
}
return result
}
}
/****** Function declaration part ******/
/*
* Common parameters
*/
const step_getCommonParam = function(ruleForm){
const {type,time,title,status,weight,comment} = ruleForm
return{type,time,title,status,weight,comment}
}
/*
* Create step function
*/
const step_getCreateParam= function(args){
// ... Omit a series of operations
return {...args,only: "create"}
}
const step_createRequest = function(creatParam){
return createRequest(creatParam).then(res=> ... )
}
/*
* Edit step function
*/
const getEditParam1= function(args){
// ...
return {...args,only: "edit1"}
}
const getEditParam2= function(args){
// ...
return {...args,only: "edit2"}
}
const step_editRequest = function(editParam){
return editRequest(editParam).then(res=> ... )
}
/*
* Audit step function
*/
const getReviewParam1= function(args){
return {...args,only1: "review1"}
}
const getReviewParam2= function(args){
return {...args,only1: "review2"}
}
const getReviewParam3= function(args){
return {...args,only1: "review3"}
}
const step_reviewRequest = function(reviewParam){
return reviewRequest(reviewParam).then(res=> ... )
}
/****** Function call part ******/
const handleCreate = (...args) => {// Create operation
const steps = [step_createRequest, step_getCreateParam, step_getCommonParam] // The steps are read from right to left
compose(...steps)(...args)
}
handleCreate(ruleForm)
const handleEdit = (...args) => {// Edit operation
const steps = [step_editRequest, step_getEditParam2,step_getEditParam1, step_getCommonParam]
compose(...steps)(...args)
}
handleEdit(ruleForm)
const handleReview = (...args) => {// Audit operation
const steps = [step_reviewRequest,step_getReviewParam3,step_getReviewParam2,step_getReviewParam1,step_getCommonParam]
compose(...steps)(...args)
}
handleReview(ruleForm)
Review Summary
边栏推荐
- jupyter notebook中切换虚拟环境
- What's the use of regular ^ $
- String STR = new string ("ABC") how many variables are created?
- Analysis of copyonwritearraylist
- 调整数组顺序使奇数位于偶数前面
- List to string, remove before and after []
- Chrome插件开发教程
- Tencent cloud free upgrade.
- Implementation of recommendation system collaborative filtering in spark
- OpenGL drawing coordinate axis indicator
猜你喜欢
洛阳综合保税区正式获国务院批复同意设立
Configure opencv in QT
数学运算
Axure 中继器
Array of C language
Leetcode-12: integer to Roman numeral
c语言之指针(四)
jupyter notebook中切换虚拟环境
position: -webkit-sticky; /* for Safari */ position: sticky;
1978-2021 China Statistical Yearbook, 2003-2019 (provincial and municipal panel data), 1999-2019 (county panel data)
随机推荐
Failed to push the solo article to the community side
c语言之指针(三)
Array of C language
Pointer of C language (4)
flex布局
go-zero微服务实战系列(三、API定义和表结构设计)
Axure 中继器
Documentation (part108) -- distributive robot and multi objective nonnegative matrix factorization
Parallelism and concurrency
Is it safe for Huatai to scan the code to open an account? How to open a low commission account
编辑器公式
包装类(类型之间的相互转化)
jupyter notebook中切换虚拟环境
快乐数~~~(其实我一点都不快乐) && 丑数
队列(堆栈)
EasyCVR平台V2.5.0版本及以上如何配置WebRTC协议实现低延迟播放?
EasyCVR平台如何实现无需鉴权即可接入特殊设备?
The multi indicator data of 4000+ listed companies' provinces, cities and operations have been updated to 2022.1
The request was rejected because the URL contained a potentially malicious String “//“
Tens of millions of data: from 2000 to 2019, enterprise registration data longitude and latitude, registration number and other multi indicator information of provinces, cities and counties in China