Generated code - Using the TypedView classes, SelfServicing

Preface

LLBLGen Pro supports read-only lists based on a database view, in the form of TypedViews. A TypedView is generated as a typed DataTable, which is a variant of the typed DataSet concept included in Visual Studio.NET. A typed DataTable is a class which derives from the .NET DataTable and defines properties and a row class to access the individual fields in a typed fashion.

In this section the TypedView classes are briefly discussed and their usage is illustrated using examples.

Instantiating and using a Typed View

A TypedView definition is a 1:1 mapping of a database view on an element in an LLBLGen Pro project. A TypedView contains a field for each database view column. You can limit the number of rows using standard LLBLGen Pro filtering techniques. These techniques use Predicate Expressions and Relation collections (See: Getting started with filtering).

Because the typed DataTable is derived from the .NET DataTable class, it can be used to create DataView objects. With DataView objects you can apply specify additional filtering, sorting, calculations etc. For the examples below, we'll use the view 'Invoices' from the Northwind database, and use the TypedView 'Invoices' mapped onto that view.
Instantiating and filling a Typed View
To create an instance of the 'Invoices' typed view and fill it with all the data in the view, the following code is sufficient:

// [C#]
InvoicesTypedView invoices = new InvoicesTypedView();
invoices.Fill();
' [VB.NET]
Dim invoices As New InvoicesTypedView()
invoices.Fill()

In the above example, the rows will be added as they are received from the database provider; no sorting nor filtering will be applied. Furthermore, all rows in the view are read, which is probably not what you want. Let's filter on the rows, so the TypedView will only contain those rows with an OrderID larger than 11000. We also specify not to filter out duplicate rows. 

// [C#]
InvoicesTypedView invoices = new InvoicesTypedView();
IPredicateExpression invoicesFilter = new PredicateExpression(InvoicesFields.OrderID > 11000));
invoices.Fill(0, null, true, invoicesFilter);
' [VB.NET]
Dim invoices As New InvoicesTypedView()
Dim invoicesFilter New PredicateExpression(InvoicesFields.OrderID > 11000)
invoices.Fill(0, Nothing, True, invoicesFilter)

The overloaded Fill() version that accepts a filter, also accepts other parameters:

Specifying a filter will narrow down the number of rows to the ones matching the filter. The filter can be as complex as you want. See for filtering information and how to set up sorting clauses Getting started with filtering and Sorting.
Instantiating a TypedView mapped onto a Stored Procedure Resultset.

When you map a TypedView onto a resultset of a stored procedure, the fetch action of the TypedView is slightly different. Instead of a Fill() method as described above, the generated TypedView has a different Fill() method which accepts values for the parameters of the stored procedure to call. In the following example, a TypedView is mapped onto the resultset of a stored procedure which returns the Customers for a given country. No transaction is in progress, so null / Nothing is specified.

// [C#]
CustomersOnCountryTypedView customersTv = new CustomersOnCountryTypedView();
customersTv.Fill(null, "USA");
' [VB.NET]
Dim customersTv as New CustomersOnCountryTypedView()
customersTv.Fill(Nothing, "USA")
Instantiating a TypedView mapped onto a Table Valued Function

When you map a TypedView onto a resultset of a table valued function, the fetch action of the TypedView is equal to fetching a TypedView mapped onto a table or view: simply call the Fill method, by specifying the Table Valued Function parameter values when calling Fill().  In the following example, a TypedView is mapped onto the resultset of a table valued function which returns the Orders for a given Customer.

The Table Valued Function call, created through the usage of the TvfCallFactory, requires an alias for the Table Valued Function Call's resultset. Appending filters to the fetch is possible by specifying predicates after the Table Valued Function calls's parameters, when calling Fill(). Be aware to specify 'O' as the source for fields in predicates and sort expressions. An example predicate has been specified in the example below, so the fetch filters on all employees with ID 4. The .Source(string) extension method is available in the SD.LLBLGen.Pro.QuerySpec namespace. If no predicate is required, simply use the overload of the Fill() method which doesn't accept a predicate.

// [C#]
var toFetch = new OrdersForCustomerTypedView();
toFetch.Fill("O", "ALFKI", OrdersForCustomerFields.EmployeeId.Source("O")==4);
' [VB.NET]
Dim toFetch As New OrdersForCustomerTypedView()
toFetch.Fill("O", "ALFKI", OrdersForCustomerFields.EmployeeId.Source("O")=4)
Reading a value from a filled Typed View
After we've filled the TypedView object, we can use the values read. As said, TypedView objects in the form of a typed DataTable are read-only, and therefore handy for filling lists on the screen or website, but not usable for data manipulation. For modifying data you should use the entity classes/collection classes.

Below, we'll read a given value from row 0, the value for the Sales person. We assume the invoices object is filled with data using any of the previously mentioned ways to do so.

// [C#]
string salesPerson = invoices[0].Salesperson;
' [VB.NET]
Dim salesPerson As String = invoices(0).Salesperson

The index 0 points to the first row, and as the row is 'typed', it has named properties for the individual columns in the object; you can read the value using a property.

Null values
Because the TypedView classes are derived classes from DataTable, the underlying DataTable cells still contain System.DBNull.Value values if the field in the database is NULL. You can test for NULL by using the generated methods IsFieldNameNull(). When reading a field which value is System.DBNull.Value in code, like the example above, will result in the default value for the type of the field, as defined in the TypeDefaultValue class. Databinding will result in the usage of a DataView, as that's build into the DataTable, which will then return the System.DBNull.Value values and not the TypeDefaultValue values.
Limiting and sorting a typed view
To sort the data in the typed view, we're not actually sorting the data in the object, but sorting the data before it is read into the object, thus using a sort operator in the actual SQL query. To do that, you specify a set of SortClauses to the Fill() method. Below is illustrated the sorting of the invoices typed view on the field 'ExtendedPrice' in descending order. Sortclauses are easily created using the SortClause factory in the generated code. We pass the same filter as mentioned earlier.

// [C#]
invoices.Clear();	// clear al current data
ISortExpression sorterInvoices = new SortExpression(InvoicesFields.ExtendedPrice | SortOperator.Descending);
invoices.Fill(0, sorterInvoices, true, invoicesFilter);
' [VB.NET]
invoices.Clear()	' clear al current data
Dim sorterInvoices As ISortExpression = New SortExpression( _
	New SortClause(InvoicesFields.ExtendedPrice, SortOperator.Descending))
invoices.Fill(0, sorterInvoices, True, invoicesFilter)

The rows are now sorted on the ExtendedPrice field, in descending order.

LLBLGen Pro Runtime Framework v4.0 documentation. ©2013 Solutions Design bv