2
\$\begingroup\$

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)
{
 tm1 = db.tblModel1.Add(tm1);
 await db.SaveChangesAsync();
 return tm1.ID;
}
public async Task<int> InsertAsync(tblModel2 tm2)
{
 tm2 = db.tblModel2.Add(tm2);
 await db.SaveChangesAsync();
 return tm2.ID;
}
public async Task<int> InsertAsync(tblModel3 tm3)
{
 tm3 = db.tblModel3.Add(tm3);
 await db.SaveChangesAsync();
 return tm3.ID;
}

Manager

public async Task<bool> InsertAsync(DomainModel M)
{
 try
 {
 await Task.Run(async () => {
 var tm1ID = await repo.InsertAsync(M.tm1);
 var tm2ID = await repo.InsertAsync(AddID(M.tm2,tm1ID));
 var tm3ID = await repo.InsertAsync(AddID(M.tm3,tm1ID));
 });
 }
 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!

asked Jun 28, 2015 at 20:45
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

answered Jun 29, 2015 at 8:33
\$\endgroup\$
1
  • \$\begingroup\$ I think I should also be returning the DomainModel that I'm adding to the database instead of a bool value. \$\endgroup\$ Commented Jan 27, 2016 at 0:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.