3

I'm writing classes to support domain model CRUD operations and I would like to try multi threading to get the best performance out of my code.

My workflow is as follows:

  1. Create a Project
  2. Create a historical record for the Project
  3. Extract tasks from the Project and create each Task

My code:

public class CreateProject : Base.BaseBMSEntityCreate<Project> {
    public CreateProject(BMSContext context, IValidationDictionary validation) : base(context, validation) { }

    public override Project Create(Project entity) {
        //create the project
        //set any default values on entity
        //base.Create adds the entity to the DbContext (BMSContext) that was set in constructor
        //and returns the entity
        var project = base.Create(entity);

        //create history for the project
        System.Threading.Tasks.Task history = System.Threading.Tasks.Task.Factory.StartNew(() => createHistory(db, validation, project));

        //create tasks for the project
        System.Threading.Tasks.Task tasks = System.Threading.Tasks.Task.Factory.StartNew(() => createTasks(db, validation, project));

        System.Threading.Tasks.Task.WaitAll(history, tasks);

        return project;
    }

    private void createTasks(BMSContext db, IValidationDictionary validation, Project project) {
        //in here I will be making use of:
        //1.    context
        //2.    validation
        //3.    project.Tasks
    }

    private void createHistory(BMSContext context, IValidationDictionary validation, Project project) {
        //in here I will be making use of:
        //1.    context
        //2.    validation
        //3.    project
    }
}

When using the parameters BMSContext (DbContext), validation and project will I be at risk of locking exceptions?

(if yes) How do I prevent against this?

and finally, is it wise to protect against my variables (DbContext in particular) in this way or is there a better architecture/pattern that would work more efficiently?

Smithy
  • 2,170
  • 6
  • 29
  • 61
  • Know that you'll need support from whatever your DBMS is to enable multi-threading options. Here's a useful link: http://dba.stackexchange.com/questions/2918/about-single-threaded-versus-multithreaded-databases-performance – ryuu9187 Oct 16 '15 at 15:35
  • Here's a link more representative of the question you just asked: http://stackoverflow.com/questions/9952137/multi-threading-c-sharp-application-with-sql-server-database-calls – ryuu9187 Oct 16 '15 at 15:36

0 Answers0