最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Import repository in a custom attribute class in asp.net core - Stack Overflow

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论