Skip to content

Extensions

Craig edited this page Oct 30, 2017 · 6 revisions

Nettle extensions increase the pool of functions available (sort of like plugins). Unlike the Nettle core, extensions have dependencies to other packages - this is why they have been separated, to keep Nettle free of dependencies (and to keep the code base free of clutter).

There are two steps to installing an extension:

Step 1

Install the NuGet package for the extension required.

Step 2

Before getting a Nettle compiler instance, register the extensions resolver with the NettleEngine.RegisterResolvers static method. This will tell Nettle how to resolve the additional functions.

For example, We can register the default resolver (which is automatically registered if not done manually) with the following code:

NettleEngine.RegisterResolvers
(
    new DefaultNettleResolver()
);

var compiler = NettleEngine.GetCompiler();

The RegisterResolvers takes a params array of INettleResolver, so any number of resolvers can be registered.

Custom Extensions

You can create custom extensions for Nettle. Simply create a new class library project, install the Nettle NuGet package and create one or more custom functions. Then just create a new resolver that implements INettleResolver.

If you create a class that inherits DefaultNettleResolver, this code will automatically register all functions that can be resolved in the same assembly (the functions that can be automatically resolved are those that contain parameterless constructors - if you want to register functions with constructors that take parameters, you will need to register these manually).

Nettle.Data

To install the NuGet package:

Install-Package Nettle.Data

This extension provides various data reading and formatting functions. The aim of this extension was to allow data to be queried from a database or file and rendered. An example requirement might be a custom report or data export (in CSV format). See the functions documentation for a full list of functions this extension provides.

The ExecuteQuery and ExecuteStoredProcedure functions require a database connection to be registered. The example below demonstrates how to register an SQL Server Database connection with the data resolver.

var dataResolver = new NettleDataResolver();

var connectionString = ConfigurationManager.AppSettings
[
    "DatabaseConnectionString"
];

dataResolver.ConnectionRepository.AddConnection
(
    new SqlClientConnection
    (
        "Demo",
        connectionString
    )
);

NettleEngine.RegisterResolvers
(
    dataResolver
);

The code above uses the SqlClientConnection class, which is an SQL Server implementation for the IDbConnection interface (note: this also uses an SQL Server implementation of the IDbAdapter interface, which is needed for the database connection to talk to the database).

Nettle.Web

To install the NuGet package:

Install-Package Nettle.Web

This extension provides various functions for working with the web, such as getting a HTTP resource or posting to a resource. This can be useful if you want to render the results of a Web API request or just insert the contents of a HTTP resource.

Nettle.NCalc

To install the NuGet package:

Install-Package Nettle.NCalc

This extension provides a direct integration with the open source NCalc mathematical expressions evaluator engine.

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.

The extension provides a single function Evaluate, which takes at least one parameter (expression, which is a string). Any additional parameter values will be merged into the expression using the static String.Format method. The function returns an object, which is either a number or Boolean.

Clone this wiki locally