Can't get value of Ninject ConstructorArgument (passed in as parameter to kernel.Get)

前端 未结 2 1709
广开言路
广开言路 2021-01-26 10:50

Having trouble getting the value of a ConstructorArgument parameter passed to kernel.Get(). I want to use the parameter\'s value to determine which of two string values will

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 11:07

    Edit: After your comments and edits, I've reformulated the answer. The suggestion below is just one way to go about this. I'm implementing a Provider that will inspect the parameter sent to the request when using kernel.Get(). This parameter has nothing to do with constructor, it is only used as a "context variable" so you can decide what to do with it.

    Here is the code:

    public class ObjectContextFactory
    {
        private readonly string _idNum;
        private readonly IResolutionRoot _container;
    
        public ObjectContextFactory(IResolutionRoot container, string idNum)
        {
            _container = container;
            _idNum = idNum;
        }
    
        public ObjectContext CreateObjectContext()
        {
            //Use _idNum here as you wish, here is a sample implementation
            ConstructorArgument constructorArgument;
            if (_idNum.Substring(0,1) == "2")
            {
                constructorArgument = new ConstructorArgument("connectionString", "your conn string 1");
            }
            else
            {
                constructorArgument = new ConstructorArgument("connectionString", "your conn string 2");
            }
    
            return _container.Get(constructorArgument);
        }
    }
    
    public class ObjectContextFactoryProvider : Provider
    {
        protected override ObjectContextFactory CreateInstance(IContext context)
        {
            var idNum = context.Parameters.FirstOrDefault(p => p.Name == "idNum");
            var idNumValue = idNum.GetValue(context, null) as string;
    
            var factory = new ObjectContextFactory(context.Kernel, idNumValue);
            return factory;
        }
    }
    

    And the bindings:

    public override void Load()
    {
        Bind().To();
        Bind().ToProvider();
    }
    

    When retrieving the factory:

    var factory = kernel.Get(new Parameter("idNum", "1", true));
    

    Another way to achieve this would be via Named Bindings. Just do a kind of translator from your idNum to a string (or use it directly) and call it when getting an instance.

提交回复
热议问题