Skip to content

Instantly share code, notes, and snippets.

@tarikguney
Last active February 3, 2017 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarikguney/b1487bf3b394e081f249cbd3d2874afd to your computer and use it in GitHub Desktop.
Save tarikguney/b1487bf3b394e081f249cbd3d2874afd to your computer and use it in GitHub Desktop.
Unit Testing with Dependency Injection
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