title: How to Implement a general Save() Function in a Base Class
body: <p>As an assessment, I received the following Testcase so I can implement the code behind:</p>
  <pre class="lang-cs prettyprint-override"><code>[TestCase]
  public void ProgrammerTest() 
  {
      var address = new Address(&quot;56 Main St&quot;, &quot;Mesa&quot;, &quot;AZ&quot;, &quot;38574&quot;);
      var customer = new Customer(&quot;John&quot;, &quot;Doe&quot;, address);
      var company = new Company(&quot;Google&quot;, address);
   
      Assert.IsNullOrEmpty(customer.Id);
      customer.Save();
      Assert.IsNotNullOrEmpty(customer.Id);
   
      Assert.IsNullOrEmpty(company.Id);
      company.Save();
      Assert.IsNotNullOrEmpty(company.Id);
   
      Customer savedCustomer = Customer.Find(customer.Id);
      Assert.IsNotNull(savedCustomer);
      Assert.AreSame(customer.Address, address);
      Assert.AreEqual(savedCustomer.Address, address);
      Assert.AreEqual(customer.Id, savedCustomer.Id);
      Assert.AreEqual(customer.FirstName, savedCustomer.FirstName);
      Assert.AreEqual(customer.LastName, savedCustomer.LastName);
      Assert.AreEqual(customer, savedCustomer);
      Assert.AreNotSame(customer, savedCustomer);
   
      Company savedCompany = Company.Find(company.Id);
      Assert.IsNotNull(savedCompany);
      Assert.AreSame(company.Address, address);
      Assert.AreEqual(savedCompany.Address, address);
      Assert.AreEqual(company.Id, savedCompany.Id);
      Assert.AreEqual(company.Name, savedCompany.Name);
      Assert.AreEqual(company, savedCompany);
      Assert.AreNotSame(company, savedCompany);
   
      customer.Delete();
      Assert.IsNullOrEmpty(customer.Id);
      Assert.IsNull(Customer.Find(customer.Id));
   
      company.Delete();
      Assert.IsNullOrEmpty(company.Id);
      Assert.IsNull(Company.Find(company.Id));
  }
  </code></pre>
  <p>The requirements are:</p>
  <ul>
  <li>In C# create a single class that when subclassed allows this sample test code to run using only the file system for storage, no pre-built database allowed; use files.</li>
  <li>Create all classes required to compile and pass the test case; you may not modify the test. The test is not wrong, it's not a trick.</li>
  <li>The <code>Id</code>, <code>Save</code>, <code>Delete</code>, and <code>Find</code> methods must be in the super class only; subclasses must not implement these methods themselves.</li>
  </ul>
  <p>Here are a class example I could extract from the test cases:</p>
  <pre class="lang-cs prettyprint-override"><code>public abstract class MyBase
  {
      public abstract void Save();
      public abstract void Delete();
  }
  
  public class Company : MyBase
  {
      public string Id { get; set; }
      public string Name { get; set; }
      public Address Address { get; set; }
  
      public Company(string name, Address address)
      {
          this.Name = name;
          this.Address = address;
      }
  
      internal static Company Find(string id)
      {
          throw new NotImplementedException();
      }
  
      public override void Delete()
      {
         throw new NotImplementedException();
      }
  
      public override void Save()
      {
          object company = new List&lt;Company&gt;();
  
          if (company != null)
          {
              // serialize JSON to a string and then write string to a file
              File.WriteAllText(@&quot;c:\company.json&quot;, JsonConvert.SerializeObject(company));
          }
      }
  }
  </code></pre>
  <p>I have experience creating TestCases for a given code, but not the other way out and I could extract the Classes from it but, how can I implement the methods <code>Save()</code>, <code>Delete()</code>, <code>Find(customer.Id)</code> as Generic Types without passing the populated data classes? I know I could do something like <code>void Save(object obj)</code> and implement the passed object on the Base, but the testcase just calls like <code>customer.Save()</code>... So, how can I do this?</p>
  <p>Thanks in advance for any input!</p>