SHARE
TWEET

Untitled

a guest Jun 15th, 2017 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////
  2. // Example without dependency
  3. // injection
  4. //////////////////////////////////
  5.  
  6. public class UserService : IUserService
  7. {
  8.     readonly IRepository<User> _userRepo;
  9.     readonly IEmailService _emailService;
  10.    
  11.     public UserService()
  12.     {
  13.         _userRepo = new Repository<User>(Configuration.ConnectionStrings["ConnectionStr"]);
  14.         _emailService = new SmtpEmailService(
  15.             Configuration.AppSettings["emailHost"],
  16.             Configuration.AppSettings["emailUser"],
  17.             Configuration.AppSettings["emailPass"],
  18.             Configuration.AppSettings["emailFromAddress"]
  19.         );
  20.     }
  21.    
  22.     public void AddUser(User user)
  23.     {
  24.         _userRepo.Add(user);
  25.        
  26.         string subject = "Account created";
  27.         string body = "Thank you, #{0}. Your user account has been created.";
  28.         _emailService.SendEmail(subject, body, user.Email);
  29.     }
  30. }
  31.  
  32. ///////////////////////////////////////
  33. // Solution that lets dependencies
  34. // get injected, instead of constructing
  35. // them in the constructor
  36. ////////////////////////////////////////
  37.  
  38. public class UserService : IUserService
  39. {
  40.     readonly IRepository<User> _userRepo;
  41.     readonly IEmailService _emailService;
  42.    
  43.     public UserService(IRepository<User> userRepo, IEmailService emailService)
  44.     {
  45.       _userRepo = userRepo;
  46.       _emailService = emailService;
  47.     }
  48.    
  49.     public void AddUser(User user)
  50.     {
  51.         _userRepo.Add(user);
  52.        
  53.         string subject = "Account created";
  54.         string body = "Thank you, #{0}. Your user account has been created.";
  55.         _emailService.SendEmail(subject, body, user.Email);
  56.     }
  57. }
  58.  
  59. //////////////////////////////////////////////////////
  60. // Testing the second example. Testing the first
  61. // example will be nearly impossible, since it
  62. // has hard dependencies on the services it requires
  63. //////////////////////////////////////////////////////
  64.  
  65. public class MockEmailService : IEmailService
  66. {
  67.     readonly IList<Email> _sentEmails = new List<Email>();
  68.    
  69.     public void SendEmail(string subject, string body, string recipient)
  70.     {
  71.         var email = new Email
  72.         {
  73.             Subject = subject,
  74.             Body = body,
  75.             Recipient = recipient
  76.         };
  77.        
  78.         _sentEmails.Add(email);
  79.     }
  80.    
  81.     public IList<Email> SentEmails
  82.     {
  83.         get { return _sentEmails; }
  84.     }
  85. }
  86.  
  87. public class MockUserRepository : IRepository<User>
  88. {
  89.     readonly IList<User> _addedUsers = new List<User>();
  90.    
  91.     public void Add(User user)
  92.     {
  93.         _addedUsers.Add(user);
  94.     }
  95.    
  96.     public IList<User> AddedUsers
  97.     {
  98.         get { return _addedUsers; }
  99.     }
  100. }
  101.  
  102. public class UserServiceTest : UnitTest
  103. {
  104.     public void TestAddUser()
  105.     {
  106.         var emailMock = new MockEmailService();
  107.         var repoMock = new MockUserRepository();
  108.         IUserService userService = new UserService(repoMock, emailMock);
  109.         User user = new User
  110.         {
  111.             Username = "metallic",
  112.             Password = "It's a secret",
  113.             Email = "metallic@example.com"
  114.         }
  115.         userService.AddUser(user);
  116.        
  117.         Assert.AreEqual(repoMock.AddedUsers.Count, 1);
  118.         Assert.AreEqual(emailMock.SentEmails, 1);
  119.     }
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top