透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
現在のソフトウェア開発環境では、高水準のドキュメントを作成し、データの整合性を保証することが不可欠な作業となっています。 この投稿では、強力な C# ライブラリである Flunt C# と IronPDF を組み合わせて、データ検証およびドキュメント作成のワークフローを改善する方法を見ていきます。 開発者は、IronPDFの高度なPDF生成機能とFluntの強力な検証機能を活用することで、さまざまなソフトウェアアプリケーションに対して効果的で信頼性の高いソリューションを構築できます。
新しいC#コンソールプロジェクトを作成します。
NuGetからFluntパッケージをインストールします。
名前空間をインポートして、クラスを継承します。
データモデルにバリデーションを追加します。
多用途で軽量な.NETフレームワークであるFluntは、C#アプリケーションにおける流れの良いバリデーションおよび通知パターンの開発を容易にするために作られました。 コードは、開発者がFluntを使用して流暢で表現力豊かな方法で検証ルールおよびビジネスロジックを構築すると、より読みやすく保守されやすくなります。 Fluntの幅広い統合バリデーション技術と拡張機能を活用することで、開発者はオブジェクトやコレクションのような複雑なデータ構造を簡単に検証できます。
さらに、Fluntは、.NETライブラリアプリケーションの信頼性と堅牢性を高めるための有用なツールです。Fluntは既存のコードベースやフレームワークに簡単に統合できます。 要するに、Fluntは検証とエラーハンドリングにおいて宣言的なアプローチを推奨し、開発者がよりクリーンで堅牢なコードを書くことを可能にします。
フルエントインターフェース: Fluntは、検証ルールを構築するための読みやすく簡潔なインターフェースを提供しており、複雑な検証ロジックの表現を簡素化します。
チェイナブル検証: 自然に検証ルールをつなげることで、少ないコードでチェイナブル検証シナリオを作成できます。
統合バリデーター: Fluntには、日付、整数、文字列、コレクションなど、よく使用されるデータタイプ用のいくつかの組み込みバリデーターが含まれています。 流暢な構文により、これらのバリデーターをプロパティに容易に適用することができます。
カスタム検証ルール: Fluntフレームワークを拡張することで、開発者は特定のドメイン要件に適応した検証ロジックを可能とするカスタム検証ルールを追加できます。
通知システム: 検証の問題を報告し、エラーメッセージを収集するために、Flunt は通知システムを提供します。 これにより、開発者は検証の失敗をユーザーや他のアプリケーションコンポーネントに簡単に知らせることができます。
フレームワークとの統合: Fluntは、Entity FrameworkやASP.NET Coreなどのよく知られたフレームワークやライブラリと簡単に接続でき、既存のプロジェクトに検証ロジックを追加するのが簡単です。
テスタビリティ:Fluntは、アプリケーションコードとバリデーションロジックの間に明確な分離を提供することで、テスト駆動開発(TDD)を促進します。これにより、バリデーションルールをユニットテストすることが簡単になります。
オープンソースと活気あるコミュニティ: Fluntはオープンソースであり、開発者のグループが積極的にメンテナンスしています。 これにより、フレームワークの継続的なメンテナンス、改良、およびサポートが保証されます。
通知およびバリデーションの名前空間はFluntベースクラスライブラリの一部であり、C#プロジェクトでデフォルトでアクセスできるはずです。 Fluntは、柔軟なインターフェースを提供することにより、C#プログラムのバリデーションを迅速化します。これにより、バリデーションルールの定義と適用が容易になります。 よりクリーンなコードのサポート、可読性の向上、徹底したエラーハンドリングにより、ユーザー入力、ドメインオブジェクト、APIリクエストの検証がより容易になります。
Flunt は、Windows コンソール、Web アプリケーション、Windows フォーム (WinForms) を含む多数の C# アプリケーションタイプによって実装されています。 フレームワークごとに実装は異なりますが、一般的な概念は常に同じです。
インストールが完了したら、次のコード 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");
}
}
Imports Flunt.Validations
Shared Sub Main(ByVal args() As String)
Dim person As New Person With {
.Name = "Jack",
.Age = -25
}
Dim contract = New PersonContract(person)
' validation checks
If contract.IsValid Then
Console.WriteLine("Person is valid!")
Else
Console.WriteLine("Validation failed:")
For Each notification In contract.Notifications
Console.WriteLine($"- {notification.Key}:{notification.Message}")
Next notification
End If
End Sub
Public Class Person
Public Property Name() As String
Public Property Age() As Integer
End Class
Public Class PersonContract
Inherits Contract(Of Person)
Public Sub New(ByVal person As 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")
End Sub
End Class
Person
クラス: FluentValidation
の例と同じです。
PersonContract
: このクラスは Flunt の基本コンセプトである Contract.Verifications
から派生しています。 このRequires
メソッドを使用して、コンストラクターはPersonオブジェクトを受け取り、検証ルールを提供します。 Requires
は、複数の検証を追加するためのチェーン可能なメソッドを提供します。 バリデーションは、IsNotNull
、NotEmpty
、HasMinLength
、IsGreaterThan
のようなメソッドによって実行されます。 すべての検証ルールには、個別のエラーメッセージが利用可能です。
バリデーション: オブジェクトに対する FluentValidation.Together
の例と同様に、PersonContract
インスタンスと Person オブジェクトを作成します。 検証結果は契約のValid属性によって示されます。 検証結果に応じて、成功や失敗の通知と特定のエラー詳細が表示されます。
C#アプリケーションの検証および通知処理のために、Fluntは以下のような多くの操作を提供します:
検証ルールの作成: 必須フィールド、データ型、値の範囲、最大長、最小長のような属性の検証ルールを作成するには、流暢なインターフェースを使用します。
検証の実行: データの整合性とビジネスロジックの遵守を保証するために、事前定義されたルールに従ってオブジェクトを検証します。
検証ミスの管理: 検証ミスをアラートとして記録し、ユーザーにエラーメッセージを与えるか、トラブルシューティングのためにエラーを記録することで、礼儀正しく対応してください。 個別の検証ロジック
複雑な検証状況や特定のドメイン要件に応じて、Fluntを拡張するために独自の検証ルールを使用します。
フレームワークとの統合: 現在のアプリケーションでのバリデーション機能を向上させるために、FluntはEntity Framework、ASP.NET Coreなどの多くのよく知られた.NETフレームワークやライブラリとシームレスに統合できます。
開発者は Flunt と IronPDF を統合することで、C# アプリケーションにおけるビジネスロジックの検証とドキュメント作成を迅速化するために、これら両技術の強みを活用することができます。 Fluntで入力データを検証した後、IronPDFを使ってPDFドキュメントを作成することで、開発者はアプリケーションをより信頼性の高い、ユーザーフレンドリーなものにすることができます。
Install-Package IronPdf
検索結果からIronPDFパッケージを探索して選択した後、「インストール」ボタンをクリックしてください。 インストールとダウンロードは Visual Studio によって処理されます。
IronPDFの機能、互換性、その他のダウンロードオプションについて詳しくは、NuGetウェブサイトのNuGetパッケージ詳細ページをご覧ください。
代替手段として、IronPDFのDLLファイルを利用してプロジェクトに直接組み込むことができます。 DLL を含む ZIP ファイルを取得するには、次の IronPDF ZIP ダウンロードページを訪問してください。 DLLファイルを解凍したら、それをプロジェクトに含めます。
IronPDFを使用してPDF作成を行い、Fluntを用いてデータ検証を行う基本的なC#アプリケーションを作成しましょう。 この例では、Fluntを使用して登録フォームのユーザー入力を検証し、IronPDFを使用して検証されたユーザーデータの概要を含むPDFドキュメントを作成します。
パーソン クラス: 名前と年齢の属性を持つパーソン クラスが定義されています。 私たちは、Fluntのフルエントインターフェースをコンストラクタで使用して、事前定義された検証ルールに基づいてPersonデータを検証します。
PDFを生成: RenderHtmlAsPdf
というメソッドが定義されており、ユーザーオブジェクトを入力として受け取ります。 この関数は、IronPDFのHtmlToPdf
クラスを使用して、ユーザー登録の概要を表すHTMLテキストをPDFドキュメントにレンダリングします。
メインメソッド: サンプルのPersonデータを使用して、メインメソッドでUserクラスのインスタンスを構築します。 次に、Flunt のIsValid
属性を使用して、Person データが正当かどうかを判断します。 データが正しい場合、PDFドキュメントを作成するためにIronPDFメソッドを呼び出します。 コンソールに検証問題が表示されます。
私たちは、PDF生成のためのIronPDFとデータ検証のための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");
}
}
}
Imports IronPdf
Imports System
Imports System.Linq
Imports System.Text
Imports Flunt.Validations
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim sb As New StringBuilder()
Dim person As New Person With {
.Name = "Jack",
.Age = -25
}
Dim contract = New PersonContract(person)
If contract.IsValid Then
Console.WriteLine("Person is valid!")
Else
sb.Append("<p>Validation failed: </p>")
For Each notification In contract.Notifications
sb.Append($"- {notification.Key}: {notification.Message}")
Next notification
End If
Dim renderer = New IronPdf.HtmlToPdf()
'Set HTML content for the page
Dim 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!")
End Sub
End Class
Public Class Person
Public Property Name() As String
Public Property Age() As Integer
End Class
Public Class PersonContract
Inherits Contract(Of Person)
Public Sub New(ByVal person As 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")
End Sub
End Class
End Namespace
以下は上記コードからの実行出力です:
IronPDFとFluntは、ドキュメント作成とデータ検証のワークフローを合理化するためにうまく機能する2つの強力なC#ライブラリです。 IronPDFの高度なPDF生成機能とFluntの強力な検証機能を活用することで、開発者はさまざまなアプリケーションに対して信頼性が高く、効率的で高品質なソリューションを構築できます。 FluntとIronPDFは、デスクトップアプリ、ウェブアプリケーション、クラウドベースのソリューションを開発する際に、ユーザーやステークホルダーのニーズに応える高品質のソフトウェアを作成するために必要なツールを開発者に提供します。
ソフトウェアサポートの1年間、永久ライセンス、そしてライブラリのアップグレードが$liteLicense Liteバンドルにすべて含まれています。 IronPDFは、費用とライセンス要件に関する詳細についての無料のライセンス情報を提供しています。 Iron Softwareライブラリに関する追加情報は、Iron Software公式ウェブサイトをご覧ください。