A conditional binding based on a property value isn't a good design and isn't even possible (at least for constructor injection) as dependencies are normally created before the object receiving them. What if the property is changed later? The preferable way is to inject a factory or factory method that requests the instance from Ninject and exchange the strategy on initialization and property value change internally.
public enum EntityType { A,B }
public class MyControl : UserControl
{
[Inject]
public Func<EntityType, IMyEntityDisplayStrategy> DisplayStrategyFactory
{
get { return this.factory; }
set { this.factory = value; this.UpdateEntityDisplayStrategy(); }
}
public EntityType Type
{
get { return this.type; }
set { this.type = value; this.UpdateEntityDisplayStrategy(); };
}
private UpdateEntityDisplayStrategy()
{
if (this.DisplayStrategyFactory != null)
this.entityDisplayStrategy = this.DisplayStrategyFactory(this.type);
}
}
Bind<Func<EntityType, IMyEntityDisplayStrategy>>
.ToMethod(ctx => type =>
type == ctx.kernel.Get<IMyEntityDisplayStrategy>( m =>
m.Get("EntityType", EntityType.A));
Bind<IMyEntityDisplayStrategy>.To<AEntityDisplayStrategy>()
.WithMetadata("EntityType", EntityType.A)
Bind<IMyEntityDisplayStrategy>.To<BEntityDisplayStrategy>()
.WithMetadata("EntityType", EntityType.B)
Alternatively add an activation action and inject the dependency manually. But be aware that changing the constraint property will lead to an inconsistent state.
OnActivation((ctx, instance) =>
instance.MyStrategy = ctx.Kernel.Get<MyDependency>(m =>
m.Get("MyConstraint", null) == instance.MyConstraint);