I have some logic like this, before save the stock into the db, i will check whether there is stock has the same stock code in the database. My question is where should I put the logic, in the service layer or repository layer. here is the sample code:
option 1: put in the service layer, i put the IsAccountAlreadyExists Method in the service layer
public override void Save(AccountInfo accountInfo)
{
using (var scope = new TransactionScope())
{
if(this.IsAccountAlreadyExists(accountInfo))
{
throw new AccountAlreadyExistedException(
"Account Code : " + accountInfo.AccountCode +
" already existed.");
}
accountRepository.Save(accountInfo);
scope.Complete();
}
}
option 2: I will move the IsAccountAlreadyExists logic to the repository layer.
public override void Save(AccountInfo accountInfo)
{
try
{
using (var scope = new TransactionScope())
{
accountRepository.Save(accountInfo);
scope.Complete();
}
}
catch(AccountAlreadyExistedException e)
{
...
}
}
what's your opinion?