Showing posts with label ASP.NET Core. Show all posts
Showing posts with label ASP.NET Core. Show all posts

Fluent Validations in ASP.NET Core

In this article we will implement  Fluent Validations using ASP.NET Core.

Most of you might have used Data Annotations for server-side model validations. We use data annotations to apply validations on public properties of the entity/model class so that when the view is submitted, these model validations are executed. 

Although Data Annotations are good, we have some limitations, e.g. the Data Annotations are bound to the properties so performing conditional validations is difficult. 

To implement conditional validations, we need to write logic in controller and execute it. This is where we need to think of an alternative to the Data Annotations. Fluent Validation is a cool solution we have for performing validations on model classes. 

Fluent Validation is a .NET library for building strongly-typed validation rules. It has support from .NET Framework v4.6.1+ and .NET Core v2.0 onwards. 

ASP.NET Core 3.1 Unit Testing using xUnit and Moq

In any software life-cycle, Testing is an important step for ensuring a good quality software application. Typically, in case of web applications, testing plays an important role. In the software applications, we have the following major testing types:
  1. Unit Testing is the most popular testing methodology. The unit test helps to test every small unit of the source code. Unit testing is used to mock the dependency of the code so that the individual unit of the code is tested separately without including the dependency in the test code. Typically, in a software application, the unit is a class or a method in a class. If the class has any dependency, then to unit test the code, we mock the dependency to attain loose coupling. Unit Testing can be done by a developer and the developer can test the quality of the code.
  2. Integration Testing uses an approach where tests includes individual units and all these units are tested as a group. The advantage of the integration testing is to detect and expose faults during an interaction between these integrated units of the software application. Since, the integration testing uses all units in the test, writing integration test is a costlier approach.
  3. End-to-End Testing is an approach of testing the entire system. In this approach, the testing is implemented by considering every component of the software application right from the UI to the data persistence layer. This guarantees the expected workflow of the software application. The entire application is tested for the critical functionalities such as communication with third party components, databases, networks, etc.
Considering the various testing types, we can see that the cost of the Unit Testing is very low where as End-to-End testing proves costlier, but nevertheless important.

If we consider the number of tests required for software applications, then we can say that we must write several unit tests. How many? As a thumb rule, the overall number of methods written by developers in software applications, should be the number of unit tests you write. Also, if we have conditional statements in these methods, then we have to write one unit test per conditional statement. Cumbersome, but totally worth the cost and time put into it.

This is also the reason why unit tests are the responsibility of the developer or so to say if Test-Driven-Development approach (or user stories and test cases) is understood by the developer, then the code can be bug-free. Figure 1 shows the summary of the testing methodologies.
       


Figure 1: Testing methodologies summary

In this article, we will be implementing Unit Testing of an ASP.NET Core 3.1 application. We will implement the Unit Test MVC Controller, API Controller by mocking their dependencies.

File Upload using ASP.NET Core WEB API 3.1 and React.js Client Application

In this post we will see how to upload files (jpeg/png) to the ASP.NET Core 3.1 WEB API. We all know that WEB APIs are mainly used for Data Communication using JSON format across applications. But what if we want to use WEB APIs for accepting binary files from the client applications? 

In ASP.NET Core WEB API, the Request property of the type HttpRequest object from the ControllerBase class can be used to read the FormData posted by the client application. The following figure explains the approach of the application development


Figure 1: Uploading file from the Client Application to the ASP.NET Core WEB API

As seen in Figure 1, the WEB API project must be configured with Body Request limit to allow how much HTTP request body size will be accepted to process request. This is the FromData posted by the client application. The Static File middleware must be configured to define which folder will be used to store all uploaded files.

Parallel HTTP calls from Angular

Angular being an extensible framework is widely used for developing high responsive front end modern web applications.

These modern web applications consist of several complex workflows on the server side. To provide access of these workflows to client applications, we need REST APIs so that client applications can access these APIs over HTTP.

Client applications developed using  Front-End frameworks like Angular can make use of HttpClient object from @angular/common/http to access these REST APIs and then using Observable object from RxJs Angular, can store and manage data received from the REST APIs in the front-end application.

But what if the Angular application wants to makes call to REST APIs in Parallel? 


Using Entity Framework Core to access Stored Procedures having User Defined Table Types as Parameters

Last week while conducting a .NET Core Full Stack training, my client asked me to explain the mechanism of accessing SQL Server Stored Procedures using Entity Framework (EF) Core. They were creating an ASP.NET Core 2.2 WEB API app.

I have explained the entire process with examples and demonstrations. My attendees even requested me to show an example to access a stored procedure that accepts a User Defined Table (UDT) Type as input parameter.       

Entity Framework Core (EF Core) is an ORM for building the data access layer of modern applications.

EF Core provides Database First and Code First approaches to generate entity classes from database and generate Db Tables from entity classes respectively. The object model of EF Core provides methods to connect to a relational database and perform database transactions.

In most of the cases, we use Entity classes to perform all CRUD operations with the database. In this case, we will write all the logic for database operations in the .NET application by creating various repositories.

Global Error Handler in ASP.NET Core

Error handling is crucial for any application. In Line-of-Business (LOB) ASP.NET applications, error handling can be implemented in multiple ways. Generally, when an application needs to perform data validations on data submitted by end-users, we implement data validations using annotations which is also known as Model validation

Model validation is limited with the Model class (aka entity class), but our application must be capable to validate code while executing - this is known as exception handling.

Traditionally, most of the programming languages provide try…catch…finally block for exception handling. There is nothing wrong in having a try…catch…finally block for every method in each class of the application, but it is always better to implement a global error or exception handler. By having a centralized exception handling, we can make our code more readable and maintainable.

In ASP.NET MVC applications we have exception action filters. Using this action filter, it is easier for the developer to implement global exception handler. I have already posted on MVC Exception Handling at this link

In the current article we will implement error handling in ASP.NET Core applications for WEB API. It is important to to have a global exception handler in WEB API so that the API action methods can remain clean, readable and maintainable.

ASP.NET Core provides a built-In middleware for global error handling, but we can also implement custom middleware or even we can write try...catch block for each action method. We will implement error handling with all these methods.

File Upload in ASP.NET Core

In one of the .NET Core trainings I was conducting recently, one of my students asked about uploading files using .NET Core application. Here's a solution. When we use ASP.NET MVC to upload a file, we use the HttpPostedFileBase class. This class is used to access the files uploaded by the client in an MVC application. In .NET Core, the IFromFile interface is used to represent a file that is sent with the HttpRequest. In this article we will use the IFromFile interface to upload the file. We will use Visual Studio 2017 and .NET Core 2.1. Information about the IFromFile interface can be read from this link.

Step 1: Open Visual Studio 2017 and create a new ASP.NET Core Web Application. Name this application as Core_FileUpload. Select the MVC project with .NET Core and ASP.NET Core 2.1 version for the project as shown in the following image:


mvc-app[8]