UnityContainer.Resolve或ServiceLocator.GetInstance? [英] UnityContainer.Resolve or ServiceLocator.GetInstance?

查看:562
本文介绍了UnityContainer.Resolve或ServiceLocator.GetInstance?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来愚蠢的问题,因为在我的代码一切工作,但我已经注册了一个单身我统一容器 _ambientContainer 是这样的:

It could seem a stupid question because in my code everything is working, but I've registered a singleton this way with my Unity container _ambientContainer:

 _ambientContainer.RegisterType<Application.StateContext>(new ContainerControlledLifetimeManager());

为了避免使用我的本地外地,我用:

In order to avoid to use my local field, I use:

get {
    return ServiceLocator.Current.GetInstance<Application.StateContext>();
}



我获取属性里面,让我的对象的实例。
这样,我总是得到相同的实例( Application.StateContext 仍是单身)或不的GetInstance 创建一个新的?

inside my get property to get an instance of my object. This way I get always the same instance (Application.StateContext is still a singleton) or does GetInstance create a new one?

是更好地使用本地 _ambientContainer 字段呢?

Is it better to use the local _ambientContainer field instead?

get {
    return _ambientContainer.Resolve<Application.StateContext>();
}

感谢您。

推荐答案

我假设的的ServiceLocator 类型是从的 CommonServiceLocator 项目,您使用统一接口,在这种情况下,的GetInstance 调用 container.Resolve ,所以这两条线是等价的。

I'm assuming that the ServiceLocator type is from the CommonServiceLocator project, and that you're using the Unity adapter, in which case GetInstance invokes container.Resolve, so both lines are equivalent.

您可以查看这里的源 - 的 http://commonservicelocator.codeplex.com/wikipage?title=Unity%20Adapter&referringTitle=Home

You can view the source here - http://commonservicelocator.codeplex.com/wikipage?title=Unity%20Adapter&referringTitle=Home

这篇关于UnityContainer.Resolve或ServiceLocator.GetInstance?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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