如何使用一些提供相同服务的提供商?
我注册了服务&具有某些 IoC 工具的提供程序(例如 StructreMap)
public interface IProvider
{
void Load(int id);
}
public class Provider : IProvider
{
public void Load(int id) {...}
}
public interface IService
{
void Load(int id);
}
public class Service : IService
{
public Service(IProvider provider) { }
public void Load(int id) { provider.Load(id); }
}
现在我有另一个提供程序
public class Provider2 : IProvider
{
public void Load(int id) {...}
}
,我想注册该提供程序(Provider2)并从 IoC 工具获取具有 Provider2 的服务实例。我认为执行此操作的唯一方法是创建将从 IService2 继承的 Service2,最后一个将从 IService 继承。现在我可以获得 IService2 类型的实例,它将使用 Provider2 启动提供程序。
public interface IService2 : IService
{ }
public class Service2 : IService2
{
public Service2(IProvider2 provider) { }
public void Load(int id) { provider.Load(id); }
}
我的问题是:对于每个新的提供者,即使服务的功能应该保持不变,我也需要为服务编写新的类?
I register the Service & Provider with some IoC tool(e.g. StructreMap)
public interface IProvider
{
void Load(int id);
}
public class Provider : IProvider
{
public void Load(int id) {...}
}
public interface IService
{
void Load(int id);
}
public class Service : IService
{
public Service(IProvider provider) { }
public void Load(int id) { provider.Load(id); }
}
Now I have another provider
public class Provider2 : IProvider
{
public void Load(int id) {...}
}
I want to register that provider(Provider2) and to get the instance of Service with Provider2 from IoC tool. The only way I can see to do this is to create Service2 that will inherit from IService2 and the last will inherit from IService. And now I can get instance of type IService2 that will initiate provider with Provider2.
public interface IService2 : IService
{ }
public class Service2 : IService2
{
public Service2(IProvider2 provider) { }
public void Load(int id) { provider.Load(id); }
}
My question is: for every new Provider I need to write new class for Service even if the functionality of the service should remain the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不需要这样做。
我建议您实现一个抽象工厂模式,以便根据运行时已知的值选择正确的提供程序实现。
有关示例,请查看此处:http://alsagile.com/archive/2010/06/28/Abstract-Factory-and-IoC-Container-in-asp-net-MVC.aspx。
它在 MVC 上下文中,但可以在其他场景中轻松实现。
示例:
您可以像这样注册您的服务(伪代码):
您需要一种将容器注入抽象工厂的方法。例如,您可以像这样包装它:
和实现:
并设置 StructureMap 来解决对抽象工厂的依赖关系:
然后在您的服务中,您应该像这样注入提供程序工厂
这应该可以工作。
No, you don't need to do that.
I would advice you to implement an abstract factory pattern for selecting the right implementation of provider depending on the value know at runtime.
For an example please look here : http://alsagile.com/archive/2010/06/28/Abstract-Factory-and-IoC-Container-in-asp-net-MVC.aspx.
It's in MVC context but can be easily implemented in other scenarios.
Example :
Youd could register your service like this (pseudo code) :
You need a way to inject the container in your Abstract factory. You could wrapp it for example like this:
And the implementation :
And setup StructureMap to resolve dependencies to your abstract factory like that :
Then into your Service you should inject the provider factory like this
This should work.