document, Nancy is used in lg-sim webUI 2019.07 version, pretty new staff for me.
Introduction
Nancy is a lightweight, low-ceremony framework for building HTTP based services on .NET and Mono. Nancy is designed to handle DELETE
, GET
, HEAD
, OPTIONS
, POST
, PUT
and PATCH
request and provides a simple, elegant Domain Specific Language(DSL) for returning a response.
build to run anywhere
Nancy was designed to not have any dependenceis on existing frameworks, it’s used pretty much wherever you want to. host
in Nancy acts as an adaptor
for a hosting environment, thus enabling Nancy to run on existing techs, such as ASP.NET, WCF.
the bare minimum requires to build a Nancy service are the core framework and a host.
helloworld service
all module should be derived from NancyModule
, and define a route handler. tips: always make the module public
, so NancyFx can discover it.
|
|
exploring modules
module is where you define the logic, is the minimum requirement for any Nancy app. module should inherit from NancyModule
, then define the behaviors, with routes
and actions
.
modules are globally discovered
the global discovery of modules will perform once and the information is then cached, so not expensive.
Define routes
to define a Route
need to specify a Method
+ Pattern
+ Action
+ (optional) Condition
|
|
or async run:
|
|
Method
method is the HTTP method
used to access the resource, Nancy support: DELETE
, GET
, HEAD
, OPTIONS
, POST
, PUT
and PATCH
.
secret for selecting the right route to invoke
in case when two routes capture the same request, remember :
the order in which modules are loaded are non-deterministic
routes in a given module are discovered in the order in which they are defined
if several possible matches found, the most specific match.
root path
all pathes used in Nancy are relative to root path
, which tell Nancy where its resources are stored on the file system, which is defined in IRootPathProvider
static content
static content
is things e.g. javascript files, css, images etc. Nancy uses a convention based approach to figure out what static content it is able to serve at runtime.
Nancy supports the notion of having multiple conventions for static content and each convention is represented by a delegate
with the signature Func<NancyContext, string, Response>
the delegate accepts two parameters: the context of the current request and the application root path, the output of the delegate is a standard Nancy Response
object or null
, which means the convention has no static content.
define your own conventions usign the bootstrapper
View engines
view engine, takes a template
and an optional model
(the data) and outputs(usually) HTML
to be rendered into the browser. in lg-sim, the view
is rendered in nodejs.
MVC
- understand controller
a controller is reponsible for controlling the flow logic in the app. e.g. what reponse to send back to a user when a user makes a browser request.
any public method in a controller is exposed as a controller action.
- understand view
a view contains the HTML markup and content that is send to the browser. in general to return a view for a controller action, need to create a subfolder in the Views
folder with the same name as the controller.
- understand model
the model is anything not inside a controller or a view. e.g. validation logic, database access. the view should only contain logic related to generating the user interface. the controller should only contain the bare minimum of logic required to return the right view.
C# anonymous
|
|
=>
is theLambda
operator, on its left is input paramters(if exist). Lambda expression/sequence is equal to a delegate class.
delegate
is a refer type, used to pass one function as paramter to another function. e.g. in event handle.
Nancy in lg-sim WebUI
a few steps to build Nancy server:
- Nancyhost.start()
- add NancyModule instance(where define route logic)
a few other libs used :
PetaPoco, a light-weight ORM(object relational mapper) framework in C#
SQLite
refer
`