In the last article,
“Creating the first application on NancyFX”, we took the first steps in studying the NancyFX framework. In this article we will introduce TinyIoC and Nancy Bootstrapper built into Nancy by default.
Directly out of the box with Nancy comes your DI. As mentioned earlier, this is TinyIoC. If for some reason this DI does not suit you, then you can install one of the following DI:
- Ninject
- Autofac
- Structuremap
- Windsor
- Mef
- Nancy bootstrapper for speedy mailer
- Nancy Mono bootstrapper for speedy mailer
In order to download one of the above DIs, you just need to call the Nuget package manager and type nancy.bootstrappers in the search bar.

')
But back to using the package out of the box. In the last article we created a test project. Let's use it and add the IUserStorage.cs interface to it, which will be implemented by our fake repository.
using System.Collections.Generic; namespace NancyFxApplication { public interface IUserStorage { IList<dynamic> GetAllUsers(); } }
Next, we actually create our fake storage, Storage.cs, which will implement the interface we created.
using System.Collections.Generic; namespace NancyFxApplication { public class Storage : IUserStorage { public IList<dynamic> GetAllUsers() { return new List<dynamic> { new {Name = "Homer", Age = 30}, new {Name = "Bart", Age = 12}, new {Name = "Marge", Age = 30}, new {Name = "Lisa", Age = 10} }; } } }
Add a Nancy module with the following code.
using Nancy; namespace NancyFxApplication { public class UsersModule : NancyModule { public UsersModule(IUserStorage storage) : base("/users") { Get["/GetAll"] = param => (Response.AsJson(storage.GetAllUsers())); } } }
After the build, run our application and get the following result:

As we can see, Nancy.Bootstrapper automatically found and substituted into our module the first IUserStorage interface implementation found in the application without any effort on our part. However, some will notice that if the application has another implementation of the IUserStorage interface, this will put us in some awkward situation in which we will not know which interface implementation will cause Nancy (in fact, Nancy will call the first class in alphabetical order from the application that implements the requested interface, but this does not make it any easier for us). If we have two or more implementations of our repository interface in our application, TinyIoC will help us.
Let's add the next class to our project. SouthParkUsers.cs
using System.Collections.Generic; namespace NancyFxApplication { public class SouthParkUsers : IUserStorage { public IList<dynamic> GetAllUsers() { return new List<dynamic> { new {Name = "Eric", SecondName = "Cartman", Age = 9}, new {Name = "Kenny", SecondName = "McCormick", Age = 9}, new {Name = "Kyle", SecondName = "Broflovski", Age = 9}, new {Name = "Stan", SecondName = "Marsh", Age = 9}, new {Name = "Butters", SecondName = "Stotch", Age = 9} }; } } }
Now add the class MyBootstrapper.cs.
using Nancy; namespace NancyFxApplication { public class MyBootstrapper : DefaultNancyBootstrapper { protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) { base.ApplicationStartup(container, pipelines); container.Register<IUserStorage, SouthParkUsers>(); } } }
And now run the application. The result will be as follows:

As you can see, we have changed the class implementing IUserStorage without loading third-party packages, but operating within the framework of NancyFX. As you can see, using NancyFX is fairly easy to use dependency injection. More detailed information and more advanced examples can be found on the
TinyIoC wiki , as well as on the
Nancy Bootstrapper wiki .
In conclusion, I would like to make the announcement of the next article on Nancy. In it, we take a closer look at the Nancy modules. Thanks for attention. I would be glad to review the article and comments.