[转]关于System.IServiceProvider的探讨
2010-05-24 18:19
465 查看
以下内容转自论坛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]
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]
相关文章推荐
- 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.VisualStudio.Tools.Applications.Runtime.IHostServiceProvider”
- Could not load type 'System.Data.Services.Providers.IDataServiceUpdateProvider' from assembly 'System.Data.Services, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
- 关于sharepoin teamservice的权限控制-探讨
- 在Spring中DAO与Service关于依赖注入写法探讨
- 关于”System.ServiceModel.Activation.WebServiceHostFactory“与"<webHttp/>"以及RestFul/启用了Ajax的WCF服务
- Android 关于getSystemService()方法的一些常用服务
- JNDI File System Service Provider
- 在SharePoint 2010 中使用REST时提示:未能加载类型System.Data.Services.Providers.IDataServiceUpdateProvider的解决办法
- 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法
- 关于弹出GnomeSystemTrayService.initNative错误的原因
- 关于运行mysql_install_db命令导致sudo systemctl start /restart mariadb.service启动失败问题
- 关于VS.NET RSACryptoServiceProvider的疑惑
- US Patent 7277705 - Method, apparatus, and system for selecting a service provider system
- 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法
- [李景山php]每天laravel-20160916|FileSystemServiceProvider
- 关于Laravel Service Provider开发设置延迟加载时遇到的问题详解
- Android 关于getSystemService()方法的一些常用服务
- 无法将类型为“System.__ComObject”的 COM 对象强制转换为接口类型“Microsoft.VisualStudio.OLE.Interop.IServiceProvider”。
- [李景山php]每天laravel-20160916|FileSystemServiceProvider
- System.Drawing.Design.UITypeEditor自定义控件属性GetEditStyle(ITypeDescriptorContext context),EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)