When are there just too many generics?

Generics are a pretty cool language feature. They provide type safety and felxible reuse at the same time, and I reckon that’s pretty cool. However as with all the other cool language features – like implicit typing, you need to be responsible with them. The question is though, when do you have too many generics?One of the peices of code I wrote this week was a generic entity retrieval command. I already had a command signature that looked a little like this:

internal void MyCommand : Command <TRequest,TResponse>

No problems here. A request, a response.Now this was ok, but I couldn’t really get an entity of any type like this. I tried putting the type in the request as a parameter, but it didn’t really work. So then I moved to this..

internal void MyCommand<TEntityType> : Command <TRequest,TResponse>

Now I’ve got 3 generic parameters. 1 that specifies the type of entity I’m requesting, and 1 each for the request and response types. I might be getting a bit zesty with my generics. However this lets me go and get a table from my context like so:

Table<TEntityType> myQueryTable = myDataContext.GetTable<TEntityType>

Which is nice I think, as it lends itself to almost any table that you’ve got mapped in your data context.

Now, in case there weren’t already enough generics in my class definition, I also needed to know what type I was returning in my response! So….

internal void MyCommand<TEntityType> : Command<TRequest,TResponse<TEntityType>>
So I really still only have 3 types here, but now I have the TEntityType on the response as well. Do I have too many generics? Am I creating a readability and maintainability issue here? I’m eagerly awaiting the code review on this one actually… I’m interested to see what is said.

What do you think? 1 too many? 2 too many? What the hell am I doing? Would you have done it differently?


4 thoughts on “When are there just too many generics?

  1. Anonymous

    Mr. Steve,

    I think there may be a bit of a problem with the way you are using generics. If you post the code it will be easier to give some advice.


  2. Darren Neimke

    Hi Steve, not quite sure why you had to add the extra generics beyond Command. The basic idea is that once you declare a type of:

    var cmd = new Command() ;

    Then you should have all of the properties at your disposal via the .Response and .Request properties like so:

    cmd.Request.Whatever = “qwerty” ;


    ComplexType t = cmd.Response.Whatever ;

  3. stephengodbold Post author


    I couldn’t work out another way to dynamically retrieve the table I wanted to query.

    So the Table<T> queryTable = myContext.GetTable<T> seemed to be my only option. I’m thinking the TEntity type should be on the Request and Response though as that would hold more true with the command pattern as you’ve suggested.


  4. Jamal Mavadat

    Well, I’m not much into your particular context, but guess there could be better design alternatives here.

    And in regard to the question of how many generic params you should use, I’d say that template designs are generally having less readability score for newbies and therefore if consumers are not much relaxed with template-designs, that’s okay you downgrade to v1-style design! But, if your design users are experts then take advantage of template design techniques; looking for a sample of template designs?! checkout C++ Standard Template Library (STL) or VC++ ATL!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s