0

I am very new to Ninject and my scenario is this: I have two classes ClassA and ClassB with respective interfaces iClassA and iClassB . Classes are given below:

public ClassA()
{
    private ClassB _clsb;
    private String _trustKey;

    public class ClassA(ClassB clsB)
    {
        _clsb = clsB;
    }

    public string getTrustKey()
    {
        this._trustKey = _clsb.trustKey
        return this._trustKey;
    }

}

public class ClassB()
{
    private string _trustKey;

    public ClassB(string trustkey)
    {
        _trustKey = trustKey;
    }

    public string trustKey() 
    {
        get  { return _trustKey; }
        set  { _trustKey = value; }
    }

}

And I have a controller class and its constructor like this

public class controller : ApiController
{
    private iClassA _iclsA;

    public mycontroller(iClassA clsA)
    {
        _iclassA = clsA;
    }

    public string getTrustKey()
    {
        clsA.getTrustKey();
    } 

}

How can I initialize the trustkey value of ClassB at runtime so that when classA is called in the controller constructor, classB gets its trustkey initialized with runtime value. When i used kernel binding like kernel.bind<iclassA>.To<ClassA>() and kernel.bind<iClassB>.To<ClassB>(), I could not assign a value to trustkey of classB.

Thanks in advanced!

Matthew
  • 41
  • 8
  • please fix your code formatting. – BatteryBackupUnit Jun 24 '16 at 15:13
  • How is the value of trustkey defined? Is the value of trustkey static during the life of the application? – BatteryBackupUnit Jun 25 '16 at 00:03
  • either http://stackoverflow.com/questions/13893268/ninject-constructor-parameter or http://stackoverflow.com/questions/6935870/combining-di-with-constructor-parameters apply here. – BatteryBackupUnit Jun 25 '16 at 00:09
  • BatteryBackupUnit, it is dynamic. – Matthew Jun 25 '16 at 00:40
  • what kind of dynamic? can you show some code how you retrieve it and when you retrieve it? A factory might be what you need... – BatteryBackupUnit Jun 25 '16 at 12:42
  • trustkey can be assumed as the userid and once its assigned at runtime ,it remains static throught the application life. And it comes from a diiferent application . – Matthew Jun 27 '16 at 18:17
  • i tried factory pattern with a method called Create that accepts parameters and this method returns classb and it solves the purpose but how can i swap the classB for different environments like dev , production etc becasue on dev i mimic trustkey differently from production meaning for dev i use a different version of classb like devclassb and for production i use different version of classb like ClassB – Matthew Jun 27 '16 at 18:26
  • As i was off this weekend, i could not respond to ur post immediately .Sorry for that. – Matthew Jun 27 '16 at 18:27
  • that's an entirely different problem. Again, it depends on how/when ou know what kind of environment is. If you already know at the time when you create the bindings, an `If { Bind... } else { Bind...}` is probably right for you. – BatteryBackupUnit Jun 28 '16 at 06:19
  • Thank you very much. That actually answers my question. I appreciate all your help! – Matthew Jun 28 '16 at 18:29

0 Answers0