Reputation: 477
I have my Factory
which should be called all the time I want an IValidationProgram
public static class Factory{
public static IValidationProgram CreateProgramA(){
var program = new ValidationProgram(
new DependencyA(
new SubDependencyA(),
new SubDependencyB(),
),
new DependencyB()
);
}
}
I wanted to Register like that:
Container.Register(Component.For<IValidationProgram>().Instance(Factory.CreateProgramA()).LifeStyleTransient());
But I do not get an new instance all the time when IValidationProgram
is resolved. I read that I need a typed Factory, but I tried for 2 days without success for my case. All these Dependencies and Subdependencies have an Interface. But I THINK I dont need them for that. If they may help lets name them IDependency
and ISubDependency
.
Upvotes: 1
Views: 43
Reputation: 3070
You don't get a new instance because you supply, during the registration, the instance already created instead of let the container create it(suggested behaviour...). IOW: since you use "Instance" during the registration, the specified lifestyle will be simply ignored.
The usual registration should be:
Component.For<IValidationProgram>().ImplementedBy<yourValidationProgram>.LifeStyleTransient()
if you really need a factory for you component you may used a TypedFactory or the inline registration UsingFactoryMethod
container
.Register(
Component.For<IValidationProgramFactory>().ImplementedBy<YourValidationProgramFactory>(),
Component.For<IValidationProgram>()
.UsingFactoryMethod(kernel => kernel.Resolve<IValidationProgramFactory>().Create())
);
In that case you may use your instance approach but for the factory only!
Component.For<IValidationProgramFactory>().Instance(new Factory())
Upvotes: 2