Last active
February 3, 2017 04:31
-
-
Save tarikguney/b1487bf3b394e081f249cbd3d2874afd to your computer and use it in GitHub Desktop.
Unit Testing with Dependency Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountCreator{ | |
// Interface'ler tanımlıyoruz. Dolayısıyla kendi sınıflarımızı rahatlıkla kullanabiliriz. | |
private IAccountChecker _accountChecker; | |
private IAccountRepository _accountRepository; | |
// Dependency'lerimizi constructor method vasıtasıyla enjekte ediyoruz. | |
public AccountCreator(IAccountChecker accountChecker, IAccountRepository accountRepository){ | |
_accountChecker = accountChecker; | |
_accountRepository = new accountRepository; | |
} | |
public void CreateAccount(AccountInfo accountInfo){ | |
if(HasAccountNumber(accountInfo)){ | |
throw new InvalidAccountInfo("New accounts cannot have account numbers"); | |
} | |
if(_accountChecker.Exists(SanitizeUserName(accountInfo.UserName)){ | |
throw new UsernameExistsException("This username is already taken. Please use a different username"); | |
} | |
_accountRepository.Create(GetAccountDataTransferObject(accountInfo)); | |
} | |
private string SanitizeUserName(string username){ | |
var sanitizedUsername = username.Trim().ToLower().HtmlEncode(); | |
} | |
private bool HasAccountNumber(AccountInfo accountInfo){ | |
return accountInfo.AccountNumber != null; | |
} | |
private AccountDTO GetAccountDataTransferObject(AccountInfo accountInfo){ | |
return new AccountDTO { | |
FirstName = accountInfo.FirstName, | |
LastName = accountInfo.LastName, | |
UserName = accountInfo.UserName, | |
AccountCreated = accountInfo.AccountCreated | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment