Functions

Table Of Contents

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 application

var repo = new RepositoryContainer();

// create the request and populate the parameters

var request = new GetCustomersByCity_Request
{
    CityID = 123
};

// call the function called GetCustomersByCity

var 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 application

var repo = new RepositoryContainer();

// create the request and populate the parameters

var request = new CalculateCustomerPrice_Request
{
    CustomerID = 123,
    PricingDate = DateTime.Now,
    StockItemID = 345
};

// call the function called CalculateCustomerPrice

var price = _repo.AppDb.Sales.Functions.CalculateCustomerPrice(request);

// do something with the price