Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bo Jacobsen 606 posts 2404 karma points
    Jan 05, 2021 @ 08:29
    Bo Jacobsen
    0

    Inject custom service into custom DataAnnotation

    Hi all.

    How do i Inject a custom service into a custom DataAnnotation?

    I am using Umbraco version 8.9.1 and have setup a very simple test to get it going.

    Interface

    public interface ICustomService
    {
        bool DoSomeThing(object value);
    }
    

    Service

    using Umbraco.Core.Logging;
    
    public class CustomService : ICustomService
    {
        private readonly ILogger _logger;
    
        public CustomService(ILogger logger)
        {
            _logger = logger;
        }
    
        public bool DoSomeThing(object value)
        {
            return value == null;
        }
    }
    

    DataAnnotation (this do not work)

    using System.ComponentModel.DataAnnotations;
    
    public class CustomValidationAttribute : ValidationAttribute
    {
        private readonly ICustomService CustomService;
    
        public CustomValidationAttribute(ICustomService customService)
        {
            CustomService = customService;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if(CustomService.DoSomeThing(value))
            {
                return ValidationResult.Success;
            }
    
            return new ValidationResult(ErrorMessage);
        }
    }
    

    IUserComposer (Can i do something else here?)

    using Umbraco.Core.Composing;
    
    public class Installer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register<ICustomService, CustomService>();
        }
    }
    

    Any thoughts and help is very appreciated.

  • Bo Jacobsen 606 posts 2404 karma points
    Jan 05, 2021 @ 09:42
    Bo Jacobsen
    0

    This works. But is it the right way to do it?

    using Umbraco.Core.Composing;
    using Umbraco.Core;
    
    public class CustomValidationAttribute : ValidationAttribute
    {
        private readonly ICustomService CustomService;
    
        public CustomValidationAttribute()
        {
            CustomService = Current.Factory.GetInstance<ICustomService>();
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (CustomService.DoSomeThing(value))
            {
                return ValidationResult.Success;
            }
    
            return new ValidationResult(ErrorMessage);
        }
    }
    
  • Bo Jacobsen 606 posts 2404 karma points
    Oct 12, 2023 @ 11:43
    Bo Jacobsen
    0

    It seems like you also could use the validationContext to get a service by using Microsoft.Extensions.DependencyInjection like

    public class CustomValidationAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var customService = validationContext.GetService<ICustomService>();
            if (customService.DoSomeThing(value))
            {
                return ValidationResult.Success;
            }
    
            return new ValidationResult(ErrorMessage);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft