Related classes around generic types I'm wondering if this code can be simplified. I'm less than thrilled about the fact that I repeat the Draw method in both CatModel and DogModel. For that matter, I'm not real happy that that method is even in the models at all. Trying to move it up to the AnimalModel superclass forces a cast.

EDIT: The "Model" classes should probably have been named "ViewModel" as that's the role they are playing in the actual project.

namespace GenericInterface
{
  public interface IDrawer {
  }

  public interface IDrawer&lt;TModel&gt;: IDrawer
    where TModel: IAnimalModel&lt;TModel&gt; 
  {
    void Draw(TModel model);
  }

  public interface IAnimalModel 
  {
    void Draw();
  }

  public interface IAnimalModel&lt;TModel&gt;: IAnimalModel
    where TModel: IAnimalModel&lt;TModel&gt; 
  {
    IDrawer&lt;TModel&gt; GetDrawer();  // parent interface might like to know that it can get some kind of drawer, but it gets messy with the types.
  }

  public class CatDrawer: IDrawer&lt;CatModel&gt; {
    // eventually, we want to have different types of CatDrawers.
    public void Draw(CatModel model) {
      Console.WriteLine(model.NWhiskers + " whiskers");
    }
  }

  public abstract class AnimalModel {
    public abstract void Draw();
  }

  public abstract class AnimalModel&lt;TModel&gt;: AnimalModel, IAnimalModel&lt;TModel&gt; 
    where TModel: class, IAnimalModel&lt;TModel&gt; {
    public abstract IDrawer&lt;TModel&gt; GetDrawer();
  }

  public class CatModel: AnimalModel&lt;CatModel&gt; {
    // The repetition of "CatModel" in the above line is less than ideal. It is driven by the fact that, as best I can tell, there is no way to say that a type parameter should always be equal to the name of the current class.
    public int NWhiskers { get; set; }
    // &lt;summary&gt;Would eventually become IDrawer&lt;CatModel&gt; GetDrawer(DrawerFactory factory) { return factory.GetCatDrawer(this); }
    public override IDrawer&lt;CatModel&gt; GetDrawer() {
      return new CatDrawer();
    }
    // Once we have different CatDrawers, this would become 
    // public override void Draw(DrawerFactory factory) { this.GetDrawer(factory).Draw(this); {
    public override void Draw() {
      this.GetDrawer().Draw(this);
    }
  }

  public class DogModel: AnimalModel&lt;DogModel&gt; 
  {
    public string Breed { get; set; }
    public override IDrawer&lt;DogModel&gt; GetDrawer() {
      return new DogDrawer();
    }
    public override void Draw() {
      this.GetDrawer().Draw(this);
    }
  }

  public class DogDrawer: IDrawer&lt;DogModel&gt; {
    public void Draw(DogModel model) {
      Console.WriteLine(model.Breed);
    }
  }

  class MainClass {
    public static void Main(string[] args) {
      // Works. Output is:
      // 9 Whiskers
      // collie
      CatModel cat = new CatModel();
      cat.NWhiskers = 9;
      DogModel dog = new DogModel();
      dog.Breed = "collie";
      List&lt;IAnimalModel&gt; animals = new List&lt;IAnimalModel&gt;(){ cat, dog };
      foreach (IAnimalModel animal in animals) {
        animal.Draw();
      }
    }
  }
}
You have the tightest coupling of interfaces I have seen in my life. You are also restricting them in abnormal manners. For example, IDrawer should not need to know about the IAnimalModel. Instead, it should be worried about IDrawableModel, which IAnimalModel can worry about. This way, the coupling is reduced and things are coupled with appropriate interfaces/classes. You want to reduce your coupling to only couple around what needs to depend on what else. Things that are generic like IDrawer shouldn't have to worry about more specific things. They should be, well, for the buzzword: generic.

Think about it as "what happens if I need something else to draw?" If you have another class/interface that has to draw something, and you need to modify IDrawer to count that in, then something is fishy.



You could test this better by developing around Test-Driven Development. You have interfaces for a reason, they are also very good at TDD. It's easy to mock things with them. (So that you can verify that whatever the thing is works. Especially with generics.)



Please do us all a favour and format your code more appropriately. C# does not use Egyptian braces (at least, not by any best-practices). (Java and JAVA do, but this is neither Java nor JAVA.)


namespace GenericInterface
{
    public interface IDrawer
    {
    }

    public interface IDrawer&lt;TModel&gt; : IDrawer
      where TModel : IAnimalModel&lt;TModel&gt;
    {
        void Draw(TModel model);
    }