Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

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.

share|improve this answer
    
The DomainModel is not part of the context because I'm separating the domain from the database. I'm fairly new to ef but it seems as though you're asking me to include the DomainModel in my database. Is that what you think I should do? –  christo8989 Jun 29 at 19:59
    
Also, I stripped out the names. This is also my first time on code review. Should I only paste my actual code? –  christo8989 Jun 29 at 20:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.