在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
在當前的軟體開發環境中,製作高品質的文件並保證數據完整性是至關重要的任務。 在這篇文章中,我們將探討如何結合強大的 C# 程式庫——Flunt C# 和 IronPDF,以改善數據驗證和文件創建的工作流程。 開發人員可以構建針對各種需求的高效且可靠的解決方案軟體應用程式利用IronPDF的先進PDF製作功能和Fluent的強大驗證能力。
創建一個新的 C# 控制台項目。
從 NuGet 安裝 Flunt 套件。
導入命名空間並繼承類別。
將驗證添加到數據模型中。
這個多功能且輕量的 .NET 框架 Flunt,是為了促進在 C# 應用程式中開發流暢的驗證和通知模式而創建的。 當開發人員使用 Flunt 以流暢且富表達性的方式構建驗證規則和業務邏輯時,代碼將變得更易讀且更易維護。 使用 Flunt 廣泛的整合驗證技術和擴展,開發人員可以輕鬆驗證複雜的數據結構,如物件和集合。
此外,Flunt 是一個有用的工具,可以提高 NET 程式庫應用程式的可靠性和穩健性,因為它可以輕鬆與現有的代碼庫和框架集成。 總的來說,Flunt 鼓勵以宣告式的方法進行驗證和錯誤處理,使開發人員能夠編寫更乾淨、更健壯的代碼。
流暢介面: Flunt 提供一個易讀且簡潔的介面來建立驗證規則,這使得表達複雜的驗證邏輯更加簡單。
可鏈式驗證: 可鏈式驗證場景可以通過自然地連接驗證規則並用少量的代碼來創建。
集成驗證器: Flunt 提供多個內建驗證器,適用於經常使用的數據類型,包括日期、整數、字串和集合。 流暢語法允許輕鬆將這些驗證器應用於屬性。
自訂驗證規則: 透過擴充 Flunt 框架,開發人員可以新增自訂驗證規則,以便根據特定的領域需求調整驗證邏輯。
通知系統: 為了報告驗證問題並收集錯誤訊息,Flunt 提供了一個通知系統。 這使開發人員能夠簡單地通知用戶或其他應用程式元件有關驗證失敗的情況。
與框架整合: Flunt 容易與著名的框架和庫,如 Entity Framework 和 ASP.NET Core 連接,使得在現有項目中添加驗證邏輯變得簡單。
可測試性: Flunt 促進測試驅動開發(測試驅動開發)通過在應用程式代碼和驗證邏輯之間提供明確的劃分,使驗證規則的單元測試變得簡單。
開源與蓬勃發展的社群:一群開發者積極維護Flunt,使其成為開源項目。 這保證了該框架的持續維護、增強和支援。
通知和驗證命名空間是 Flunt 基本類別庫的一部分,應該可以在您的 C# 專案中預設存取。 Flunt通過提供靈活的介面來定義和應用驗證規則,從而加速C#程式的驗證。 其對更清晰代碼、增強可讀性和全面錯誤處理的支持,使驗證用戶輸入、域對象和 API 請求變得更容易。
Flunt 被多種類型的 C# 應用程式實現,包括 Windows 控制台、Web 應用程式和 Windows Forms。(WinForms). 雖然每個框架的實現不同,但一般的概念始終相同。
安裝完成後,您可以立即使用以下程式碼 Flunt。 這是一個簡單的範例,展示如何使用 Flunt 構建驗證規則:
using Flunt.Validations;
static void Main(string[] args)
{
var person = new Person { Name = "Jack", Age = -25 };
var contract = new PersonContract(person);
// validation checks
if (contract.IsValid)
{
Console.WriteLine("Person is valid!");
}
else
{
Console.WriteLine("Validation failed:");
foreach (var notification in contract.Notifications)
{
Console.WriteLine($"- {notification.Key}:{notification.Message}");
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonContract : Contract<Person>
{
public PersonContract(Person person)
{
// ensure the correct format of the object
Requires()
.IsNotNull(person, nameof(person))
.IsNotEmpty(person.Name, nameof(person.Name), "Name is required")
.IsGreaterThan(person.Age, 0, nameof(person.Age), "Age must be a positive number");
}
}
using Flunt.Validations;
static void Main(string[] args)
{
var person = new Person { Name = "Jack", Age = -25 };
var contract = new PersonContract(person);
// validation checks
if (contract.IsValid)
{
Console.WriteLine("Person is valid!");
}
else
{
Console.WriteLine("Validation failed:");
foreach (var notification in contract.Notifications)
{
Console.WriteLine($"- {notification.Key}:{notification.Message}");
}
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonContract : Contract<Person>
{
public PersonContract(Person person)
{
// ensure the correct format of the object
Requires()
.IsNotNull(person, nameof(person))
.IsNotEmpty(person.Name, nameof(person.Name), "Name is required")
.IsGreaterThan(person.Age, 0, nameof(person.Age), "Age must be a positive number");
}
}
Person
類別: 與 FluentValidation
的範例中相同。
PersonContract
: 此類別源自Flunt的基本概念Contract.Verifications
。 使用 Requires
方法,構造函數接收一個 Person 物件並提供驗證規則。 Requires
提供一個可鏈結的方法來添加多個驗證。 驗證由方法進行,例如 IsNotNull
、NotEmpty
、HasMinLength
和 IsGreaterThan
。 每個驗證規則都有個性化的錯誤訊息可供使用。
驗證: 類似於 FluentValidation.Together
與物件的範例,它創建了一個 PersonContract
實例和一個 Person 物件。 驗證結果由合約的 Valid 屬性顯示。 根據驗證結果,將顯示成功或失敗的通知以及特定錯誤詳情。
在 C# 應用程式中,Flunt 提供多種操作來進行驗證和通知處理,例如:
建立驗證規則: 要為強制欄位、資料類型、值範圍、最大長度和最小長度等屬性建立驗證規則,請使用流暢接口。
執行驗證:為了確保資料完整性和遵循商業邏輯,需根據預先定義的規則驗證物件。
管理驗證錯誤: 將驗證錯誤作為警報記錄下來,並通過向用戶提供錯誤信息或記錄錯誤以進行故障排除,禮貌地回應這些錯誤。 個性化驗證邏輯 使用獨特的驗證規則來擴展 Flunt,以應對複雜的驗證情況或特定領域需求。
與框架的整合: 為了提高當前應用程式的驗證能力,Flunt 可以無縫整合許多知名的 .NET 框架和庫,包括 Entity Framework、ASP.NET Core 等。
開發人員可以通過將 Flunt 與 IronPDF 相結合,利用兩者技術的優勢,加速 C# 應用程式中的商業邏輯驗證和文件創建。 開發人員可以在使用 Flunt 驗證輸入數據後,使用 IronPDF 創建 PDF 文件,使應用程式更加可靠和使用者友好。
Install-Package IronPdf
瀏覽並從搜索結果中選擇IronPDF套件後,點擊“安裝”按鈕。 安裝和下載將由 Visual Studio 處理。
要了解有關 IronPDF 功能、相容性和其他下載選項的更多資訊,請參閱其NuGet 套件詳情頁面在 NuGet 網站上。
作為替代方案,您可以使用 IronPDF 的 DLL 檔案直接將其包含在您的專案中。 若要獲取包含 DLL 的 ZIP 檔案,請訪問以下網址IronPDF ZIP 下載頁面. 將 DLL 解壓縮後,將其包含到您的專案中。
讓我們創建一個基本的 C# 應用程式,使用 IronPDF 進行 PDF 創建以及 Flunt 進行數據驗證。 在此範例中,我們將使用 Flunt 驗證註冊表單的使用者輸入,並使用 IronPDF 創建包含已驗證使用者資料摘要的 PDF 文件。
Person 類別: 定義了一個擁有名稱和年齡屬性的 Person 類別。 我們在構造函數中使用Flunt的流暢界面,根據預定義的驗證規則驗證Person數據。
生成 Pdf: 定義了一個名為 RenderHtmlAsPdf
的方法,並接受 User 物件作為輸入。 該函式使用IronPDF的HtmlToPdf
類別將用戶註冊摘要的HTML文字渲染成PDF文件。
主方法: 使用範例的 Person 資料,我們在主方法中構建一個 User 類的實例。 接下來,我們使用 Flunt 的 IsValid
屬性來判斷 Person 數據是否合法。 若資料正確,我們將調用IronPdf方法來創建PDF文件。 如果沒有,驗證問題會顯示在控制台上。
我們結合了IronPDF進行PDF生成和Flunt進行資料驗證,開發了一個快速的工作流程,用於在C#應用程式中評估使用者輸入並生成PDF文件。 此方法確保資料完整性,產生專家級品質的文件,並鼓勵撰寫清晰、易讀且可維護的代碼。 若要了解有關IronPDF功能的更多資訊,請參閱文檔頁面. 以下是範例代碼片段。
using IronPdf;
using System;
using System.Linq;
using System.Text;
using Flunt.Validations;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
var person = new Person { Name = "Jack", Age = -25 };
var contract = new PersonContract(person);
if (contract.IsValid)
{
Console.WriteLine("Person is valid!");
}
else
{
sb.Append("<p>Validation failed: </p>");
foreach (var notification in contract.Notifications)
{
sb.Append($"- {notification.Key}: {notification.Message}");
}
}
var renderer = new IronPdf.HtmlToPdf();
//Set HTML content for the page
var pdfDocument = renderer.RenderHtmlAsPdf(sb.ToString());
// save the document
pdfDocument.SaveAs("output.pdf");
//Dispose the render object
renderer.Dispose();
//Display a message
Console.WriteLine("Report generated successfully!");
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonContract : Contract<Person>
{
public PersonContract(Person person)
{
Requires()
.IsNotNull(person, nameof(person))
.IsNotEmpty(person.Name, nameof(person.Name), "Name is required")
.IsGreaterThan(person.Age, 0, nameof(person.Age), "Age must be a positive number");
}
}
}
using IronPdf;
using System;
using System.Linq;
using System.Text;
using Flunt.Validations;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
var person = new Person { Name = "Jack", Age = -25 };
var contract = new PersonContract(person);
if (contract.IsValid)
{
Console.WriteLine("Person is valid!");
}
else
{
sb.Append("<p>Validation failed: </p>");
foreach (var notification in contract.Notifications)
{
sb.Append($"- {notification.Key}: {notification.Message}");
}
}
var renderer = new IronPdf.HtmlToPdf();
//Set HTML content for the page
var pdfDocument = renderer.RenderHtmlAsPdf(sb.ToString());
// save the document
pdfDocument.SaveAs("output.pdf");
//Dispose the render object
renderer.Dispose();
//Display a message
Console.WriteLine("Report generated successfully!");
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public class PersonContract : Contract<Person>
{
public PersonContract(Person person)
{
Requires()
.IsNotNull(person, nameof(person))
.IsNotEmpty(person.Name, nameof(person.Name), "Name is required")
.IsGreaterThan(person.Age, 0, nameof(person.Age), "Age must be a positive number");
}
}
}
以下是上述程式碼的執行輸出:
IronPDF 和 Flunt 是兩個強大的 C# 函式庫,它們結合得很好,可以簡化文檔創建和數據驗證的工作流程。 借助IronPDF的先進PDF製作功能和Flunt的強大驗證能力,開發人員可以為各種應用構建可靠、高效和高品質的解決方案。 Flunt 和 IronPDF 為開發人員提供必要的工具,以創建滿足用戶和利益相關者需求的高品質軟體,無論是開發桌面應用程式、網絡應用程式還是基於雲的解決方案。
在 $749 Lite 套裝中包括一年的軟體支援、永久授權和庫升級。 IronPDF 提供免費授權詳細資訊IronPDF 以獲取有關成本和許可需求的更多詳細信息。 如需有關 Iron Software 程式庫的其他資訊,請造訪Iron Software 官方網站。