Skip to main content

Posts

Showing posts from September, 2021

A Demo On Request Logging Using MediatR IPipelineBehavior In Asp.Net Core Application

Click here  for step by step implementation of the CQRS MediatR design pattern. In this article, we are going to do request logging into an Asp.Net application Core application build on the CQRS MediatR design pattern. The application request logging helps to track the time taken per request. The MediatR provides a pipeline interface that is 'IPipelineBehavior' that helps to implement our logging logic. IPipelineBehavior: The 'IPipelineBehavior' exactly works like Asp.Net Core middleware, but its starting execution and ending execution happens within the 'IMediator'. So the 'IMediator.send()' is usually used to invoke the 'Query' or 'Command' handlers. So if we implement 'IPipelineBehavior' then begin logic inside of its start executes then invokes 'Query' or 'Command' handlers, later again go through 'IPipelineBehavior' and executes the end logic. Create A .Net5 Web API Sample Application: In this

Model Validation In Asp.Net Core Application Using Fluent Validation Library

In this article, we will explore the usage of the Fluent Validation library in the Asp.Net Core application. Fluent Validation Library: Fluent Validation is a popular .Net library for enabling validation rules. It has a lot of features like in-built validation rules that are most commonly used in any application and also has flexible support for creating custom rules. Implementing Fluent validation rules is a cleaner approach than using the 'DataAnnotation'(.Net default implementation for applying validation rules). It can be automatically injected into .NetCore model validations without any explicit implementation. How To Implement Fluent Validation To A Model: To apply fluent validation to the model, we have to create a separate validator class. The validator class must inherit from 'FluentValidation.AbstractValidator<TModel>'. Inside of the constructor need to apply required validation for the properties of 'TModel'. public class Student { public

AutoMapper In Asp.Net Core Applications

AutoMapper: Automapper is an object-to-object mapper. Object to object mapping works by transforming an input object of one type into an output object of a different type. Manually mapping one object property value to another object property value leads to more lines of code, so all this dirty work can be avoided by using the Automapper. As long as all properties names are the same for 2 different objects that need to map, almost zero configuration is needed to map them.  When To Use AutoMapper?: In any kind of architecture, it is always advised to not expose the 'Entity'(Table classes) directly. So we will create replica classes for 'Entity' we called them as 'DTO'(Data Transfer Object) or 'VM'(View Models). So this is the real-time best case to use Automapper for mapping 'Entity'(Table classes) data to DTO or VM's. Create A Sample Dotnet5 Web API Project: In this demo, we are going to understand the different mapping techniques that are

An Overview On SameSite Cookie Options In Dotnet Core Application

The SameSite cookie option is used by the browsers to determine whether to attach or remove the cookie for a request. So to understand all the options of SameSite cookie, here we are going to check different scenarios with the help of 2 different domains like "http://mycookieapp.com/" and 'http://mythirdparty.com/' . The  "http://mycookieapp.com/" is a dotnet5 MVC application where have login cookie authentication enabled, so here we will check the SameSite option for my login cookie(SameSite option applied to any kind of cookie, here I'm just using the login cookie for my testing). So here I enable the 'Authorization' attribute on my index page, so only authenticated users can access it. The  'http://mythirdparty.com/' is a normal dotnet5 MVC application where we consume the  "http://mycookieapp.com/" website as a link or iframe. AspNet Core Cookie SameSite Options: The following are the cookie SameSite options: Strict Lax Non

Vue 3 Consume GraphQL Endpoint Using Vue Apollo

In this article, we are going to implement Vue 3 sample application that consumes GrpahQL Endpoint using the Vue Apollo library. Create Vue 3 Application: Let's create a sample Vue 3 application to accomplish our demo. Vue CLI Command To Create Project: vue create your_project_name GraphQL Server Endpoint: We should have a GraphQL server endpoint for our demo. So I created a GraphQL server endpoint using 'Dotnet', so I'm going to use it here so its URL be like 'https://localhost:6001/graphql'. So please make sure to have your own GraphQL server endpoint. Install Vue Apollo And Its Dependent Libraries: npm install --save @vue/apollo-option npm install --save @apollo/client npm install --save graphql npm install --save graphql-tag One more plugin we have to install 'react', at the time of this article written we will get an error that says that need to install the 'react', so this 'react' installation might be avoided in feature r