, , ( , ), , . , ? factory factory, Ninject .
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)
. , .
OnActivation((ctx, instance) =>
instance.MyStrategy = ctx.Kernel.Get<MyDependency>(m =>
m.Get("MyConstraint", null) == instance.MyConstraint);