I'm using the Ninject Factory Extension and creating a custom instance provider explained in the wiki:
class UseFirstArgumentAsNameInstanceProvider : StandardInstanceProvider
{
protected override string GetName(System.Reflection.MethodInfo methodInfo, object[] arguments)
{
return (string)arguments[0];
}
protected override Parameters.ConstructorArgument[] GetConstructorArguments(System.Reflection.MethodInfo methodInfo, object[] arguments)
{
return base.GetConstructorArguments(methodInfo, arguments).Skip(1).ToArray();
}
}
I've defined the following factory interface:
interface IFooFactory
{
IFoo NewFoo(string template);
}
I've created the following bindings:
kernel.Bind<IFooFactory>().ToFactory(() => new UseFirstArgumentAsNameInstanceProvider());
kernel.Bind<IFoo>().To<FooBar>().Named("Foo");
Now when I call the following I will get an instance of FooBar
:
var foobar = fooFactory.NewFoo("Foo");
This all works great. What I would like however is something a little more like this:
interface IFooTemplateRepository
{
Template GetTemplate(string template);
}
I have a repository that will bring back a template based on the name ("Foo") and I want to pass the template as a constructor argument.
public class FooBar
{
public FooBar(Template template)
{
}
}
Is this possible? I'm not sure what should have the dependency on ITemplateRepository.