Reputation: 53
I have this configuration in my GeneralRegistry:
ForRequestedType<IClientBonusHistoryLoadTask>().AlwaysUnique().TheDefaultIsConcreteType<ClientBonusHistoryLoadTask>();
And I have this code:
public ClientAdvantagesUpdateTask(IBaseRepo<Client> repository, INHUnitOfWorkProvider uowProvider, IClientBonusHistoryLoadTask clientBonusHistoryLoadTask, ClientBonusHistoryLoadTask masterClientBonusHistoryLoadTask) : base(repository, uowProvider)
{
_clientBonusHistoryLoadTask = clientBonusHistoryLoadTask;
_masterClientBonusHistoryLoadTask =
masterClientBonusHistoryLoadTask;
bool y = clientBonusHistoryLoadTask.Equals
(masterClientBonusHistoryLoadTask);
var task = ObjectFactory.GetInstance<IClientBonusHistoryLoadTask>
();
var task2 =
ObjectFactory.GetInstance<IClientBonusHistoryLoadTask>();
bool x = task.Equals(task2);
}
For some reason, y is true (which is the problem) and x is false (which works as expected). Is this a bug, or am I doing something wrong?
Upvotes: 3
Views: 855
Reputation: 73
I have the same problem, always unique is not making it always unique. but there is little documentation to know what it is supposed to do. I use the following:
For<ILeadRepository>().Use<LeadRepository>();
and then inside an object factory method:
LeadRepository instance = ioc.GetInstance<LeadRepository>();
ioc.Inject(typeof(ILeadRepository), instance);
ioc.Inject(typeof(LeadRepository), instance);
where IOC is an IContainer
.
Again, I am not sure Inject is supposed to be used like that, but this always pulls the same
instance of LeadRepository out the OIC container after the inject code was run.
Hope this helps or somebody could post on how it is supposed to be done.
I don't like using static factories or static methods, so I made my own factory and everything inside this factory pulls out unique instances out the IOC container, using the code above.
Assuming you want a unique instance, that is a singleton per factory. and you always want the same instance returned. I found a better way to do this and that is to use subcontainers in the factory:
e.g.
myParentContainer = new Container(x => {
x.AddRegistry<ObjectFactoryModelRegistry>();
x.AddRegistry<ServiceRegistry>();
});
myIoc = myParentContainer.GetNestedContainer();
myIoc.AssertConfigurationIsValid();
then can get same instance by using code below:
myIoc.GetInstance<T>;
Upvotes: 1