Unique Property Checker

17 views
Skip to first unread message

Jake Scott

unread,
Jan 28, 2009, 4:50:27 PM1/28/09
to sharp-arc...@googlegroups.com
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

[HasUniqueDomainSignature(Message = "Another member is already registered with that email address")]
Member
{
    [DomainSignature]
    public string EmailAddress{ get; set; }
   
    [UniqueConstraint("Another member is already registered with that nickname")]
    public string Nickname{get;set;}
}


That would be a cool addition to the Sharp Validators!

Cheers
Jake Scott


Billy

unread,
Jan 28, 2009, 6:08:05 PM1/28/09
to S#arp Architecture
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*
Reply all
Reply to author
Forward
0 new messages