• [转]关于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]

  • 相关阅读:
    Windows Server 2008关闭internet explorer增强的安全配置
    【转载并整理】mysql分页方法
    Mysql:MyIsam和InnoDB的区别
    【转载】web网站css,js更新后客户浏览器缓存问题,需要刷新才能正常展示的解决办法
    【转载】java前后端 动静分离,JavaWeb项目为什么我们要放弃jsp?
    Redis命令汇总
    Redis介绍及安装
    【转载】Spring Cache介绍
    简单示例:Spring4 整合 单个Redis服务
    【转载整理】Hibernater的锁机制
  • 原文地址:https://www.cnblogs.com/ChenZB/p/1742948.html
Copyright © 2020-2023  润新知