Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay, I found a better solution that lets me use my previous library architecture. </p> <p>I was able to keep the UserMetadata class in a separate library from UserEntity. Here is what the dependency graph looks like:</p> <p>UserMetadata [depends on] DuplicateUserValidationAttribute [depends on] IDuplicateUserValidator</p> <p>The actual DuplicateUserValidator implementation class is in my service layer, and looks something like this:</p> <pre><code>public class DuplicateUserValidator : Validator&lt;string&gt;, IDuplicateUserValidator { public DuplicateUserValidator() : base(null, null) { } [Dependency] public IRepository&lt;UserEntity&gt; UserRepository { get; set; } protected override string DefaultMessageTemplate { get { throw new NotImplementedException(); } } protected override void DoValidate(string stringToValidate, object currentTarget, string key, ValidationResults validationResults) { if (this.UserRepository == null) throw new InvalidOperationException("This should not happen"); // actual code to perform validation... } } </code></pre> <p>The trick is getting the DuplicateUserValidatorAttribute to not depend on DuplicateUserValidator:</p> <pre><code>public class DuplicateUserValidatorAttribute : ValidatorAttribute { protected override Validator DoCreateValidator(Type targetType) { var validator = Unity.Container.Resolve&lt;IDuplicateUserValidator&gt;() as Validator; validator.Tag = Tag; validator.MessageTemplate = GetMessageTemplate(); return validator; } } </code></pre> <p>All I needed to do from there was add a mapping to the unity config. When the singleton resolves the IDuplicateUserValidator, it injects my implementation class from the service layer, honoring the [Dependency] attribute on the UserRepository property.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload