TinyIoc - how do I configure all interface registrations AsPerRequestSingleton?

1.1k views Asked by At
    /// <summary>
    /// Starts up the Application.
    /// </summary>
    /// <param name="container">The container.</param>
    /// <param name="pipelines">The pipelines.</param>
    protected override void ApplicationStartup(TinyIoC.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);

        container.Register<UserProvider>().AsPerRequestSingleton();
    }

The above is my existing application startup containing my TinyIoc configuration. Is there any way to take all interface registrations and turn them all into PerRequestSingletons? I can't do them 'one by one' as I have too many repositories, and I can't leave them as singletons, because it doesn't work with the way that NHibernate is set up.

1

There are 1 answers

2
Chris Sainty On BEST ANSWER

If you want everything to be request scoped, then try moving the registration into the ConfigureRequestContainer method rather than ApplicationStartup.

Generally this is how you handle request scoped singletons in Nancy.

Failing that you might need to look at another DI package, eg. http://nuget.org/packages/Nancy.Bootstrappers.Autofac
It has more features and conventions to deal with mass registrations.

Just nuget it, then make your bootstrapper a subclass of the bootstrapper inside the package.