# MVC Repository Insert Using async

This is practically my first time using async in my code. I read about dos-and-donts but I wanted to get some feedback on what I've written.

I'm using a repository pattern and I'm inserting asynchronously into the database.

An explanation of the models involved: I have one DomainModel and three tblModels (tblModel1, tblModel2, tblModel3). tblModel2 and tblModel3 hold an ID from tblModel1. The DomainModel is made up of all three of the tblModels. I'm simplifying the domain to database mapping here because I'm focusing on feedback about the asynchronous calls.

Repository

public async Task<int> InsertAsync(tblModel1 tm1)
{
await db.SaveChangesAsync();
return tm1.ID;
}

{
await db.SaveChangesAsync();
return tm2.ID;
}

{
await db.SaveChangesAsync();
return tm3.ID;
}


Manager

public async Task<bool> InsertAsync(DomainModel M)
{
try
{
var tm1ID = await repo.InsertAsync(M.tm1);
});
}
catch (Exception ex)
{
return false;
}

return true;
}


The method AddID(tblModel, int) is to get the point across that I'm adding the ID from tblModel1 after it's inserted into tblModel2 and tblModel3. In actuality, I do this but I map the DomainModel to completely new tblModels and during that process I add the IDs to tblModel2 and tblModel3. I believe the code above is representative of what I'm doing.

Please let me know if there is a better way for me to optimize these asynchronous inserts into the database. Thank you!

The best thing you could do is let EF do what it's good at... You don't need to save every single time you add an entity - let EF track the changes in its DbSets repositories and then save the changes in the DbContext unit of work.

As a more general point - why aren't you saving the whole DomainModel at once? I'd expect to just be able to do:

context.DomainModels.Add(someDomainModel);
await context.SaveChanges();


I've hinted at it but I'll say it outright - you get very little to no benefit in writing Repositorys wrapping the db context. It's just not worth it.

I'm not going to comment on the bad naming because I'm guessing that this is actually just an example of your working code.

• I think I should also be returning the DomainModel that I'm adding to the database instead of a bool value. – christo8989 Jan 27 '16 at 0:41