在webapp中重用api中的模型

时间:2016-07-12 06:50:24

标签: php database api models

我正在构建API,我发现自己处于一种情况,我希望在我的webapp中重用API中的模型。

[查询]<> [web应用]<> [模型]<> [API]<> [模型]<> [dB]的

在webapp中,我根据API的输出创建模型。 在API中,我根据数据库的输出创建模型。 两个模型彼此相同。

有没有办法可以在没有复制/粘贴模型的情况下正确执行此操作? 或者有没有办法避免使用相同的模型?

编辑: 我的意思是我使用的是PHP-API和PHP-Frontend。 PHP-API和PHP-Frontend中的两个模型彼此相同。

1 个答案:

答案 0 :(得分:0)

您可以在API中创建一个返回模型数据作为响应的服务。这样你就可以构建一个简单的转换器,将模型从PHP改为后端的某种JSON或其他数据结构格式并传输它们。

您只拥有实际模型的一个副本,并会自动生成前端使用的模型。

一个非常简单的例子:

DO {

    $objForm = New-Object System.Windows.Forms.Form #init -> initializing window form

    $objForm.Text = "Input Window v0.5" #name -> the name which will be display in the top border of the window

    $objForm.Size = New-Object System.Drawing.Size(300,250) #size -> set the size of the window

    $objForm.StartPosition = "CenterScreen" #location -> where the window will appear on the screen

    $objForm.FormBorderStyle = 'Fixed3D' #sizing -> fixes the size of the window so you cannot make it bigger

#objForm Keys -> here are the keys 'Enter' and 'Escape (ESC)' defined to the buttons 'OK' and 'Cancel'

    $objForm.KeyPreview = $True

    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") #Enter -> when the key 'Enter' has been pressed read out the input fields and assign them to variables

        {$ParID=$ParIDInbox.Text;$Research_Groups=$ResearchGroupInbox.Text;$Customer=$CustomerInbox.Text;$Projectname=$ProjectnameInbox.Text;$objForm.Close()}}) #variables -> assigns the variables

    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") #Escape (ESC) -> when the key 'Escape (ESC)' has been pressed then the window closes

        {$objFOrm.Close()}}) #windowclose -> closes the window

#OKButton -> creates a button with the value 'OK'

    $OKButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button

    $OKButton.Location = New-Object System.Drawing.Size(75,180) #Location -> where the button is located in the window

    $OKButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button

    $OKButton.Text = "OK" #value -> sets the value of the button to 'OK'

    $OKButton.Add_Click({$ParID=$ParIDInbox.Text;$Research_Groups=$ResearchGroupInbox.Text;$Customer=$CustomerInbox.Text;$Projectname=$ProjectnameInbox.Text;$objForm.Close()}) #variables -> assigns the variables

    $objForm.Controls.Add($OKButton) #adding -> adds the button to the window

#CancelButton -> creates a button with the value 'Cancel'

    $CancelButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button

    $CancelButton.Location = New-Object System.Drawing.Size(150,180) #Location -> where the button is located in the window

    $CancelButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button

    $CancelButton.Text = "Cancel" #value -> sets the value of the button to 'Cancel'

    $CancelButton.Add_Click({$objFOrm.Close()}) #closing -> closes the window after clicked

    $objForm.Controls.Add($CancelButton) #adding -> adds the button to the window

#ParID_Label -> creates a Label for the 'Par ID' input field

    $ParIDLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label

    $ParIDLabel.Location = New-Object System.Drawing.Size(10,10) #Location -> where the label is located in the window

    $ParIDLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label

    $ParIDLabel.Text = "Par ID (6 numbers!)" #value -> sets the value of the Label to 'Par ID (6 numbers)'

    $objForm.Controls.Add($ParIDLabel) #adding -> adds the label to the window

#ParID Input Box -> Input box for the Par ID input

    $ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box

    $ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window

    $ParIDInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox

    $objForm.Controls.Add($ParIDInbox) #adding -> adds the input box to the window

#Research Group Label -> creates a Label for the 'Research Group' input field

    $ResearchGroupLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label

    $ResearchGroupLabel.Location = New-Object System.Drawing.Size(10,50) #Location -> where the label is located in the window

    $ResearchGroupLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label

    $ResearchGroupLabel.Text = "Research Group (3 letters!)" #value -> sets the value of the Label to 'Research Group (3 letters!)'

    $objForm.Controls.Add($ResearchGroupLabel) #adding -> adds the label to the window

#Research Group Input Box -> Input box for the Research Group input

    $ResearchGroupInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box

    $ResearchGroupInbox.Location = New-Object System.Drawing.Size(10,70) #Location -> where the label is located in the window

    $ResearchGroupInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox

    $objForm.Controls.Add($ResearchGroupInbox)  #adding -> adds the input box to the window

#Customer Label -> creates a Label for the 'Customer' input field

    $CustomerLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label

    $CustomerLabel.Location = New-Object System.Drawing.Size(10,90) #Location -> where the label is located in the window

    $CustomerLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label

    $CustomerLabel.Text = "Customer" #value -> sets the value of the Label to 'Customer'

    $objForm.Controls.Add($CustomerLabel) #adding -> adds the label to the window

#Customer Input Box -> Input box for the Customer input   

    $CustomerInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box

    $CustomerInbox.Location = New-Object System.Drawing.Size(10,110) #Location -> where the label is located in the window

    $CustomerInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox

    $objForm.Controls.Add($CustomerInbox) #adding -> adds the input box to the window

#Projectname Label -> creates a Label for the 'Projectname' input field      

    $ProjectnameLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label

    $ProjectnameLabel.Location = New-Object System.Drawing.Size(10,130) #Location -> where the label is located in the window

    $ProjectnameLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label

    $ProjectnameLabel.Text = "Projectname"  #value -> sets the value of the Label to 'Projectname'

    $objForm.Controls.Add($ProjectnameLabel) #adding -> adds the label to the window

#Projectname Input Box -> Input box for the Projectname input

    $ProjectnameInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box

    $ProjectnameInbox.Location = New-Object System.Drawing.Size(10,150) #Location -> where the label is located in the window

    $ProjectnameInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox 

    $objForm.Controls.Add($ProjectnameInbox) #adding -> adds the input box to the window 

    $objForm.Topmost = $True #topmost -> A topmost form is a form that overlaps all the other (non-topmost!) forms!

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$checkInputs =  {
    (($using:ParID -ne 6) -or 
    ($using:Research_Groups -ne 3) -or 
    ($using:Customer -lt 1) -or 
    ($using:Projectname -lt 1)) -and
    !$using:Exit
}

while(Invoke-Command $checkInputs)
{
    exit
}

} while($ParID)

加入你的api:

class Backend_Book {
  public $title;
  public $author;    
}

class Backend_Recipe {
  public $name;
  public $ingredients;
  public $steps;
}

从您的前端致电:

class Api_Get_Domain {
  $domain = load_domain();
  $json = [];
  foreach( $domain as $domain_model ) {
    $json[] = convert_model($domain_model);
  }
  return json_encode($json);
}

然后这个想法是"响应"会是这样的:

$http.get('your/api/domain').then( function( response ) {
  domain = response;
});

现在,在您的前端,您有一个后端使用的域名副本,您不需要更新。显然这是一个非常的基本示例,您可以根据需要向域模型添加尽可能多的信息,但基本思想是让API为API用户提供自己的域。 / p>