Question

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

Was it helpful?

Solution 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);

OTHER TIPS

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);            
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top