4. Query Classes
In this tutorial, you will create a query service and learn about query service conventions.
Create a Query Service
Now you will create a query service to see all the tasks completed. Add below code to
Tasks class in
By method comes from
Query<T> base class, and it simply accepts a lambda expression with one parameter of type
T. For this example
By method will accept a parameter of type
Like every public method, this query method is also exposed as a business service. Build and run
AppHost.Service to see your completed tasks.
When a query method returns a list of objects, the convention is to name the method as
By. When a query method takes only one parameter, in order to prevent confusion it is recommended to include name of the only parameter in the method name. This is why this method is named as
ByCompleted. For more information see: Queries
Now add following test case to your
Note that there is a
Context.Query<T>() shortcut to access query classes. Unlike
Context.Get<T>() this shortcut is available in module projects as well, so that you don't have to inject query classes.
Query classes helps you to organize your query methods for the same table into one class, so that you can create reusable query methods.
Now try to create other query methods that accepts more than one parameter, re-run and see the results.
Once you are ready, you can proceed to next page to learn how to create service packages and expose your business services publicly.
You can download complete source code for this tutorial here