0

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

peshkatari
  • 151
  • 3
  • 15

2 Answers2

3

you should probably do this in the bootstrapper

something like:

public class MyBootstrapper: DefaultNancyBootstrapper
{
    ConfigureApplicationContainer (TinyIoCContainer container)
    {
        //the .AsSingleton() instructs TinyIOC to make only one of those.
        container.Register<IMessageDeliverer>().AsSingleton();
        base.ConfigureApplicationContainer (container);            
    }
}
albertjan
  • 7,739
  • 6
  • 44
  • 74
2

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);
peshkatari
  • 151
  • 3
  • 15