Nancy Configuration of IoC for Module

Nancy module constructor is by default without parameters. However, soon you will need to have your route match to some of your logic which should be under some business logic classes. In fact, the next step after routing should be to call your controllers or or service layer classes. To be able to inject your service layer class, you need to inject this class. Nancy Bootstrap class come to the rescue. You only need to override a single method to be in business. Nancy comes with a default small IoC. The method to override is named ConfigureRequestContainer.

public class YouBootstrapper : DefaultNancyBootstrapper
    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
        base.ConfigureRequestContainer(container, context);
        container.Register<IServiceLayer, YourServiceLayer>();

After this modification, you only need to remove the parameterless constructor and have the one with the interface defined in the request container of the BootStrap class.

public class IndexModule : NancyModule
    public IndexModule(IServiceLayer serviceLayer,

Concerning the boot strap class, Nancy will load automatically your class by scanning the assembly where you startup class reside.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.