Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.