Search code examples
c#thread-safetycastle-windsor

Does having multiple instances of a class (Reference Type) make it thread safe?


Hoping you can help me clear up my understanding on thread safety and multiple instances of a reference type.

I've outlined a simple scenario below. My question is: if ProcessorOne and ProcessorTwo are running at the same time, and if the DatabaseIntegration class is a reference type and ProcessorOne and ProcessorTwo both have their own instances of DataBaseIntegration, then is the potential for the Entity saved by ProcessorOne to have the name from ProcessorTwo prevented?

public class ProcessorOne
{
    private readonly DatabaseIntegration databaseIntegration;
    public ProcessorOne(DatabaseIntegration databaseIntegration)
    {
        this.databaseIntegration = databaseIntegration;
    }

    public void Process()
    {                                  
       this.databaseIntegration.SetName("NameOne")
       Entity entity = new Entity()
       this.dataBaseIntegration.Save(entity);
    }
}

public class ProcessorTwo
{
    private readonly DatabaseIntegration dataBaseIntegration;
    public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
    {
        this.dataBaseIntegration = dataBaseIntegration;
    }

    public void Process()
    {            
       this.databaseIntegration.SetName("NameTwo")
       Entity entity = new Entity()
       this.dataBaseIntegration.Save(entity);
    }
}

public class DatabaseIntegration 
{
    private string entityName;

    public void SetName(string entityName)
    {
        this.entityName = entityName;
    }

    public void Save(Entity entity)
    {            
         entity.EntityName = this.entityName;
         using (DbContext context = new DbContext(sqlConnection))
         {
             context.Entity.Add(entity);
             context.SaveChanges();                 
         }
    }
}

Solution

  • Answer is yes they are thread safe. But why don't you set entity name as you create them. In that case even if you use the same DatabaseIntegration object it will still be thread safe because you pass different entity objects each time you call Save().

    public class ProcessorOne
    {
       private readonly DatabaseIntegration databaseIntegration;
       public ProcessorOne(DatabaseIntegration databaseIntegration)
       {
           this.databaseIntegration = databaseIntegration;
       }
    
       public void Process()
       {                                  
          Entity entity = new Entity(){entityName = "NameOne"}
          this.dataBaseIntegration.Save(entity);
       }
     }
    
    public class ProcessorTwo
    {
       private readonly DatabaseIntegration dataBaseIntegration;
       public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
       {
           this.dataBaseIntegration = dataBaseIntegration;
       }
    
       public void Process()
       {            
          Entity entity = new Entity(){entityName = "NameTwo"}
          this.dataBaseIntegration.Save(entity);
       }
    }
    
    public class DatabaseIntegration 
    {
       public void Save(Entity entity)
       {            
         using (DbContext context = new DbContext(sqlConnection))
         {
             context.Entity.Add(entity);
             context.SaveChanges();                 
         }
       }
    }