Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NancyFX: How to use bootstrapper to persist an object

Tags:

c#

nancy

I have an XML file I want to access sometimes in post/get's. I dont want to have to load it up every time I hit the post/get route as its application specific. I think I should be loading up an object to store my data once in bootstrapper and referring to this as I need, but can't find any specific examples - how to achieve this?

like image 506
qtime67 Avatar asked Jan 23 '26 11:01

qtime67


1 Answers

You can read the XML file and stick the result in some object that you register in the container during application start up. Your modules can then have that object injected.

That is; something like this in your bootstrapper:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        base.ApplicationStartup(container, pipelines);
        var myXmlCacheInstance = ... // read your xml file and create an object to hold it
        container.Register<MyXmlCahce>(myXmlCacheInstance);
    }
}

and like this in your modules:

public class HomeModule : NancyModule
{
    public HomeModule(MyXmlCache xmlCache)
    {
         Get["/"] => xmlCache;
    }
  }
like image 113
Christian Horsdal Avatar answered Jan 25 '26 00:01

Christian Horsdal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!