Hi people,
Im fairly sure this will have been asked before but I haven't found the answer. I have an account object that creates a user like so;
public class Account
{
public ICollection Users { get; set; }
public User CreateUser(string email)
{
User user = new User(email);
user.Account = this;
Users.Add(user);
}
}
so in my service layer when creating a new user i call this method. However there is a rule that the users email MUST be unique to the account, so where does this go? To me it should go in the CreateUser method with an extra line that just checks that the email is unique to the account. However if it were to do this then ALL the users for the account would need to be loaded in and that seems like a bit of an overhead to me. It would be better to query the database for the users email - but doing that in the method would require a repository in the account object wouldn't it? Maybe the answer then is when loading the account from the repository instead of doing;
var accountRepository.Get(12);
//instead do
var accountRepository.GetWithUserLoadedOnEmail(12, "[email protected]");
Then the account object could still check the Users collection for the email and it would have been eagerly loaded in if found.
Does this work? What do you do? What would you do? Any guidance would be much appreciated.
Oh and Im using NHibernate as an ORM if that helps.