0

What would be the best way to validate the uniqueness of a model? For example, making sure that only one user can have a certain username.

I am using the repository pattern to interact with the database, so should I place the check int there? If so, how do I get this to filter back to the model?

Thanks

1
  • Why don't you just post some code? Patterns are such an abstract thing... Commented Aug 4, 2009 at 13:04

2 Answers 2

2

Are you also enforcing this constraint in the database?

Sign up to request clarification or add additional context in comments.

2 Comments

No, not at the moment. Does LINQ to SQL undertsand that?
That I don't know, but I usually put these kinds of constraints in the code and the database to be sure.
0

Add a method to your UserRepository to lookup if the UserName is in use. The UserRepository could perform this validation on User.Save

public class UserRepository {

  private bool IsUserNameInUse(string userName) {
     return false;
  }

  private bool IsUserNameInUse(int userId, string userName) {
    // Verify no record other than the userid submitted is using the username
    return false;
  }

  public void Save(User userToSave) {
      if (IsUserNameInUse(userToSave.UserName)) throw new Exception();
  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.