.NET 可配置接口实现
我有一个接口,我希望使用工厂模式来实例化该接口的不同实现者。但我希望确定要在配置文件中实例化哪个实现者的详细信息。我希望以 RoleProvider 配置的方式拥有它:
<section name="MembershipProvider" type="MyOwn.UserManagement.Providers.MembershipProvider.CustomMembershipProviderConfigurationSection,MyOwn.UserManagement.Providers.MembershipProvider" allowDefinition="MachineToApplication" />
所以它是一种依赖注入技术。有人可以帮助我吗?
编辑:
实际情况是这样的:
我有一个接口 IDataExchange
并希望针对不同的客户使用不同的程序集来实现它。因此,我可能有两个程序集,它们具有实现 IDataExchange
的类,例如 DataExchange1
和 DataExchange2
。现在,当我部署时,根据我向哪个客户提供它,我希望在配置文件中设置程序集的详细信息(DataExchange1
或 DataExchange2
)。如果自动处理实例化决策,这还允许我或任何开发人员编写实现 IDataExchange
的新程序集以进行任何所需的更改。
那么我该怎么做呢?
我的实施:
谢谢大家。特别感谢@Pauli Østerø,
我从你的回答中得到了提示并实施了一个解决方案。
在配置文件中,我添加了以下内容:
<add key="Exchanger" value="DExchanger.DExchange1, DExchanger.DExchange1"/>
我添加了一个类(用作 DI 容器或抽象工厂)。该类是 DIContainer,包含以下方法:
public IDataExchange CreateInstance(string config)
{
var type = Type.GetType(config);
return (IDataExchange)Activator.CreateInstance(type);
}
在实例化时,我编写以下内容:
var config = ConfigurationManager.AppSettings.Get("Exchanger");
DIContainer x = new DIContainer();
var instance = x.CreateInstance(config);
Console.Write("Provide your input please: ");
string inp=Console.ReadLine();
Console.WriteLine(instance.DoDataExchange(inp));
Console.ReadLine();
这给出了我正在寻找的结果。
我将请求大家提出反馈意见,以便进一步改进。
I have an interface and I wish to do a factory pattern to instantiate different implementor of the interface. But I wish to keel the details of which implementor to be instantiated in the config file. I shall like to have it in the fashion of RoleProvider configuration:
<section name="MembershipProvider" type="MyOwn.UserManagement.Providers.MembershipProvider.CustomMembershipProviderConfigurationSection,MyOwn.UserManagement.Providers.MembershipProvider" allowDefinition="MachineToApplication" />
So its kind of an dependency injection technique. Can somebody help me?
Edit:
The actual situation is like this:
I have an interface IDataExchange
and want to implement it with different assemblies for different customers. So I might have two assemblies that have classes implementing IDataExchange
say, DataExchange1
and DataExchange2
. Now when I deploy, depending upon to which customer I am providing it, I shall like to set the details of the assembly (either DataExchange1
or DataExchange2
) in the config file. This will also allow me or any developer to write new assemblies implementing IDataExchange
for any change required, if the instantiation decision is handled automatically.
So how can I do it?
My implementation:
Thanks to all of you. And special thanks to @Pauli Østerø
I have taken cue from your answers and implemented a solution.
In the config file I added the following:
<add key="Exchanger" value="DExchanger.DExchange1, DExchanger.DExchange1"/>
I added a class (to work as a DI container or the abstract factory). The class is DIContainer
containing the following method:
public IDataExchange CreateInstance(string config)
{
var type = Type.GetType(config);
return (IDataExchange)Activator.CreateInstance(type);
}
And while I am instantiating, I write the following:
var config = ConfigurationManager.AppSettings.Get("Exchanger");
DIContainer x = new DIContainer();
var instance = x.CreateInstance(config);
Console.Write("Provide your input please: ");
string inp=Console.ReadLine();
Console.WriteLine(instance.DoDataExchange(inp));
Console.ReadLine();
And this is giving me the result I was looking for.
I shall request all of you to put your feedback for further improvement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
向您的
CustomMembershipProviderConfigurationSection
添加一个名为Type
的属性,以便您可以在配置文件中像这样配置它。在您的
CustomMembershipProviderConfigurationSection
上,您创建一个名为的方法>CreateInstance
返回IDataExchange
。该方法的主体非常简单,当您需要
IDataExchange
引用时,您可以编写Add a property to your
CustomMembershipProviderConfigurationSection
namedType
so you can configure it like this in your config fileOn your
CustomMembershipProviderConfigurationSection
you create a method namedCreateInstance
that returnsIDataExchange
. The body of the method is pretty simpleand when you need an
IDataExchange
reference you write大多数依赖注入容器将能够通过其对XML配置的支持来完成您所要求的任务。
一些常见的 DI 容器有:
Most Dependency Injection Containers will be able to do what you ask for through their support for XML configuration.
Some common DI Containers are:
你应该看看MEF。
点击点击
它应该完全符合您的要求。
You should take a look at MEF.
Clickity click
It should do exactly what you are looking for.