Please correct me if I'm wrong, but I believe you'd have to create a
class level validator to do what you're trying to do; e.g.,
[HasUniqueProperty(PropertyName="Nickname")] above the class
declaration. It would need to be at the class level to take the
object's ID into consideration when it checks the database for
uniqueness. IMO, this is getting a bit into the realm of "application
specific" and I would encourage you to add such a validator to
YourProject.Core.
Billy
On Jan 28, 2:50 pm, Jake Scott <
jake....@gmail.com> wrote:
> Has anyone had a go a adding a Unique Property checker validator?
>
> The HasUniqueDomainSignatureAttribute validator is fine for situations where
> you want to combine your properties to make sure that it is unique - but in
> my situation I have an Entity that has EmailAddress and Nickname that both
> have to be unique - so I cannot add DomainSignature to the Nickname
> property. At the moment I am resorting to overriding IsValid and using the
> common service locator to get my repository, run a custom query to make sure
> that the nickname is valid.
>
> *BEFORE*
>
> [HasUniqueDomainSignature(Message = "Another member is already registered
> with that email address")]
> Member
> {
> [DomainSignature]
> public string EmailAddress{ get; set ;}
>
> public string Nickname{ get; set; }
>
> public override bool IsValid()
> {
> return ValidationResults().Count == 0;
> }
>
> public override ICollection<IValidationResult> ValidationResults()
> {
> var results = base.ValidationResults();
> if (!string.IsNullOrEmpty(NickName))
> {
> var memberRepository =
> SafeServiceLocator<IMemberRepository>.GetService();
> var existing = memberRepository.GetMemberBy(ID, ForumUsername);
> if (existing != null)
> {
> var value = new InvalidValue("Another member is registered with
> the nickname", typeof(Member), "Nickname", Nickname, this);
> var validationResult = new ValidationResult(value);
> results.Add(validationResult);
> }
> }
> return results;
> }
>
> }
>
> *AFTER*