IServiceProvider. Why does this interface exist?

K

Kjetil Bjerknes

I'm currently creating some user controls in C#, and I've come across
the interface IServiceProvider. This interface must be one of the
worst examples of bad design I've ever seen. It contains one single
method, with the following signature:

object GetService(Type serviceType);

Can anyone tell be what this method is supposed to do? OK, I can see
that it's supposed to return a "service", but what exactly is a
service? According to the method signature it can be absolutely
everything, and the interface could just as well be called
IObjectRetriever.

To make matters worse, this interface is implemented/extended by a
number of classes and other interfaces, and none of them seem to think
it's important to inform what kind of services they return!

Why not create the following interface while we're at it, and let
every object implement it :). Then we wouldn't need to add all those
stupid methods with descriptive names:

public interface IIronicTask {
object DoIronicTask(int taskID, object[] args);
}


__Please__ enlighten me if you're able to see the usefulness of
IServiceProvider! Or even better, if you know of some way to learn
what services it can provide. For now I'm particularly interested in
services that can be returned from ISite.GetService(),
Component.GetService() and the IServiceProvider parameter of
UITypeEditor.EditValue().

As a side note: It seems that System.ComponentModel.Component does not
implement IServiceProvider, however it does have a GetService()
method(?). The more I get into C# and the class library, words like
inconsistent and unintuitive seems to spring to mind a bit to often.


</KB>
 
G

Guest

it doesn't mean much by itself, but it's part of the .NET lightweight container implementation. the lightweight container is basically a plugin architecture. the VS IDE uses this quite heavily

----- Kjetil Bjerknes wrote: ----

I'm currently creating some user controls in C#, and I've come acros
the interface IServiceProvider. This interface must be one of th
worst examples of bad design I've ever seen. It contains one singl
method, with the following signature

object GetService(Type serviceType)

Can anyone tell be what this method is supposed to do? OK, I can se
that it's supposed to return a "service", but what exactly is
service? According to the method signature it can be absolutel
everything, and the interface could just as well be calle
IObjectRetriever

To make matters worse, this interface is implemented/extended by
number of classes and other interfaces, and none of them seem to thin
it's important to inform what kind of services they return

Why not create the following interface while we're at it, and le
every object implement it :). Then we wouldn't need to add all thos
stupid methods with descriptive names

public interface IIronicTask
object DoIronicTask(int taskID, object[] args)



__Please__ enlighten me if you're able to see the usefulness o
IServiceProvider! Or even better, if you know of some way to lear
what services it can provide. For now I'm particularly interested i
services that can be returned from ISite.GetService()
Component.GetService() and the IServiceProvider parameter o
UITypeEditor.EditValue()

As a side note: It seems that System.ComponentModel.Component does no
implement IServiceProvider, however it does have a GetService(
method(?). The more I get into C# and the class library, words lik
inconsistent and unintuitive seems to spring to mind a bit to often


</KB>
 
N

Nicholas Paldino [.NET/C# MVP]

Kjetil,

See inline.

Kjetil Bjerknes said:
I'm currently creating some user controls in C#, and I've come across
the interface IServiceProvider. This interface must be one of the
worst examples of bad design I've ever seen. It contains one single
method, with the following signature:

object GetService(Type serviceType);

Can anyone tell be what this method is supposed to do? OK, I can see
that it's supposed to return a "service", but what exactly is a
service? According to the method signature it can be absolutely
everything, and the interface could just as well be called
IObjectRetriever.

A "service" here is functionality that the site provides which might not
necessarily be implemented by the container itself. For example, say that
the container offers a logging service. If the id (in this case the type)
for the "service" is well known, then the site can return the "service"
(read: object) that provides the expected functionality. Expecting the site
itself prevents the architecture from being pluggable.
To make matters worse, this interface is implemented/extended by a
number of classes and other interfaces, and none of them seem to think
it's important to inform what kind of services they return!

I agree with you here. It would be helpful if there was an extra method
indicating the services that it provides. Since the number of services it
provides are potentially limitless, it is impossible to cycle through all
the known types, passing them to GetService to see what returns null and
what does not.
Why not create the following interface while we're at it, and let
every object implement it :). Then we wouldn't need to add all those
stupid methods with descriptive names:

public interface IIronicTask {
object DoIronicTask(int taskID, object[] args);
}


__Please__ enlighten me if you're able to see the usefulness of
IServiceProvider! Or even better, if you know of some way to learn
what services it can provide. For now I'm particularly interested in
services that can be returned from ISite.GetService(),
Component.GetService() and the IServiceProvider parameter of
UITypeEditor.EditValue().

IServiceProvider is very, very valuable if you want to actually enable
the idea of a pluggable architecture. I think you bring up a good point
about being able to provide a list of ^what^ services it provides.
As a side note: It seems that System.ComponentModel.Component does not
implement IServiceProvider, however it does have a GetService()
method(?). The more I get into C# and the class library, words like
inconsistent and unintuitive seems to spring to mind a bit to often.

GetService is meant as a helper method for classes that derive from
Component. It basically gets the site from the component and calls
GetService from that. It's shorthand for:

(this.Site == null ? null : this.Site.GetService(type));

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top