About System. IServiceProvider

Source: Internet
Author: User

The following content is transferred from Forum 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: The 1f69b085.0405120837.14f9e0d0@posting.google.c om... [color = blue]
> I'm currently creating some user controls in C #, and I 've come into operation SS
> The interface IServiceProvider. This interface must be one of
> 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
> Service? According to the method signature it can be absolutely
> Everything, and the interface coshould 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
> 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 wocould 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
> Iserviceprovider! Or even better, if you know of some way to learn
> What services it can provide. For now I'm sure interested in
> Services that can be returned from isite. getservice (),
> Component. getservice () and the iserviceprovider parameter
> 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 cballs
Getservice from that. It's shorthand:

(This. Site = NULL? Null: This. Site. getservice (type ));

Hope this helps.

--
-Nicolas paldino [. Net/C # MVP]
Mvp@spam.guard.caspershouse.com
[Color = Blue]
>
>
> </Kb> [/color]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.