generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFluentValidator.cs
24 lines (22 loc) · 1.25 KB
/
FluentValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using Microsoft.Extensions.DependencyInjection;
using System;
using FV = FluentValidation;
namespace CoreEx.FluentValidation
{
/// <summary>
/// Provides access to the fluent-validator capabilities.
/// </summary>
public static class FluentValidator
{
/// <summary>
/// Creates (or gets) an instance of the validator.
/// </summary>
/// <typeparam name="TValidator">The validator <see cref="Type"/>.</typeparam>
/// <param name="serviceProvider">The <see cref="IServiceProvider"/>; defaults to <see cref="ExecutionContext.ServiceProvider"/> where not specified.</param>
/// <returns>The <typeparamref name="TValidator"/> instance.</returns>
public static TValidator Create<TValidator>(IServiceProvider? serviceProvider = null) where TValidator : FV.IValidator
=> (serviceProvider == null ? ExecutionContext.GetService<TValidator>() : serviceProvider.GetService<TValidator>())
?? throw new InvalidOperationException($"Attempted to get service '{typeof(TValidator).FullName}' but null was returned; this would indicate that the service has not been configured correctly.");
}
}