如何将服务放入ServiceLocator? [英] How do I get the services into a ServiceLocator?

查看:48
本文介绍了如何将服务放入ServiceLocator?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在我的项目中,我想将代码分成三个不同的程序集:
1.)前端
2.)后端
3.)后端的界面
基本上,后端将实现后端接口定义的所有接口.现在,前端应该只知道后端的接口,而不是后端本身.
因此,前端和后端都应具有对后端接口的引用.但是他们不应该彼此认识.
现在,我需要一个ServiceLocator,它为前端提供后端接口的实现.

Hi,
in my project, I would like to separate the code into three different assemblies:
1.) The frontend
2.) The backend
3.) The backend''s interface
Basically, the backend will implement all the interfaces that are defined by the backend''s interface. Now the frontend should only know about the backend''s interface, not the backend itself.
Thus both, the frontend and the backend should have a reference to the backend''s interface. But they should not know each other.
Now I need a ServiceLocator that provides the frontend with implementations of the backend''s interface.
Something like

DoSomethingService dss = ServiceLocator.GetService<DoSomethingService>();



但是ServiceLocator如何获得有关实现的信息?
最简单的方法是将其放入后端,但是前端在那里会有一个依赖关系,而我不想拥有这个依赖关系.
通过XML并在ServiceFactory中使用"Type.GetType(string)"定义接口和实现也不起作用,因为在尝试从字符串创建类型时,我需要引用该类型的实现. >
我还有什么其他选择?

该应用程序是具有xcopy-deployment和两个程序集的ASP.NET-Application:后端和后端的接口.

您还有其他想法如何实现吗?



But how does the ServiceLocator get the information about the implementations?
The easy way would be to put it into the backend, but then the frontend would have a dependency there, which I don''t want to have.
Defining the interface and implementation via XML and using "Type.GetType(string)" in the ServiceFactory does not work either, since I need a reference to the type''s implementation when trying to create the type from the string.

What other options do I have?

The application is an ASP.NET-Application with xcopy-deployment and two assemblies used: The backend and the backend''s interface.

Do you have any other ideas, how I can implement this?

推荐答案

仅供参考.
简单的解决方案:只需在类名中添加,AssemblyName"即可.
无论如何,我还是没有决定,因为它包含所有服务实现者都必须编辑的中央配置文件(后端和后端的接口将有多个程序集).
因此,我创建了一个配置"项目,该项目被允许了解所有服务实现程序集.然后,该项目会调用所有服务实现程序集,并要求它们注册其服务.
不幸的是,必须在前端初始化期间触发配置.因此,前端可以间接引用服务实现.但是我可以忍受这个.
Just for the record.
Simple solution: Just add ",AssemblyName" to the class-name.
I did not decide for this anyway, since it contains a central configuration-file that all service-implementors have to edit (there will be multiple assemblies for the backend and the backend''s interface).
Thus I created a "Configuration"-project that is allowed to know all the service-implementing assemblies. This project does then call all the service-implementing assemblies and asks them to register their services.
Unfortunately, the configuration has to be triggered during the frontend''s initialization. Thus the frontend has an indirect reference to the service-implementations. But I can live with this.


这篇关于如何将服务放入ServiceLocator?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆