问题描述
我有4个项目:
核心(iServer中):
- 系统
- System.Core程序
DependencyResolver:
- 核心
- StructureMap
基础设施(服务):
- 核心
- 对外依存度
控制台:
- 核心
- DependencyResolver
Requierements:
我想只有在DependencyResolver使用StructureMap。
此外,控制台应用程序不应该知道的基础建设任何事情。
I am trying to use StructureMap only in the DependencyResolver.Furthermore the Console application should not know anything about Infrastucture.
当我不想引用StructureMap在我的控制台应用程序我必须建立一个服务定位。
When I do not want to reference StructureMap on my Console Application I have to build a ServiceLocator.
在DependencyResolver我有一个引导程序,它负责调用StructureMap注册表的东西(注册)
In the DependencyResolver I have a Bootstrapper that is responsible for calling StructureMap registry stuff (Register)
在我的控制台应用程序,我想获得一个实例。为此,我需要引用StructureMap。另一种方式是写StructureMaps解决方法,围绕一个小包装。
In my Console application I want to get an instance. For this I need to reference StructureMap. Another way would be to write a little wrapper around StructureMaps resolving methods.
是否有从StructureMap?
Is there any other better way of decoupling the console from StructureMap?
推荐答案
虽然我明白了一个道理分离IOC寄存器,解决,发布从应用程序的执行情况,我看不出有任何理由IoC容器不应该在控制台应用程序(成分根)和应用程序。FPGA实现另一个程序,而不是
While I see a reason for separating IoC register,resolve,release from the implementation of the application, I don't see any reason why the IoC container shouldn't be in the console application (the composition root) and the application implemention in another assembly instead.
这样的控制台应用程序是很容易的:
That way the console application is very easy:
- 创建容器
- 装载容器配置
- 解析的应用程序
- 呼叫的应用程序运行并通过沿
- 控制台参数配置储藏当应用程序退出的run方法
- Create the container
- Load the container configuration
- Resolve the Application
- Call run on the application and pass the console arguments along
- dispose the container when the application exits the run method
随着SM看起来大约是这样的:
With SM it look about like this:
public void Main(params string[] args)
{
using (var container = new Container())
{
container.LoadAllConfigurationModules();
container.AddRegistry<SomeRegistry>();
container.GetInstance<Application>().Run(args);
}
}
有关的东西,你不能在启动时创建创建在你的应用程序组装工厂接口:
For things you can't create at startup you create a factory interface in your application assembly:
interface ISomeFactory { ISomeDependency CreateSomeDependency() }
和通过注入容器实现在控制台应用这个接口,并用它来解决该实例。我猜SM实现看起来是这样的:
and implement this interface in the console application by injecting the container and use it to resolve the instance. I guess the SM implementation looks like this:
public class SomeFactory : ISomeFactory
{
public SomeFactory(IContainer sontainer) { this.container = container; }
ISomeDependency CreateSomeDependency() { this.container.GetInstance<ISomeDependency>(); }
}
其他IoC容器甚至能够自动实现这些接口工厂后的功能。
Other IoC container even have the functionallity to implement these interface factories automatically.
这篇关于依赖注入和项目结构控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!