.NET 可配置接口实现

发布于 2024-10-10 18:09:44 字数 1661 浏览 6 评论 0原文

我有一个接口,我希望使用工厂模式来实例化该接口的不同实现者。但我希望确定要在配置文件中实例化哪个实现者的详细信息。我希望以 RoleProvider 配置的方式拥有它:

<section name="MembershipProvider" type="MyOwn.UserManagement.Providers.MembershipProvider.CustomMembershipProviderConfigurationSection,MyOwn.UserManagement.Providers.MembershipProvider" allowDefinition="MachineToApplication" />

所以它是一种依赖注入技术。有人可以帮助我吗?

编辑:

实际情况是这样的:

我有一个接口 IDataExchange 并希望针对不同的客户使用不同的程序集来实现它。因此,我可能有两个程序集,它们具有实现 IDataExchange 的类,例如 DataExchange1DataExchange2。现在,当我部署时,根据我向哪个客户提供它,我希望在配置文件中设置程序集的详细信息(DataExchange1DataExchange2)。如果自动处理实例化决策,这还允许我或任何开发人员编写实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热鲨 2024-10-17 18:09:44

向您的 CustomMembershipProviderConfigurationSection 添加一个名为 Type 的属性,以便您可以在配置文件中像这样配置它。

<group>
   <MembershipProvider type="... my concrete type" />
</group>

在您的 CustomMembershipProviderConfigurationSection 上,您创建一个名为 的方法>CreateInstance 返回IDataExchange。该方法的主体非常简单

public IDataExchange CreateInstance()
{
   var type = Type.GetType(this.Type);
   return (IDataExchange)Activator.CreateInstance(type);
}

,当您需要 IDataExchange 引用时,您可以编写

var config = (CustomMembershipProviderConfigurationSection)ConfigurationManager.GetSection("group/MembershipProvider");
var instance = config.CreateInstance();

Add a property to your CustomMembershipProviderConfigurationSection named Type so you can configure it like this in your config file

<group>
   <MembershipProvider type="... my concrete type" />
</group>

On your CustomMembershipProviderConfigurationSection you create a method named CreateInstance that returns IDataExchange. The body of the method is pretty simple

public IDataExchange CreateInstance()
{
   var type = Type.GetType(this.Type);
   return (IDataExchange)Activator.CreateInstance(type);
}

and when you need an IDataExchange reference you write

var config = (CustomMembershipProviderConfigurationSection)ConfigurationManager.GetSection("group/MembershipProvider");
var instance = config.CreateInstance();
岁月流歌 2024-10-17 18:09:44

大多数依赖注入容器将能够通过其对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:

居里长安 2024-10-17 18:09:44

你应该看看MEF。
点击点击

它应该完全符合您的要求。

You should take a look at MEF.
Clickity click

It should do exactly what you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文