创建依赖关系的对象 - 依赖注入 [英] Creating objects with dependencies - dependency injection

查看:143
本文介绍了创建依赖关系的对象 - 依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有类:

  public class WithDependencies 
{
public WithDependencies(IAmDependencyOne first ,IAmDependencyTwo second)
// ...
}

现在的问题。在应用程序中如何创建WithDependencies类的对象?
我知道有很多方法。



  new WithDependencies(new DependencyOne(),new DependencyTwo ); 

  new WithDependencies(IoC.Resolve(IDependencyOne)),IoC.Resolve(IDependencyTwo )); 


  //注册IDependencyOne,IDependencyT应用程序启动时的两个实现
IoC.Resolve(WithDependencies);


  //注册IDependencyOne,IDependencyTwo app start 
//隔离我自己与具体IoC容器
MyCustomWithDependenciesFactory.Create();



等等...



你觉得怎么办?



编辑:



由于我没有得到答案,或者我不明白他们,我会再试一次,让我们说一些事情按钮,定时器,任何)我需要新对象WithDependencies()。我如何创建它?假设IoC容器已经被配置。

解决方案

p>这取决于上下文,所以不可能提供一个单一的答案你会从组织根 a>:

  var wd = new WithDependencies(new DependencyOne(),new DependencyTwo()); 

然而,即使没有DI Container,上述代码并不总是明确的回答。在某些情况下,您可能希望在多个消费者中分享相同的依赖关系,如下所示:

  var dep1 = new DependencyOne() ; 
var wd = new WithDependencies(dep1,new DependencyTwo());
var another = AnotherWithDependencies(dep1,new DependencyThree());

在其他情况下,您可能要共享依赖关系,其中例如,第一个选项更正确。



这只是一个关于终身管理的DI的整个维度的一小瞥。许多DI容器可以为您照顾,这是一个很好的选择,更喜欢DI容器比穷人的DI。



一旦你开始使用DI容器,你应该遵循注册解析版本模式解决类型时,让自动布线照顾实际组合:

  var wd = container.Resolve< WithDependencies>(); 

上述示例假定容器已正确配置。


Let's say we have class:

public class WithDependencies
{
  public WithDependencies(IAmDependencyOne first, IAmDependencyTwo second)
  // ...
}

Now the question. How do you create objects of WithDependencies class in an application? I know there are many ways.

new WithDependencies(new DependencyOne(), new DependencyTwo());

new WithDependencies(IoC.Resolve(IDependencyOne), IoC.Resolve(IDependencyTwo());

// register IDependencyOne, IDependencyTwo implementations at app start
IoC.Resolve(WithDependencies);

// register IDependencyOne, IDependencyTwo implementations at app start
// isolate ourselves from concrete IoC Container
MyCustomWithDependenciesFactory.Create();

and so on...

What do you think is the way to do it?

Edit:

Because I don't get answers or I don't understand them I'll try to ask again. Let's say that on some event (button, timer, whatever) I need new object WithDependencies(). How do I create it? Assume IoC container is already configured.

解决方案

It depends on the context, so it's impossible to provide a single answer. Conceptually you'd be doing something like this from the Composition Root:

var wd = new WithDependencies(new DependencyOne(), new DependencyTwo());

However, even in the absence of a DI Container, the above code isn't always unambiguously the correct answer. In some cases, you might want to share the same dependency among several consumers, like this:

var dep1 = new DependencyOne();
var wd = new WithDependencies(dep1, new DependencyTwo());
var another = AnotherWithDependencies(dep1, new DependencyThree());

In other cases, you might not want to share dependencies, in which case the first option is more correct.

This is just a small glimpse of an entire dimension of DI concerned with Lifetime Management. Many DI Containers can take care of that for you, which is one excellent argument to prefer a DI Container over Poor Man's DI.

Once you start using a DI Container, you should follow the Register Resolve Release pattern when resolving types, letting Auto-wiring take care of the actual composition:

var wd = container.Resolve<WithDependencies>();

The above example assumes that the container is already correctly configured.

这篇关于创建依赖关系的对象 - 依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆