Unity IOC建立与解决? [英] Unity IOC Buildup vs Resolve?

查看:251
本文介绍了Unity IOC建立与解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候使用buildup,什么时候使用resolve,何时使用Unity IOC。

I was wondering when do I use buildup and when do I use resolve, when using the Unity IOC.

什么时候调用拆解?

谢谢

推荐答案

Resolve用于希望Unity容器构造实例的情况(仅在需要时使用一个新对象,或者是一个预先存在的单例对象),注入其依赖项,然后将对对象的引用交给您。

Resolve is used when you want the Unity container to construct the instance (a new one just when you need it or an pre-existing singleton), inject its dependencies and hand you the reference to the object.

BuildUp在您已经使用时使用拥有对象的实例,并希望容器仅解析并注入其依赖项。

BuildUp is used when you already have an instance of the object and want the container to just resolve and inject its dependencies.

Teardown与BuildUp相反。您可以将对象传递到容器的Teardown方法中,以关闭/清理/任意操作。现有的容器行为在拆解时不执行任何操作,但是可以编写扩展来利用此功能。您还可以使您的对象实现IDisposable,并且容器将在对象自身处置后调用Dispose()。

Teardown is the opposite of BuildUp. You can pass your object to the Teardown method of the container to shut down / clean up / whatever you want. The existing container behavior does nothing at Teardown time, but extensions can be written to take advantage of this. You can also make your objects implement IDisposable, and the container will call Dispose() on your object when it is disposed itself.

IMyService service = container.Resolve<MyService>(); // New service or pre-existing singleton

IMyService service = GetMyService(); // Get the instance from some source
container.BuildUp(service); // Inject dependencies
container.Teardown(service); // Clean-up

这篇关于Unity IOC建立与解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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