title: Best way to do this generic abstract class in c#?
body: <p>I know I'm not doing this right, but I also know there is a way to do this. I'm trying to be as generic and abstract as possible, otherwise my code is going to get real messy. So I'm using strategy pattern here as well, which is the <code>GetAggregateClient()</code> method.</p>
  
  <p>I want to have an abstract class called <code>AbstractAggregate&lt;T&gt;</code>, so that it uses generics. The generic type will be a series of data classes (<code>BlogItem</code>, <code>ResourceItem</code>, and <code>AskItem</code>), which all inherit from <code>ListItem</code>.</p>
  
  <p>So that's the background info.</p>
  
  <p>The problem here is that I want <code>GetAbstractAggregate()</code> to return an instance of one of the client classes that implements <code>AbstractAggregate</code>, with the type of item specified depending on the enum passed in. However, I cannot return an <code>AbstractAggregate&lt;T&gt;</code>. The compiler won't let me, and that makes sense since, since the <code>AbstractAggregateFactory</code> class is not a generic.</p>
  
  <p>Does anyone know the best way to do this?</p>
  
  <p>Thanks a lot.</p>
  
  <pre><code>public static class AggregateHelper
  {
      public enum AggregateTypes { TankTruckBlog, AskTankTruck, Resources }
  }
  
  public static class AbstractAggregateFactory
  {
      public static AbstractAggregate&lt;T&gt; GetAggregateClient(AggregateHelper.AggregateTypes type)
      {
          switch (type)
          {
              case AggregateHelper.AggregateTypes.AskTankTruck:
                  return new AskTankTruckAggregate&lt;AskItem&gt;();
              case AggregateHelper.AggregateTypes.TankTruckBlog:
                  return new TankTruckBlogAggregate&lt;BlogItem&gt;();
              case AggregateHelper.AggregateTypes.Resources:
                  return new ResourcesAggregate&lt;ResourceItem&gt;();
              default:
                  throw new AggregateDoesNotExistException();
          }
      }
  }
  
  public abstract class AbstractAggregate&lt;T&gt;
  {
      public abstract List&lt;T&gt; GetAggregate(Guid[] resourcetypes);
      public abstract T GetSingle(string friendlyname);
  }
  
  public class AskTankTruckAggregate&lt;T&gt; : AbstractAggregate&lt;T&gt;
  {
      // not implemented yet
  }
  
  public class TankTruckBlogAggregate&lt;T&gt; : AbstractAggregate&lt;T&gt;
  {
      // not implemented yet
  }
  
  public class ResourcesAggregate&lt;T&gt; : AbstractAggregate&lt;T&gt;
  {
      // not implemented yet
  }
  </code></pre>