title: Creating Objects from SQL Server (not using an ORM framework)
body: <p>I am tyring to load objects from a SQL server (I am shying away from an orm framework for performance reasons)</p>
  
  <p>My thought was to have a factory class that retreived records from the server and created the appropriate type of objects.</p>
  
  <p>All objects will inherit from an abstract class: aDefinition. Although the concrete objects that derive from aDefintion both share some similar properties, they also have unique properties of their own on top of the base class.</p>
  
  <p>As I am fairly new to programming, I just wanted to get a feel for if I am on the right track.  </p>
  
  <p>I couple of questions: </p>
  
  <ul>
  <li><p>Does it make sense to use an abstract class in this case?</p></li>
  <li><p>Would it be a good idea to store the collection of definitions in the DefinitionCreator class as a <code>List&lt;aDefinition&gt;</code> and provide it methods for accessing the objects or would it be a better idea to create different concrete collections for the different object types?</p></li>
  </ul>
  
  
  
  <pre><code>public class DefinitionTemplate
  {
      public int DefinitionId { get; set; }
      public string Name { get; set; }
      public DefintionType DefinitionType { get; set; }
      public string Definition { get; set; }
      public string Parameters { get; set; }
  
      public DefinitionTemplate()
      {
      }
  }
  
  //abstract base class that my concrete classes will inherit from.
  public abstract class aDefinition
  {
      public int DefinitionId { get; protected set; }
      public string Name { get; protected set; }
      public DefintionType DefinitionType { get; protected set; }
  
      //abstract methods
      public abstract bool LoadDefinition(DefinitionTemplate template);
      public abstract bool SaveDefinition();
  }
  
  //concrete class representing a report-type definition.
  public class ReportDefinition : aDefinition
  {
      public string Definition { get; private set; }
      public string Parameters { get; private set; }
  
      public override bool LoadDefinition(DefinitionTemplate template)
      {
          this.DefinitionId = template.DefinitionId;
          this.Name = template.Name;
          this.DefinitionType = template.DefinitionType;
          this.Definition = template.Definition;
          this.Parameters = template.Parameters;
          return true;
      }
  
      public override bool SaveDefinition()
      {
          throw new NotImplementedException();
      }
  }
  
  //concrete class representing a configuration-type definition.
  public class ConfigDefinition : aDefinition
  {
      public string Parameters { get; private set; }
  
      public override bool LoadDefinition(DefinitionTemplate template)
      {
          this.DefinitionId = template.DefinitionId;
          this.Name = template.Name;
          this.DefinitionType = template.DefinitionType;
          this.Parameters = template.Parameters;
          return true;
      }
  
      public override bool SaveDefinition()
      {
          throw new NotImplementedException();
      }
  }
  
  //creator class that retreives the definitions from the server and creates the approprite object
  public static class DefinitionCreator
  {
      public void RetreiveDefinitions()
      {
          //sql statment to retreive definitions
          using(SqlCommand cmd = new SqlCommand("select * from definitions", new SqlConnection()))
          {
              using(SqlDataReader reader = cmd.ExecuteReader())
              {
                  while (reader.Read())
                  {
                      switch (reader["DefinitionType"].ToString())
                      {
                          case "ReportDefinition":
                              //create a report definition object
                              break;
                          case "ConfigDefinition":
                              //create a config definition object
                              break;
                      }
                  }
              }
          } 
      }
  }
  </code></pre>