With.ConstructorArgument exists for this purpose:-
http://stackoverflow.com/questions/1374098/with-parameters-constructorargument-with-ninject-2-0
See http://stackoverflow.com/questions/2153770/inject-value-into-injected-dependency for more details and examples of how to use the context, providers and arguments to pass stuff like this around more correctly.
EDIT: As Steven has elected to pretend my comment is irrelevant, I'd best make clear what I'm saying with some examples:
MyClass m = kernal.Get<MyClass>( With.Parameters.ConstructorArgument( "i", 2 ) );
which to my eyes is very clear and states exactly what's happening.
If you're in a position where you can determine the parameter in a more global way you can register a provider and do it like this:
class MyClassProvider : SimpleProvider<MyClass>
{
protected override MyClass CreateInstance( IContext context )
{
return new MyClass( context.Kernel.Get<IService>(), CalculateINow() );
}
}
And register it like this:
x => x.Bind<MyClass>().ToProvider( new MyClassProvider() )
NB the CalculateINow()
bit is where you'd put in your logic as in the first answer.
Or make it more complex like this:
class MyClassProviderCustom : SimpleProvider<MyClass>
{
private Func<int> _calculateINow;
public MyClassProviderCustom( Func<int> calculateINow )
{
_calculateINow = calculateINow;
}
protected override MyClass CreateInstance( IContext context )
{
return new MyClass( context.Kernel.Get<IService>(), _calculateINow() );
}
}
Which you'd register like so:
x => x.Bind<MyClass>().ToProvider( new MyClassProviderCustom( ( ) => new Random( ).Next( 9 ) ) )
As stated earlier, if you need to pass a different parameter each time and you have multiple levels in the dependency graph, you might need to do something like this.
A final consideration is that because you havent specified a Using<
Behavior>
, it's going to default to the default as specified / defaulted in the options for the kernel (TransientBehavior in the sample) which might render fact that the factory calculates i
on the fly moot [e.g., if it the object was being cached]
Now, to clarify some other points in the comments that are being FUDed and glossed over. Some important things to consider about using DI, be it Ninject or whatever else is to:
have as much as possible done by constructor injection so you dont need to use container specific attributes and tricks. There's a good blog post on that called yourt showing your container
minimise code going to the container and asking for stuff - otherwise your code is coupled to a) the specific container (which the CSL can minimise) b) the way in which your entire project is laid out. There are good blog posts on that showing that CSL isnt doing what you think it does. This general topic is referred to as Service Location vs Dependency Injection
minimise use of statics and singletons
dont assume there is only one [global] container and that it's OK to just demand it whenever you need it like a nice global variable. The correct use of multiple modules and Bind
..ToProvider
gives you a structure to manage this. That way each separate subsystem can work on it's own and you wont have low leve components being tied to top-level components etc.
If someone wants to fill in the links to the blogs I'm referring to, I'd appreciate that (they're all already linked from other posts on SO though, so all of this is just duplication UI've introduced with the aim of avoiding the confusion of a misleading answer.)
Now, if only Joel could come in and really set me straight on what's nice syntax and/or the right way to do this!