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!

\$\endgroup\$
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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