统一框架
让我澄清我的问题,我说有 100 个类,所有这 100 个类都实现了 1 个接口,比如 IDependency,假设我的类名是 Customer,它实现了 IDependency,
public class Customer : IDependency
public class Order:IDependency
它会一直持续到 100 个类
,我在 web.config 中注册这个类,如下
<type type="IDependency" mapTo="Customer"></type>
<type type="IDependency" mapTo="Order"></type>
所示错了,因为我收到错误 IDependency
已声明
现在我需要的是 web.config 中应该只有 1 行,我应该能够解析我的所有类,但我不想 < code>RegisterType 在我的代码中,就像
Container.RegisterType<IDependency,Customer>();
Container.RegisterType<IDependency,Order>();
我不想在代码文件中写入这些行一样,要求所有内容都应该通过配置文件完成。
你们认为这是否有效,如果是的话,您至少可以告诉我如何处理它吗?
总的来说,我认为应该有 1 个类 BaseClass
,它首先实现我的 IDependency
接口,我的所有 100 个类都将继承该类,我的配置文件应该像
<type type="IDependency" mapTo="BaseClass"></type>
Please let我知道您是否需要对此进行更多澄清,因为我对此感到非常震惊
Let me clear with my Question, I have say 100 classes all these 100 classes implements 1 interface say IDependency, say suppose my classname is Customer it implements IDependency
public class Customer : IDependency
public class Order:IDependency
it goes on till 100 classes
And I am registering this class in web.config like
<type type="IDependency" mapTo="Customer"></type>
<type type="IDependency" mapTo="Order"></type>
this will be wrong because i am getting error IDependency
already declared
Now what i need is there should be only 1 line in web.config and i should be able to resolve all my classes and I don't want to RegisterType
in my code like
Container.RegisterType<IDependency,Customer>();
Container.RegisterType<IDependency,Order>();
I don't want to write these lines in my code file the requirement is like that all should be done through config file.
Do you guys think is this valid if yes can you at least give me idea how to approach it.
Overall what i think is there should be 1 class say BaseClass
which implement my IDependency
Interface first and all my 100 classes will inherit that class and my config file should be like
<type type="IDependency" mapTo="BaseClass"></type>
Please let me know if you need more clarification on this because I am really struck with this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能同时为两个类注册相同的接口。在您的代码示例中:
Unity 容器将执行的操作是将
Order
注册为调用Resolve
时要返回的类,并忘记之前的客户
类注册。无论如何,我不太明白你的目标是什么。如果您可以为同一接口注册多个类,那么您希望在调用
Resolve
时获得什么,或者您希望在其中具有IDependency
参数的类获得什么?构造函数接收?编辑基于Dotnet Coder的澄清:
如果您想要的只是在运行时根据配置决定要注册哪个类,您可以在配置文件中拥有一个值(web.config、Settings、或类似的)告诉你想要关联哪个类。例如:
You can't register the same interface for two classes at the same time. In your code sample:
what the Unity container will do is to register
Order
as the class to return when you invokeResolve<IDependency>
, and forget about the previousCustomer
class registration.Anyway I don't quite understand which is your goal. If you could register multiple classes for the same interface, what would you expect to obtain when invoking
Resolve<IDependency>
, or what would you expect the classes having anIDependency
parameter in the constructor to receive?EDIT based on clarification from Dotnet Coder:
If all you want is to decide at runtime which class you want to register based on configuration, you can have one value in the configuration file (web.config, Settings, or the like) telling which class you want to associate. For example: