This is an add-in for Fody
It extends Nancy with a way to modify models after a route has been executed, but before they are serialized
Nuget package http://nuget.org/packages/Nancy.ModelPostprocess.Fody
To Install from the Nuget Package Manager Console
PM> Install-Package Nancy.ModelPostprocess.Fody
Nancy is a very flexible framework, which offers a variety of extension points. One such extension point is the AfterRequest pipeline
pipelines.AfterRequest += (ctx) => {
// Modify ctx.Response
};
The problem is that ctx.Response holds serialized value (JSON. HTML, etc) of a response ready to be sent back to the client. Currently though there is no way to modify a model outside of a NancyModule. There is a question on StackOverflow about this here
Nancy.ModelPostprocess.Fody is a Fody add-in, which means that extra code is injected to the modules so that the models returned from routes can be modified before thay are serialized.
You implement your modules as usual
public class SampleModule : NancyModule
{
public SampleModule()
{
Get["Model"] = p => new SampleModel { SomeValue = "Original value" };
}
}
public class SampleModule : NancyModule
{
public SampleModule(IModelPostprocessor processor)
{
var route = new Func<dynamic, dynamic>(
p => new SampleModel { SomeValue = "Original value" }
);
Get["Model"] = route.WrapRoute(processor, this);
}
}
In Bootstrapper you register the IModelPostprocessor and handlers for specific model types
public class SampleBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(
TinyIoCContainer container,
IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
var postprocessor = new ModelPostprocessor();
postprocessor.RegisterModelHandler(new SampleModelHandler());
container.Register<IModelPostprocessor>(postprocessor);
}
}
Model handlers are classes implementing IModelHandler<T> interface, which has a single method
public class SampleModelHandler : IModelHandler<SampleModel>
{
public void Postprocess(SampleModel model, NancyModule module)
{
model.SomeValue = "I can modify my model here :)";
}
}
The default implementation of IModelPostprocessor
- holds instances of model handlers and passes matching models for processing,
- has a built-in handler for the
Negotiatorclass, which handles model(s) returned for Content Negotiation, - currently requires that handlers are registered manually and as concrete instances
Russian Doll designed by [Simon Child](http://www.thenounproject.com/Simon Child) from the Noun Project