I am implementing Ioc and there are few things i want to make sure are right.
- If I use
RegisterInstance, on resolving it will always return the singleton object? - BootStrapper will be loaded in Global.asax or some place where it will be loaded initially, 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
RegisterInstanceif you want PerResolve instancing. Either useRegisterInstancewhich will return always the same instance of the object (that is the point of registering instance) or useRegisterTypeand definePerResolveLifetimeManager.RegisterInstanceby default usesContainerControlledLifetimeManager. The only other meaningfull lifetime manager forRegisterInstanceisExternallyControlledLifetimeManager.TransientLifetimeManagerandPerResolveLifetimeManagerdon’t make sense because these lifetimes must create new instance each time you callResolve.PerThreadLifetimeManageris useless in scenarios where you don’t control threading .