本文介绍了依赖注入-在运行时通过配置文件选择DLL和类实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API DLL(例如, API.dll ),除了许多其他想法外,它还提供了一个抽象类( AbstractClass )。



现在利用该 AbstractClass 我已经在两个不同的dll上实现了它:




  • First.API.Implementation.dll ConcreteImplementation1

  • Second.API.Implementation.dll ConcreteImplementation2



ConcreteImplementation1和ConcreteImplementation2都是同一抽象类的实现。



我想要的是一个应用程序,在该应用程序中,我可以选择使用这两个dll中的哪个,并通过该应用程序选择要使用的实现,而无需用户在其中进行任何更改。代码,如果可能的话,也可以不停止应用程序。



某些配置文件,我可以使应用程序使用我想要的任何实现。

 < appconfiguration> 
< implementation_to_use>
< dll> First.API.Implementation.dll< / dll>
< class> ConcreteImplementation1< / class>
< / implementation_to_use>
< / appconfiguration>

除了依赖注入的概念外,我几乎对它一无所知,但我想那是完美的选择



我研究了几个DI / IoC库,但是我并不熟悉所有的概念和名称。我可以使用我想要的任何库。对于我来说,这些是最常用的:,和



此外,除了所有dll和实现之外,我还需要指示该应用程序要使用的文件。我可以在同一个文件中指出它的路径吗?



我只需要一些提示和说明来实现这样的事情。



谢谢。

解决方案

要开始使用,请创建一个控制台应用程序,其中应包括: / p>

structuremap.config:

 <?xml version = 1.0  encoding = utf-8吗? 
< StructureMap MementoStyle = Attribute>
< DefaultInstance
PluginType = DemoIoC.AbstractBase,DemoIoC
PluggedType = DemoIoC.ConcreteImplementation1,DemoIoC
Scope = Singleton />
< / StructureMap>

PluginType和PluggedType属性为 FullyQualifiedClassName,AssemblyName
默认情况下,它将查找可执行文件文件夹中的程序集,我不确定您如何为程序集指定另一个位置



对于Scope有很多选项,例如单例,瞬态等



Program.cs:

 命名空间DemoIoC 
{
使用系统;
使用StructureMap;

公共静态类程序
{
public static void Main(string [] args)
{
//在这里,您从配置文件中初始化structuremap 。
// //每当structuremap.config文件更改
ObjectFactory.Initialize(x =>
{
x时,您都可以使用FileSystemWatcher重新初始化
//。 UseDefaultStructureMapConfigFile = true;
});

var concrete = ObjectFactory.GetInstance< AbstractBase>();

concrete.Method1();

Console.ReadKey(true);
}
}
}

AbstractBase.cs:

 命名空间DemoIoC 
{
公共抽象类AbstractBase
{
公共抽象void Method1 ();
}
}

ConcreteImplementation1.cs:

 命名空间DemoIoC 
{
使用系统;

公共类ConcreteImplementation1:AbstractBase
{
公共重写void Method1()
{
Console.WriteLine( Called ConcreteImplementation1);
}
}
}


I've an API DLL (API.dll, for example) which, in addition to many other thinks, makes available an abstract class (AbstractClass).

Now making use of that AbstractClass I've implemented it on two different dlls:

  • First.API.Implementation.dll with ConcreteImplementation1
  • Second.API.Implementation.dll with ConcreteImplementation2

Both ConcreteImplementation1 and ConcreteImplementation2 are implementation of the same abstract class.

What I want is an application where I can choose which of those two dlls to use and, through that, choose which implementation to use without the user having to change anything within the code and, if possible, without stopping the application.

Some configuration file where I can bring the application to use whatever implementation I want. Something like:

<appconfiguration>
    <implementation_to_use>
        <dll>First.API.Implementation.dll</dll>
        <class>ConcreteImplementation1</class>
    </implementation_to_use>
</appconfiguration>

I know near to nothing about dependency injection, apart from its concept, but I guess thats the perfect fit for this task.

I've researched several DI/IoC libraries but I'm not familiar with all the concepts and names. I can use whatever library I want. For what I can say these are the most used: StructureMap, Ninject and Sprint.NET

Moreover, apart from all the dlls and implementation I need to indicate a file to be used by that application. Can I indicate its path in that same file?

I just need some tips and directions to implement such a thing. Some examples using one of those libraries, would be awesome.

Thanks.

解决方案

To get you started using StructureMap, create a console application, include in it:

structuremap.config:

<?xml version="1.0" encoding="utf-8" ?>
<StructureMap MementoStyle="Attribute">
  <DefaultInstance
    PluginType="DemoIoC.AbstractBase,DemoIoC"
    PluggedType="DemoIoC.ConcreteImplementation1,DemoIoC"
    Scope="Singleton" />
</StructureMap>

The PluginType and PluggedType attributes are "FullyQualifiedClassName,AssemblyName"By default it will look for assemblies in the executable folder, I'm not sure how you would specify another location for the assemblies

There are plenty of options for Scope, e.g. Singleton, Transient, etc

Program.cs:

namespace DemoIoC
{
    using System;
    using StructureMap;

    public static class Program
    {
        public static void Main(string[] args)
        {
            // here you initialize structuremap from the config file.
            // You could probably use a FileSystemWatcher to reinitialize
            // whenever the structuremap.config file changes
            ObjectFactory.Initialize(x =>
            {
                x.UseDefaultStructureMapConfigFile = true;
            });

            var concrete = ObjectFactory.GetInstance<AbstractBase>();

            concrete.Method1();

            Console.ReadKey(true);
        }
    }
}

AbstractBase.cs:

namespace DemoIoC
{
    public abstract class AbstractBase
    {
        public abstract void Method1();
    }
}

ConcreteImplementation1.cs:

namespace DemoIoC
{
    using System;

    public class ConcreteImplementation1 : AbstractBase
    {
        public override void Method1()
        {
            Console.WriteLine("Called ConcreteImplementation1");
        }
    }
}

这篇关于依赖注入-在运行时通过配置文件选择DLL和类实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:57