The Sharp Factory
Esc
↑ ↓ to moveEnter to open
Data Access / Functions

Functions

Call table-valued and scalar SQL functions from C#.

3 steps · app screens and real code
On this page

What are Functions?#

This feature in The Sharp Factory Framework allows the developer to call SQL Functions.

These functions are bound to a particular SQL Schema.

The Sharp Factory App generates a method per SQL Function added to the model.

The naming convention is RepositoryContainer.{Model Name}.{SQL Schema}.Functions.{Function Name}

The parameters are wrapped in a Request class. The naming convention is {Function Name}_Request

Table Valued Functions#

These functions return a list of entities, or null when no rows match. The Sharp Factory App generates an entity that represents the return type.

The following C# code sample shows how to consume a SQL Function called CustomersInCity, a table function that returns the customers of a city.

C#
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create the request and populate the parametersvar request = new CustomersInCity_Request{    CityID = 1};// call the function called CustomersInCityvar customers = repo.AppDb.Sales.Functions.CustomersInCity(request);if (customers != null){    foreach (var customer in customers)    {        //do something with each customer found    }}

The samples on this page leave out their using lines: the ones they need are in Namespaces.

Scalar Functions#

These functions return a single scalar value.

The following C# code sample shows how to consume a SQL Function called OrderTotalFor, a scalar function that returns the total of an order.

C#
// declare an instance of the Repository Container// remember this can be a singleton if you// declare it early on and reuse it for the// lifetime of your applicationvar repo = new RepositoryContainer();// create the request and populate the parametersvar request = new OrderTotalFor_Request{    OrderID = 1};// call the function called OrderTotalForvar total = repo.AppDb.Sales.Functions.OrderTotalFor(request);// do something with the total

Something wrong or unclear on this page? Tell us on GitHub