TGViewer
C# Geeks (.NET) C# Geeks (.NET) @csharpgeeks · 550 subscribers
Post #356 167
🧱 تنظیم پایه‌ها (Setting Up the Foundation)


اول، پکیج FluentValidation را به پروژه اضافه می‌کنیم:
Install-Package FluentValidation   # پکیج اصلی
Install-Package FluentValidation.DependencyInjectionExtensions # برای یکپارچگی با DI

در مثال ما، از یک کلاس تنظیمات به نام GitHubSettings استفاده می‌کنیم که نیاز به اعتبارسنجی دارد:
public class GitHubSettings
{
public const string ConfigurationSection = "GitHubSettings";

public string BaseUrl { get; init; }
public string AccessToken { get; init; }
public string RepositoryName { get; init; }
}


🧩 ایجاد Validator با FluentValidation

سپس یک Validator برای کلاس تنظیمات می‌سازیم:
public class GitHubSettingsValidator : AbstractValidator<GitHubSettings>
{
public GitHubSettingsValidator()
{
RuleFor(x => x.BaseUrl).NotEmpty();

RuleFor(x => x.BaseUrl)
.Must(baseUrl => Uri.TryCreate(BaseUrl, UriKind.Absolute, out _))
.When(x => !string.IsNullOrWhiteSpace(x.BaseUrl))
.WithMessage($"{nameof(GitHubSettings.BaseUrl)} must be a valid URL");

RuleFor(x => x.AccessToken)
.NotEmpty();

RuleFor(x => x.RepositoryName)
.NotEmpty();
}
}
More from @csharpgeeks
  1. Sep 22, 2026یه مدتی قراره از دنیای NET. فاصله بگیرم، چون وقتشه برم سربازی. راستش نمیدونم این مدت رو چج…
  2. Sep 20, 2026🔥 حالا مشکل اصلی: Alert Storm فرض کن Database از دسترس خارج شده. ۱۰۰ Pod داری. هر Pod می‌…
  3. Sep 20, 2026🚨 طراحی سیستم Monitoring و Alerting در یک سیستم بزرگ فرض کن ساعت ۳ صبح است. سیستم شما با…
  4. Sep 19, 2026#Engineering_Leadership تصمیم نگرفتن هم یک تصمیم است یه چیز عجیب توی تیم‌های مهندسی: گاهی…
  5. Sep 19, 2026☑ چک‌لیست آماده‌سازی تیم، فرایندها و زیرساخت برای توسعه با AI توجه: هیچ چک‌لیستی جهان‌شمول…
  6. Sep 19, 2026📌پایان یک انتظار طولانی: اعتبارسنجی ناهمگام (Async Validation) در NET 11.
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →