title: Lock on an class member vs having that object initialized in class' method?
body: <p>So, I have this service, let's call it "MyService". MyService has one public method, which is "executeBatchJob", as the name says, it will execute a batchJob, in short, retrieving some data from a database, at some point, I have an object that is very much like an entity, with an ID and a list of records. In my method executeBatchJob, some checks are made, and when some conditions are met, the entity is written somewhere and re-initialized (new ID, different records), but note that it happens in private methods called by executeBatchJob. Under other conditions, in different private method, records are added to the list of records hold by the entity.</p>
  
  <p>My question is: As the service might be called by several process: is it better to have that entity declared as a class member (private readonly) and lock it when needed, and then having a method that will clean the entity's state for the next process. Or is it better to have this object declared and used in my method executeBatchJob, but dragging the object around through all the private methods as the state of the object can change at several "level" ?</p>
  
  <p>To illustrate you what I was explaining :</p>
  
  <p>Here is myEntity :</p>
  
  <pre><code>public class MyEntity
  {
   public int Id { get; set; }
   public List&lt;Record&gt; Records { get;  set; }
  
   public void CleanUp(int newId)
   {
       Id = newId;
       Records.Clear();
   }
  }
  </code></pre>
  
  <p>Here is myService with a lock : </p>
  
  <pre><code>public class MyService : IMyService
  {
   private readonly MyEntity _myEntity;
  
   public MyService()
   {
       _myEntity = new MyEntity();
   }
  
   public void executeBatchJob(int batchId)
   {
       //some code
       lock(_myEntity)
       {
          // More code and call to some private method
          _myEntity.CleanUp();
       }
  
       // still more code
   }
  }
  </code></pre>
  
  <p>Or with the second option : </p>
  
  <pre><code>public class MyService : IMyService
  {
  
   public MyService()
   {
   }
  
   public void executeBatchJob(int batchId)
   {
       MyEntity myEntity = new MyEntity();
  
       APrivateMethodInExecuteBatchJob(myEntity);
       // more code
  
   }
  
   private returnResult APrivateMethodInExecuteBatchJob(MyEntity myEntity)
   {
        // Some manipulation of myEntity
        // some code, with another call to a private method with myEntity, and manipulation of myEntity
  
        // ... write the entity
        myRepository.write(myEntity);
  
   }
  }
  </code></pre>
  
  <p>To give more context, as far as I know, each time the entity will be written, it has to be cleaned up, in the second option, I can just do a "new MyEntity()" (which makes more sense on a business point of view).</p>
  
  <p>To me, the second solution is more logic and appropriate, BUT, I'll have several private methods that will move the object, sometimes just to pass it to another private method without any other manipulation than "passing" it to that other private method... Which is not clean at all...</p>
  
  <p>That's why I need your advice / opinion on it. </p>