IoC - Unity, how RegisterInstance works, right?

I am implementing Ioc, and there are a few things that I want to do correctly.

  • If I use RegisterInstance , will it always return a singleton object when resolved?
  • Will the BootStrapper be loaded in Global.asax or in some place where it will be loaded initially, which means that all instances will be single-point?

But I want to know how to do 1. Create a separate instance for each solution, PerResolve will not work with RegisterInstance, it only works with RegisterType.
2. If I create the dependent object as a static property, will it work the same way, if I can create a separate instance for each permission?

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; } } 
+7
source share
1 answer

You cannot use RegisterInstance if you want to configure PerResolve. Either use RegisterInstance , which will always return the same instance of the object (i.e., the registration point of the instance) or use RegisterType and define PerResolveLifetimeManager .

RegisterInstance uses ContainerControlledLifetimeManager by default. The only other significant lifecycle manager for RegisterInstance is ExternallyControlledLifetimeManager .

TransientLifetimeManager and PerResolveLifetimeManager do not make sense, because these lifetimes should create a new instance every time you call Resolve .

PerThreadLifetimeManager useless in scripts where you do not control streaming.

+12
source

All Articles