Optionable 0.2.2
dotnet add package Optionable --version 0.2.2
NuGet\Install-Package Optionable -Version 0.2.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Optionable" Version="0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Optionable --version 0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Optionable, 0.2.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Optionable as a Cake Addin #addin nuget:?package=Optionable&version=0.2.2 // Install Optionable as a Cake Tool #tool nuget:?package=Optionable&version=0.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Optionable
Add options pattern to your project
Sample
Add an Option to FluentValidation Library
I want to added attribute option instead of RuleFor() method.
Option defining
public class AttributeOption : IOption
{
public bool UseNotNullAttribute { get; set; } = false;
}
Attribute defining If my option is active I will use this attribute
[AttributeUsage(AttributeTargets.Property)]
public class NotNullAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Property)]
internal class WithMessageAttribute : Attribute
{
public string Message { get; set; }
public WithMessageAttribute(string message)
{
Message = message;
}
}
Classic FluentValidation sample class
public class Customer
{
public int Id { get; set; }
//My Attribute
[NotNull]
public string Surname { get; set; }
//My Attribute
[NotNull]
[WithMessage("Please specify a first name")]
public string Forename { get; set; }
public decimal Discount { get; set; }
//My Attribute
[NotNull]
public string Address { get; set; }
}
Implement IOptionable interface your base class
public class OptionableValidator<T> : AbstractValidator<T>, IOptionable
{
public List<IOption> Options {get; set;} = new List<IOption>();
public override ValidationResult Validate(ValidationContext<T> context)
{
this.UseNotNullAttribute(context);
return base.Validate(context);
}
private void UseNotNullAttribute(ValidationContext<T> context)
{
//Get injected option
var option = this.GetOption<AttributeOption>();
if (option is not null && option.UseNotNullAttribute)
{
var props = context.InstanceToValidate.GetType().GetProperties();
foreach (var prop in props)
{
if (prop.GetCustomAttribute<NotNullAttribute>() is not null)
{
var param = Expression.Parameter(typeof(T));
var lambda = Expression.Lambda<Func<T, object>>((Expression)Expression.Property(param, prop.Name), param);
var withMessage = prop.GetCustomAttribute<WithMessageAttribute>();
if (withMessage is not null)
{
this.RuleFor<object>(lambda).NotNull().WithMessage(withMessage.Message);
}
else
{
this.RuleFor<object>(lambda).NotNull();
}
}
}
}
}
}
public class CustomerValidator : OptionableValidator<Customer>
{
public CustomerValidator()
{
}
}
Usage
Customer customer = new Customer();
CustomerValidator validator = new CustomerValidator();
validator.AddOption<AttributeOption>(opt => opt.UseNotNullAttribute = true);
ValidationResult results = validator.Validate(customer);
if (!results.IsValid)
{
foreach (var failure in results.Errors)
{
Console.WriteLine("Property " + failure.PropertyName + " failed validation. Error was: " + failure.ErrorMessage);
}
}
Console.ReadLine();
Output
Property Surname failed validation. Error was: 'Surname' boş olamaz.
Property Forename failed validation. Error was: Please specify a first name
Property Address failed validation. Error was: 'Address' boş olamaz.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
-
net7.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.