Search code examples
c#nancytinyioc

NancyFx and TinyIoC provide single instance to module


Iv got a fairly simlpe question. Im using Nancy with a windows form (passed through the constructor (autoresolve)). If i let nancy resolve automatically every module it creates a new instance of the form, which is not what i want. I thought maybe i could register my form instance in TinyIoC and then it would always use just this instance instead of creating a new one each time. But that has proved not as simple to implement as the idea is.

Thanks in advance


Solution

  • I resolved this by not assigning the window reference to the contructor but by registering it with TinyIoC and the resolving it in the default constructor

    //Registering in form
    var container = TinyIoCContainer.Current;
    container.Register<IMessageDeliverer>(this);
    
    //Resolving in Module Constructor
    var container = TinyIoCContainer.Current;
    IMessageDeliverer mdl = container.Resolve<IMessageDeliverer>();
    setDeliverer(mdl);