To avoid duplicate telephone number entries to the database, the following Custom Validation Attribute has been designed, but for some reason, the calling class cannot read the Repository
.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
sealed public class TelephoneAttribute : ValidationAttribute
{
private readonly IJobRepository _jobRepository;
public TelephoneAttribute(IJobRepository jobRepository)
{
_jobRepository = jobRepository;
}
public override bool IsValid(object value)
{
bool result = false;
// my logic to avoid duplication of telephone
var anyDuplicate = _jobRepository.AllJobs.GroupBy(x => x.CustomerContactNumber)
.Any(g => g.Count() > 1);
if (anyDuplicate) { result = true; }
return result;
}
}
While trying to use this custom attribute in a Model class
like:
[Required(ErrorMessage = "**Required**")]
[Display(Name = "Customer Contact Number")]
[DataType(DataType.PhoneNumber)]
[TelephoneAttribute(ErrorMessage = "Duplicate Entry")]
public string CustomerContactNumber { get; set; }
On [TelephoneAttribute(ErrorMessage = "Duplicate Entry")]
line of code, I'm receiving following error:
CS7036: There is no argument given that corresponds to the required parameter 'jobRepository' of 'TelephoneAttribute.TelephoneAttribute(IJobRepository)'
Please guide. Thanks