Suppose you had the following interface
public interface IUserRepository { User GetByID(int userID); } How would you enforce implementers of this interface to throw an exception if a user is not found?
I suspect it's not possible to do with code alone, so how would you enforce implementers to implement the intended behavior? Be it through code, documentation, etc.?
In this example, the concrete implementation is expected to throw a UserNotFoundException
public class SomeClass { private readonly IUserRepository _userRepository; public SomeClass(IUserRepository userRepository) { _userRepository = userRepository; } public void DisplayUser() { try { var user = _userRepository.GetByID(5); MessageBox.Show("Hello " + user.Name); } catch (UserNotFoundException) { MessageBox.Show("User not found"); } } }