IoC (Ninject) and Factories

前端 未结 4 1728
长发绾君心
长发绾君心 2020-12-09 03:15

If I have the following code:

public class RobotNavigationService : IRobotNavigationService {
  public RobotNavigationService(IRobotFactory robotFactory) {
          


        
4条回答
  •  醉话见心
    2020-12-09 03:38

    The way you should do:

    kernel.Bind().To("maximilliam");
    kernel.Bind().To("standard");
    kernel.Bind().ToFactory();
    
    public interface IRobotFactory
    {
        IRobot Create(string name);
    }
    

    But this way I think you lose the null name, so when calling IRobotFactory.Create you must ensure the correct name is sent via parameter.

    When using ToFactory() in interface binding, all it does is create a proxy using Castle (or dynamic proxy) that receives an IResolutionRoot and calls the Get().

提交回复
热议问题