ASP.NET Nano Application Services
ASP.NET application services are built-in Web services that provide access to features such as forms authentication, roles, and profile properties. These services are part of a service-oriented architecture (SOA), in which an application consists of one or more services provided on the server, and one or more clients. Following service architecture keeps code reusable.
In the Nano ASP.NET boilerplate, application services contain the business logic and are found in Application/Services. The Product Service is provided as an example.
The Product Service
The Product Service is a sample CRUD service, found within /Services/ProductService. It consists of a ProductSevice class and interface, IProductService. Support classes are contained in subfolders DTOs, Filters, and Specifications.
You can quickly scaffold new services with the dotnet new nano-service command. Check out the CLI tool guide for using this tool, with parameters and options.
The Product Service contains the following methods:
GetProductsAsync
Method to return a full list. Accepts a keyword string filter
GetProductsPaginatedAsync
Method to return a paginated list. Accepts a ProductTableFilter and returns a PaginatedResponse.
If creating a project with option -ui spa as User Interface, the request and return formats are specific to Tanstack Table v8, which is the table component used in Vue / React projects.
If creating a project with option -ui razor as User Interface, the request and return formats are specific to JQuery Datatables which is the table component used in the Razor project.
GetProductAsync
Method to get a single product by ID. Accepts a Guid, returns Response with a ProductDTO
CreateProductAsync
Method to create a new product. Accepts a CreateProductRequest and returns Response with a Guid of the newly created product
UpdateProductAsync
Method to update a product. Accepts an UpdateProductRequest and returns Response with a Guid of the updated product
DeleteProductAsync
Method to delete a product. Accepts a Guid and returns Response with a Guid of the deleted product.
Service Registration
By inheriting IProductService from ITransientService, the Product Service is dynamically registered as a transient service.
Response Objects
All responses are wrapped with either a Response or a PaginatedResponse class, which can be found in Application/Common. These objects contain a data property for the actual response data in addition to metadata like error messages, success or fail, or pagination data. Using response objects make API responses consistent and easy to manage on the front-end.
Querying Data
Data retrieval and persistence is the responsibility of the generic repository in Infrastracture/Persistence. Application services use the repository via IRepositoryAsync and dependency injection. Low level data operations like mapping entity to DTO classes, pagination, sorting and filtering is contained within the generic repository and doesn’t need to be repeated in services.
The boilerplate uses specification pattern. To query data, build an Ardalis specification class with the filter criteria and pass that to the repository method. The repository will evaluate the specifications and return filtered data. Using specifications allows you to encapsulate and reuse query code.
Paginated Data
The code for pagination will be slightly different depending on if you choose spa or razor for the -ui option. React / Vue projects use Tanstack Table v8. Due to framework limitations, the Razor project uses JQuery Datatables.
For client-side pagination, both table libraries paginate data the same way, using a full list. For server-side pagination however, the tables differ slightly in the way they expect data and return a response.
Column sorting is also particular to each table. Both table libraries allow a user to dynamically sort columns and even select multiple columns for sorting. In Application/Utility/NanoHelpers a method to parse sort order into a generic string will differ specific to each kind of table. An extension method in Ardalis specification can evaluate these generic strings and dynamically build the query with sorting.
Next Steps
Continue on with the following guides: Authentication & Authorization, Multi-Tenancy, and Persistence & Infrastructure.