开发者

IoC - Unity, how RegisterInstance Works, Am I right?

I am implementing Ioc and there are few things i want to make sure are right.

  1. If I use RegisterInstance, on resolving it will always return the singleton object?
  2. BootStrapper will be loaded in Global.asax or some place where it will be loaded in开发者_如何学Goitially, which means all the instances will be singleton?

But i want to know how to

1. Create a separate instance per resolve, PerResolve wont work with RegisterInstance, it works only with RegisterType.

2. If I make dependent object as static property, it will work the same way, if i am able to create separate instance per resolve?

please help?

public class ClientUser : UserServiceBase, IClientUser
{
    private  IDataServiceManager _dataServiceManager;
    public ClientUser()
    {

    }

    private IDataServiceManager DataServiceMgr
    {
        get
        {
            if (_dataServiceManager == null)
                _dataServiceManager = ProjectContainer.Instance.Resolve<IDataServiceManager>();

            return _dataServiceManager;
        }
    }    


You can't use RegisterInstance if you want PerResolve instancing. Either use RegisterInstance which will return always the same instance of the object (that is the point of registering instance) or use RegisterType and define PerResolveLifetimeManager.

RegisterInstance by default uses ContainerControlledLifetimeManager. The only other meaningfull lifetime manager for RegisterInstance is ExternallyControlledLifetimeManager.

TransientLifetimeManager and PerResolveLifetimeManager don't make sense because these lifetimes must create new instance each time you call Resolve.

PerThreadLifetimeManager is useless in scenarios where you don't control threading .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜