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

Functions

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

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. 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 GetCustomersByCity.

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 GetCustomersByCity_Request{    CityID = 123};// call the function called GetCustomersByCityvar customers = _repo.AppDb.Sales.Functions.GetCustomersByCity(request);foreach (var customer in customers){    //do something with each customer found}

Scalar Functions#

These functions return a single scalar value.

The following C# code sample shows how to consume a SQL Function called CalculateCustomerPrice.

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 CalculateCustomerPrice_Request{    CustomerID = 123,    PricingDate = DateTime.Now,    StockItemID = 345};// call the function called CalculateCustomerPricevar price = _repo.AppDb.Sales.Functions.CalculateCustomerPrice(request);// do something with the price