Skip to content
MemberImage:DjMax

DjMax/Code/Generic Challenge Povo Staff

Member Since January 25, 2007 •  My Contributions • Reviews • Lists  • Send a Message  • Add to Friends
So Povo consists of two major code sets, a generic wiki rendering library and then all the Povo-specific stuff like databases, location code, Lingua (the scripting language), etc. On the wiki side, there are three work horse interfaces, and a bunch of related classes:
  1. IWikiTitle - the name of a wiki page. It includes a namespace and a title. In the Povo specific classes, it also includes a region (e.g. Boston).
  2. IWikiPage - the actual page including text for a title, created in the context of an IWikiFactory
  3. IWikiFactory - a wiki factory is in charge of dealing with the persistent store to get pages
  4. WikiRenderer - a real class that renders pages in the context of a factory
Then on the Povo side we have implementations of these interfaces and classes as you would expect:
  1. PovoTitle
  2. PovoPage
  3. PovoFactory
  4. PovoRenderer
But here is my challenge... I want to modify the wiki side to use C# Generics, so that I don't constantly have to cast to PovoTitle, PovoPage, etc. The problem is that when I tried it I found myself crashing against two problems:
  1. I don't see a way to refer to "this" type in a generic.
  2. I can't constrain a type parameter to another generic without knowing the full types (I can see why)
In terms of the first problem, the reason this is bad is because I want to derive from each of the base classes like so:
  1. PovoTitle : IWikiTitle<PovoFactory>
  2. PovoPage : IWikiPage<PovoTitle,PovoFactory>
  3. PovoFactory : IWikiFactory<PovoTitle, PovoPage>
  4. PovoRenderer : WikiRenderer<PovoFactory, PovoTitle, PovoPage>
The rub is that PovoFactory, for example, has methods to return a PovoRenderer. So to get strong typing on that, I'd need to pass the PovoRenderer type to the factory. But the renderer uses a factory, so I'm in circular madness.
One solution would be if there was a way to constrain a type like so:
class WikiRenderer<FactoryType, TitleType, PageType> where FactoryType : IWikiFactory<TitleType, PageType, RendererType>
The important bit here is I can't know what RendererType is in WikiRenderer, and I don't actually care. The only reason I need the constraint is to be sure that FactoryType implements the generic IWikiFactory interface. I can see why this would be hard to do for the compiler, because I'm basically saying "don't resolve this now, figure out the whole chain when somebody instantiates me." But without it, I think I'm lost.
Scribble some thoughts on the graffiti on the right, or the talk page.
show GRAFFITI
Write a quick note about this place .