The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Use of Generics to Eliminate Casting

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter G Provost

Posts: 849
Nickname: pprovost
Registered: Aug, 2003

Peter G Provost is a Solution Architect for Interlink Group in Denver, CO.
Use of Generics to Eliminate Casting Posted: May 14, 2005 9:07 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter G Provost.
Original Post: Use of Generics to Eliminate Casting
Feed Title: Peter Provost's Geek Noise
Feed URL: /error.aspx?aspxerrorpath=/Rss.aspx
Feed Description: Technology news, development articles, Microsoft .NET, and other stuff...
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter G Provost
Latest Posts From Peter Provost's Geek Noise

Advertisement

This post is really more of a question than something educational, so please if you have an opinion on this, post a comment.

Suppose you are creating a new class that implements an older, pre-Whibey interface. For sake of argument, lets suppose you are implementing System.ComponentModel.IServiceProvider

This interface is defined like this:

public interface IServiceProvider
{
Object GetService(Type serviceType);
}

Because IServiceProvider.GetService returns an Object, you have to cast it before you can use it, resulting in the kind of ugly code shown here:

public void Foo()
{
IMyService service = (IMyService) provider.GetService(typeof(IMyService));
}

Of course, if this interface were being created today, we may defined it like this to avoid all that nastiness:

public interface IServiceProvider
{
T GetService<T>();
}

That’s not too bad eh? Now we can call it with much nicer code like this:

public void Foo()
{
IMyService service = provider.GetService<IMyService>();
}

Now here’s the question for all of you…

Given that this older interface exists, and will not likely be updated to have the kind of generics support I show here, should we use the generics form of GetService anyway? In other words, if you were creating a new class that implemented IServiceProvider, would you provide not only the required interface for use by people who expect you to be an IServiceProvider, but also provide the new generic method for people who know about it?

There is a part of my that likes it, but there is another part of me that thinks the interface clutter created by doing this isn’t worth it. We all know that behind the scenes the new implementation is just doing this…

public T GetService<T>()
{
return (T) GetService(typeof(T));
}

…so it isn’t like this is saving me a cast or anything.

What do you think? Is this kind of thing good or bad?

Read: Use of Generics to Eliminate Casting

Topic: WeProgram.NET Meeting Thurs, May 12 Previous Topic   Next Topic Topic: Visual Studio 2005 IDE Automatically Produces Code to Implement IDisposable

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use