Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////
- // Example without dependency
- // injection
- //////////////////////////////////
- public class UserService : IUserService
- {
- readonly IRepository<User> _userRepo;
- readonly IEmailService _emailService;
- public UserService()
- {
- _userRepo = new Repository<User>(Configuration.ConnectionStrings["ConnectionStr"]);
- _emailService = new SmtpEmailService(
- Configuration.AppSettings["emailHost"],
- Configuration.AppSettings["emailUser"],
- Configuration.AppSettings["emailPass"],
- Configuration.AppSettings["emailFromAddress"]
- );
- }
- public void AddUser(User user)
- {
- _userRepo.Add(user);
- string subject = "Account created";
- string body = "Thank you, #{0}. Your user account has been created.";
- _emailService.SendEmail(subject, body, user.Email);
- }
- }
- ///////////////////////////////////////
- // Solution that lets dependencies
- // get injected, instead of constructing
- // them in the constructor
- ////////////////////////////////////////
- public class UserService : IUserService
- {
- readonly IRepository<User> _userRepo;
- readonly IEmailService _emailService;
- public UserService(IRepository<User> userRepo, IEmailService emailService)
- {
- _userRepo = userRepo;
- _emailService = emailService;
- }
- public void AddUser(User user)
- {
- _userRepo.Add(user);
- string subject = "Account created";
- string body = "Thank you, #{0}. Your user account has been created.";
- _emailService.SendEmail(subject, body, user.Email);
- }
- }
- //////////////////////////////////////////////////////
- // Testing the second example. Testing the first
- // example will be nearly impossible, since it
- // has hard dependencies on the services it requires
- //////////////////////////////////////////////////////
- public class MockEmailService : IEmailService
- {
- readonly IList<Email> _sentEmails = new List<Email>();
- public void SendEmail(string subject, string body, string recipient)
- {
- var email = new Email
- {
- Subject = subject,
- Body = body,
- Recipient = recipient
- };
- _sentEmails.Add(email);
- }
- public IList<Email> SentEmails
- {
- get { return _sentEmails; }
- }
- }
- public class MockUserRepository : IRepository<User>
- {
- readonly IList<User> _addedUsers = new List<User>();
- public void Add(User user)
- {
- _addedUsers.Add(user);
- }
- public IList<User> AddedUsers
- {
- get { return _addedUsers; }
- }
- }
- public class UserServiceTest : UnitTest
- {
- public void TestAddUser()
- {
- var emailMock = new MockEmailService();
- var repoMock = new MockUserRepository();
- IUserService userService = new UserService(repoMock, emailMock);
- User user = new User
- {
- Username = "metallic",
- Password = "It's a secret",
- Email = "metallic@example.com"
- }
- userService.AddUser(user);
- Assert.AreEqual(repoMock.AddedUsers.Count, 1);
- Assert.AreEqual(emailMock.SentEmails, 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement