I am implementing a web api and i have crated three layers,data access layer,business layer and presentation layer.
In data access layer I have a method which create a user.I use a transaction which contain and a method searchByUsername which check first in the database if a username exists .If not exist then i create the user.
public bool CreateUser(CreateUserRequest request)
{
//START TRANSACTION....
if(searchByUsername(username))
{
throw new Exception("Not valid username")
}
else
{
var res =db.Execute(....);
}
}
This concept is good practice tο use transaction in data layer? I thought that I must use transaction because if my API has many requests maybe another user create account with the same username before the inserting of another user .