I have a lookup service in a blazor hybrid application that essentially gets entities from database. Have registered this in main as
AddScoped<ILookupService, LookupService>
Throughout the application have been using this service without any issues in components.
Now I have come to a point where I want to perform form validations. I wish to get some constant values from database and use these to write some custom validation attributes.
I have created a class BusinessConstants
public class BusinessConstantsLookup
{
private ILookupService _lookupService;
public BusinessConstantsLookup(ILookupService lookupService)
{
_lookupService = lookupService;
}
public string GetBusinessConstant(string businessConstantName)
{
...
}
}
Have also registered this as scoped.
Now is there a way to use this class in custom validation attribute class? The challenge is where do I get the instance for LookupService to use in the BusinessConstantsLookup constructor when I use it in that custom validation attribute class.
public class CustomRangeAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(
object? value, ValidationContext validationContext)
{
BusinessConstantsLookup businessConstantsLookup =
new BusinessConstantsLookup(<how I get LookupService instance?>);
...
}
}
Can DI in attribute class be solution?
Let's consider about Dependency Injection and Attributes in .NET Core and C#:
Thus we can rule out injecting DbContext into an attribute. So, what can we do next?
Method 1. Get the constants from database, store them into a static class.
Your attribute can then access the static properties to get the db constants, but beware of potential issues:
Method 2. Validate in services instead.
ValidationAttribute
s in C# usually are used for input validations. Write validations that rely upon database content in methods using the input objects, and the class of those methods can inject DbContext or classes which can access it.