I am implementing Ioc and there are few things i want to make sure are right.
RegisterInstance
, on resolving it will always return the singleton object?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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With