Skip to main content
Question Protected by gnat
Tweeted twitter.com/#!/StackProgrammer/status/462118257710084096
added 170 characters in body
Source Link
Matthew
  • 2k
  • 3
  • 19
  • 27

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"); } } } 

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 void DisplayUser() { try { var user = _userRepository.GetByID(5); MessageBox.Show("Hello " + user.Name); } catch (UserNotFoundException) { MessageBox.Show("User not found"); } } 

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"); } } } 
Source Link
Matthew
  • 2k
  • 3
  • 19
  • 27

How to enforce implementation of interface to behave a certain way

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 void DisplayUser() { try { var user = _userRepository.GetByID(5); MessageBox.Show("Hello " + user.Name); } catch (UserNotFoundException) { MessageBox.Show("User not found"); } }