[转]关于System.IServiceProvider的探讨

以下内容转自论坛http://bytes.com/topic/c-sharp/answers/245720-iserviceprovider-why-does-interface-exist

Kjetil,

See inline.

"Kjetil Bjerknes" <kb@trollsoft.com> wrote in message
news:1f69b085.0405120837.14f9e0d0@posting.google.c om...[color=blue]
> 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.[/color]

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.
[color=blue]
>
> 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![/color]

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.
[color=blue]
>
> 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().[/color]

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.
[color=blue]
>
> 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.[/color]

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.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
[color=blue]
>
>
> </KB>[/color]

原文地址:https://www.cnblogs.com/ChenZB/p/1742948.html