日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Microsoft Blazor Platz.SqlForms開源-使用架構(gòu)生成器設(shè)計(jì)和維護(hù)SQL Server數(shù)據(jù)庫

 碼農(nóng)9527 2021-04-27

當(dāng)您需要為客戶構(gòu)建有效的原型或公司沒有用于企業(yè)發(fā)展的預(yù)算時(shí),您別無選擇,并且需要使用一些捷徑和生活技巧,通常是低代碼或無代碼方法。在這篇文章中,我提出了一種有趣的方法,它關(guān)于如何使用開源庫Platz.SqlForms快速開發(fā)BlazorUI。SqlForms將提供SPA用戶體驗(yàn),并且無需您進(jìn)行任何編碼即可與數(shù)據(jù)庫進(jìn)行通信,您所要做的就是定義提供流暢符號(hào)定義的UI表單。使用嵌入式數(shù)據(jù)庫架構(gòu)設(shè)計(jì)器,可以定義要顯示為UI控件的實(shí)體。

    創(chuàng)建演示項(xiàng)目

    使用VisualStudio,單擊“創(chuàng)建新項(xiàng)目”,然后找到BlazorServerApp。

web

    單擊“下一步”并設(shè)置項(xiàng)目名稱和解決方案名稱,然后單擊“下一步”,然后選擇“.NET5.0”目標(biāo)框架并單擊“創(chuàng)建”。

web

    VisualStudio應(yīng)該為您創(chuàng)建一個(gè)新項(xiàng)目:

web

    添加Platz包

    下一步是添加NuGet軟件包-右鍵單擊項(xiàng)目,然后單擊菜單項(xiàng)“ManageNuGetPackages…”。

    選擇“瀏覽”標(biāo)簽,然后在搜索框中輸入“Platz”。

web

    我們需要安裝“Platz.SqlForms”和“Platz.ObjectBuilder”并通過添加Platz初始化代碼來擴(kuò)展Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
 services.AddRazorPages();
 services.AddServerSideBlazor();
 services.AddSingleton<WeatherForecastService>();
 // Platz
 services.AddPlatzSqlForms();
 services.AddPlatzObjectBuilder();
}123456789復(fù)制代碼類型:[html]

    接下來,我們需要添加一個(gè)項(xiàng)目文件夾,該文件夾將存儲(chǔ)模式和查詢配置–右鍵單擊該項(xiàng)目,然后選擇“添加”,然后選擇“新建文件夾”,然后鍵入“SchemaStore”。

    如何使用Platz對(duì)象構(gòu)建器

    PlatzSchemaDesigner和QueryBuilder是我們添加的無代碼元素,用于簡(jiǎn)化和加快開發(fā)過程。

    使用SchemaDesigner,我們可以直觀地定義數(shù)據(jù)庫表和關(guān)系,并將它們保存為json格式的配置文件。然后應(yīng)用t4模板,我們可以生成C#代碼,該代碼將管理定義的數(shù)據(jù)庫并為Platz動(dòng)態(tài)UI組件提供CRUD操作。

    查詢生成器用于數(shù)據(jù)庫查詢的可視化設(shè)計(jì),以利用SQL的全部功能來檢索數(shù)據(jù)。結(jié)果查詢存儲(chǔ)在配置json文件中,并可用于C#代碼生成。

    T4模板生成的代碼可以本地連接到Platz動(dòng)態(tài)UI組件,因此您無需手動(dòng)編碼。

    設(shè)置Platz無代碼生成器

    要設(shè)置無代碼生成器,我們需要?jiǎng)?chuàng)建兩個(gè)剃刀頁面并將它們注冊(cè)到Shared\NavMenu.razor中,還可以刪除VisualStudio演示頁面(Counter.razor和FetchData.razor)。

    將“SchemaDesigner.razor”添加到“Pages”文件夾中:

@page "/SchemaDesigner"
@using Platz.ObjectBuilder<SchemaComponent StoreDataPath="SchemaStore" DataService="PlatzDemoService" 
  Namespace="PlatzDemo.SchemaStore"
  TargetConnectionString="Server=(localdb)\mssqllocaldb;
  Database=PlatzDemo;Trusted_Connection=True;MultipleActiveResultSets=true" />1234567復(fù)制代碼類型:[html]

    將“QueryDesigner.razor”添加到“Pages”文件夾中:

@page "/QueryDesigner"
@using Platz.ObjectBuilder<QueryComponent SourceSchemaFile="SchemaStore\PlatzDemo.schema.json" 
 StoreDataPath="SchemaStore"
 DataService="PlatzDemoDataContext" Namespace="PlatzDemo.SchemaStore" />123456復(fù)制代碼類型:[html]

    將指向JQuery的鏈接添加到'Pages\_Host.cshtml'中的引導(dǎo)程序:

...<!DOCTYPE html><html lang="en"><head>
 <meta charset="utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <title>Platz.SqlForms.Demo</title>
 <base href="~/" />

 @*Added for Platz*@ <link rel="stylesheet" 
  href="https://cdn./npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" 
  integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" 
  >
 <script src="https://code./jquery-3.5.1.slim.min.js" 
  integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" 
  ></script>
 <script src="https://cdn./npm/popper.js@1.16.1/dist/umd/popper.min.js" 
  integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" 
  ></script>
 <script src="https://cdn./npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" 
  integrity="sha384-w1Q4orYjBQndcko6MimVbzY0tgp4pWB4lZ7lr30WKz0vr/aWKhXdBNmNb5D92v7s" 
  ></script>

 <link href="css/site.css" rel="stylesheet" />
 <link href="Platz.SqlForms.Demo.styles.css" rel="stylesheet" /></head>...12345678910111213141516171819202122232425262728復(fù)制代碼類型:[html]

    并更改“Shared\NavMenu.razor”:

...<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">
 <ul class="nav flex-column">
  <li class="nav-item px-3">
   <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
 <span class="oi oi-home" aria-hidden="true"></span> Home   </NavLink>
  </li>
  <li class="nav-item px-3">
   <NavLink class="nav-link" href="SchemaDesigner">
 <span class="oi oi-list-rich" aria-hidden="true"></span> Admin Schemas   </NavLink>
  </li>
  <li class="nav-item px-3">
   <NavLink class="nav-link" href="QueryDesigner">
 <span class="oi oi-list-rich" aria-hidden="true"></span> Admin Queries   </NavLink>
  </li>
 </ul></div>...123456789101112131415161718192021復(fù)制代碼類型:[html]

    設(shè)計(jì)演示數(shù)據(jù)庫

    現(xiàn)在,讓我們運(yùn)行該應(yīng)用程序,然后單擊“管理模式”頁面。您將看到帶有新架構(gòu)的頁面,該頁面已可以創(chuàng)建。

web

    輸入“PlatzDemo”作為架構(gòu)名稱,然后選擇“使用INT自動(dòng)增量ID”選項(xiàng)。現(xiàn)在,您可以通過單擊綠色的“添加”按鈕來添加表。

    每個(gè)表應(yīng)具有一個(gè)'Id'列,該列標(biāo)識(shí)每個(gè)記錄并允許我們?cè)诒碇g創(chuàng)建關(guān)系。

    對(duì)于每一列,您應(yīng)該指定Name和Type。

    我創(chuàng)建了一個(gè)'Product'表:

web

    現(xiàn)在,我想再添加兩個(gè)表來創(chuàng)建客戶訂單輸入系統(tǒng),您可以在圖上看到添加的表。添加所有表后,單擊“模式”選項(xiàng)卡上的“保存”按鈕。

   web

    可以看到,我從'OrderItem'表中添加了對(duì)Order'和'Product的引用。

    建立查詢

    保存架構(gòu)后,我們可以單擊“管理查詢”菜單,然后使用定義的表來構(gòu)建查詢。

    在下面的屏幕中,您可以看到已設(shè)計(jì)的表已合并以產(chǎn)生訂單項(xiàng)列表頁面所需的輸出。

web

    當(dāng)你關(guān)閉瀏覽器,你應(yīng)該看到的是,項(xiàng)目文件夾“SchemaStore”現(xiàn)在包含文件:“GetOrderItemProductList.json”與查詢定義,“PlatzDemo.schema.json”和“PlatzDemo.schema.migrations.json”以及架構(gòu)定義。

    生成數(shù)據(jù)上下文代碼

    現(xiàn)在我們可以使用t4模板生成數(shù)據(jù)庫ORM代碼。為此,請(qǐng)創(chuàng)建一個(gè)項(xiàng)目文件夾“SchemaServices”,并在其中創(chuàng)建一個(gè)名為“PlatzDemoDataContext.txt”的文本文件,然后打開項(xiàng)目文件“Platz.Config.Link\CopyMe.SchemaStoreDataContext.tt.txt”并將其內(nèi)容復(fù)制到創(chuàng)建文件“PlatzDemoDataContext.txt”。在此文件中,修改Schemajson的路徑:

<# // ================================================================ 
Set JsonStorePath here, relative to solution folder ================================== #>
<#   string JsonStorePath = @"Platz.SqlForms.Demo\SchemaStore\PlatzDemo.schema.json"; #>
<# // =========================================================================
============================================================================== #>12345復(fù)制代碼類型:[html]

    保存文件,然后將其重命名為“PlatzDemoDataContext.tt”–這是VisualStudio可以識(shí)別的t4擴(kuò)展名。每次您對(duì)此文件進(jìn)行更改或保存時(shí)-模板都會(huì)運(yùn)行以生成代碼,因此再次保存-此操作應(yīng)生成“PlatzDemoDataContext.cs”文件,其中包含我們?cè)O(shè)計(jì)的數(shù)據(jù)庫模式的已生成代碼:

// *********************************************************************************************
// This code is auto generated by Platz.ObjectBuilder template, 
// any changes made to this code will be lost
// *********************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Platz.ObjectBuilder;
using Platz.SqlForms;
using PlatzDemo.SchemaStore;

namespace PlatzDemo.SchemaStore
{
 #region Data Context

 public partial class PlatzDemoDataContext : DataContextBase
 {
  protected override void Configure(DataContextSettings settings)
  {
   settings.SetSchema("PlatzDemo");
   settings.SetDriver<SqlJsonStoreDatabaseDriver>();
   settings.MigrationsPath = @"\SchemaStore\PlatzDemo.schema.migrations.json";

   settings.AddTable<Order>();
   settings.AddTable<OrderItem>();
   settings.AddTable<Product>();
  }
 }

 #endregion

 #region Entities

 public partial class Order
 {
  public virtual int Id { get; set; }
  public virtual string ClientName { get; set; }
  public virtual DateTime Created { get; set; }
 }

 public partial class OrderItem
 {
  public virtual int Id { get; set; }
  public virtual int OrderId { get; set; }
  public virtual int ProductId { get; set; }
  public virtual int Qty { get; set; }
 }

 public partial class Product
 {
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  public virtual decimal Price { get; set; }
 }

 #endregion
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758復(fù)制代碼類型:[html]

    為了能夠使用生成的代碼,我們只需要將連接字符串添加到“appsettings.json”文件中:

{
  "ConnectionStrings": {
 "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=PlatzDemo;
  Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
 "LogLevel": {
   "Default": "Information",
   "Microsoft": "Warning",
   "Microsoft.Hosting.Lifetime": "Information"
 }
  },
  "AllowedHosts": "*"
}1234567891011121314復(fù)制代碼類型:[html]

    如您所見,我在此演示中使用了MicrosoftSQLLocalDB。

    創(chuàng)建動(dòng)態(tài)表單

    我們創(chuàng)建一個(gè)“SchemaForms”項(xiàng)目文件夾,并在其中放置動(dòng)態(tài)表單定義代碼:

using PlatzDemo.SchemaStore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Platz.SqlForms.Demo.SchemaForms
{
 public class ProductForm : StoreDynamicEditFormBase<PlatzDemoDataContext>
 {
  protected override void Define(DynamicFormBuilder builder)
  {
   builder.Entity<Product>(e =>
   {
 e.Property(p => p.Id).IsReadOnly();
 e.Property(p => p.Name).IsRequired();
 e.Property(p => p.Price).IsRequired();
 e.DialogButton(ButtonActionTypes.Cancel).DialogButton
 (ButtonActionTypes.Validate).DialogButton(ButtonActionTypes.Submit);
 e.DialogButtonNavigation("ProductList", 
 ButtonActionTypes.Delete, ButtonActionTypes.Cancel, ButtonActionTypes.Submit);
   });
  }
 }

 public class ProductListForm : StoreDataServiceBase<PlatzDemoDataContext>
 {
  protected override void Define(DataServiceFormBuilder builder)
  {
   builder.Entity<Product>(e =>
   {
 e.ExcludeAll();
 e.Property(p => p.Id).IsPrimaryKey();
 e.Property(p => p.Name);
 e.Property(p => p.Price);
 e.ContextButton("Edit", "ProductEdit/{0}").ContextButton
    ("Delete", "ProductDelete/{0}");
 e.DialogButton("ProductEdit/0", ButtonActionTypes.Add);
   });

   builder.SetListMethod(GetProductList);
  }

  public List<Product> GetProductList(params object[] parameters)
  {
   var db = GetDbContext();
   var result = db.Get(typeof(Product)).Cast<Product>().ToList();
   return result;
  }
 }
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051復(fù)制代碼類型:[html]

    現(xiàn)在,我們添加Blazor頁面以呈現(xiàn)產(chǎn)品表單,并在'NavMenu.razor'中注冊(cè)產(chǎn)品列表頁面。

    產(chǎn)品清單剃刀

@page "/ProductList/"
@using Platz.SqlForms.Demo.SchemaForms<h1>Product Edit</h1><FormDataServiceListComponent TForm="ProductListForm" />123456復(fù)制代碼類型:[html]

    產(chǎn)品編輯器

@page "/ProductEdit/{Id:int}"
@using Platz.SqlForms.Demo.SchemaForms<h1>Product Edit</h1><FormDynamicEditComponent TForm="ProductForm" Id="@Id" />@code {
 [Parameter]
 public int Id { get; set; }
}1234567891011復(fù)制代碼類型:[html]

    產(chǎn)品刪除剃刀

@page "/ProductDelete/{Id:int}"
@using Platz.SqlForms.Demo.SchemaForms<h1>Product Delete</h1><FormDynamicEditComponent TForm="ProductForm" Id="@Id" ForDelete="true"/>@code {
 [Parameter]
 public int Id { get; set; }
}1234567891011復(fù)制代碼類型:[html]

    同樣,我們?yōu)?Order'和'OrderItem'表添加動(dòng)態(tài)表單和頁面。

    我們省略了此代碼以減少閱讀時(shí)間。您可以在GitHub上找到完整的代碼。

    測(cè)試應(yīng)用

    當(dāng)我們第一次啟動(dòng)該應(yīng)用程序時(shí),它將自動(dòng)創(chuàng)建SQL數(shù)據(jù)庫并應(yīng)用所有遷移腳本。

    您將能夠在Platz.SqlForms項(xiàng)目文檔中找到有關(guān)如何使用遷移的詳細(xì)信息,該遷移將在更接近最終發(fā)行日期的地方進(jìn)行。

    您現(xiàn)在可以將新產(chǎn)品添加到數(shù)據(jù)庫中,進(jìn)行編輯和刪除。

web

    完整的項(xiàng)目演示還包含用于添加和編輯Orders和OrderItems的功能。

web

    結(jié)論

    在本文中,我們演示了如何使用嵌入式模式設(shè)計(jì)器定義數(shù)據(jù)庫并將其用于動(dòng)態(tài)UI生成。

    模式設(shè)計(jì)器和查詢構(gòu)建器為Platz.SqlForms快速開發(fā)和原型添加了無代碼技術(shù)。我們知道從維護(hù)的角度來看,無代碼可能會(huì)很痛苦,因此這就是我們提出一種新方法的原因-將您的模式或查詢?cè)O(shè)計(jì)保存到j(luò)son,然后您可以使用許多t4模板來生成結(jié)果代碼。

    未來的發(fā)行版可能包含生成EntityFramework數(shù)據(jù)上下文的t4模板,因此從無代碼原型開始的開發(fā)人員將能夠遷移到可靠的MicrosoftORM。

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多