I'm trying to make my ASP.net Web API methods asynchronous. This method is in the Business Logic layer of my N-tier. I'm waiting for the result from my 1st task and use it on the 2nd task, but I think I'm doing it in a wrong way. Can you please give me advice or feedback regarding my code?
public async Task<DTOUser> CreateUser(DTORegister source)
{
var result = new DTOUser();
try
{
var user = new DTOUser()
{
Active = (int)defaultNumber.One,
Deleted = (int)defaultNumber.Zero,
DateCreated = DateTime.Now,
};
result = await Task.Run(() =>_RepositoryUser.Add<DTOUser, DTOUser>(user));
//This part is where I dont feel confident
var login = new DTOLogin()
{
Email = source.Email,
Password = source.Password,
OwnerID = result.UserID, //using the result of my 1st task here.
DateCreated = DateTime.Now
};
//use the login object to my 2nd task
var task2 = await Task.Run(() =>_RepositoryLogIn.Add<DTOLogin, DTOLogin>(login));
result.IsSuccessful = true;
}
catch (Exception ex)
{
result.ErrorMsg = "BL Error - " + ex.Message;
result.IsSuccessful = false;
}
return result;
}
Both _RepositoryUser.Add and _RepositoryLogIn.Add are calling the same Generic Repository method from DAL:
public TOutPut Add<TOutPut, TInput>(TInput input)
{
var entityObj = MappingConfiguration.MapObjects<TInput, TType>(input); //Calling my automapper method to map my Entity object and DTO Object
_entity.AddObject(this.GetEntitySetName(typeof(TType).Name), entityObj);
_entity.SaveChanges();
return MappingConfiguration.MapObjects<TType, TOutPut>(entityObj);
}