Posts

Applying Single Responsibility Principle when calling a remote Web API

Introduction A requirement of a certain client was as follows Call a third-party web API from an ASP.NET MVC Controller The calling of the web API should work as follows Check if the token sent with the call is valid if the token is not valid, generate a new token and update a database field where token is stored send the token with the call to authorize the request operation This requirement was asked by the customer shortly after completing a course talking about software architecture and design principles. So it was the first practical implementing of the principles learned Implementation The design of the solution tried to stick to the Single Responsibility Principle. So when splitting the requirements above into business classes, it resulted in The responsibility of the controller action method is to call the API only.It has nothing to do with Creating an http client object Check if the authorization parameters are passed The responsibility of the class calling the API only is...

How to validate an input against a changing validation requirements

  Introduction Data annotation is a server side mechanism that is used to validate a certain model attribute value in MVC.It binds a model attribute to a certain validation attribute type .Common validation attribute types include Required Range DataType Regular expression .... Regular expression data annotation is used to validate a model attribute value against a certain pattern,as example below The following data annotation checks if a model attribute value qualifies to be a valid e-mail address [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Email is not valid.")] The model attributes are bound to the proper data annotation while creating the model <> What if the pattern is subject to change as per user requirements for example user can control the complexity degree of user passwords public partial class PasswordComplexityRuleDataModel { public int Id { get; set; } public bool MustContainLette...

Validate that a certain checkbox is checked before submit(ASP.NET MVC)

Image
Introduction In this article we will discuss different approaches of performing validation on a checkbox value to be true in an MVC view.This validation should combine security and be user friendly as well Prepare Scenario 1.Create an MVC application as in the following pictures(name it ApproachForValidatingAcheckbox for example)    2.Add a new class under model folder as follows //////////////////////////////////////////////////////////////////////////////////////////////////////////////////  public class UserAgreementModel     {                   } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 3.Add a controller class under controllers folder as follows 4.Add a view for submitting an instance of the model as follows   place the following code in the view ////////////////////////////////////////////////////////////////////////////////////...

An example of a (LINQ) approach to solve recurring problems

 Introduction Most of the time,we think about LINQ in terms of querying the data model . However ,it can give a set of alternative solutions to problems that we just drill in in a very primitive and basic way.That is because it is used to perform operations on generics of any data type. Problem One task I handled lately was to find the gap in a series of numbers.Let us discuss the first solution that comes into one's mind when he approach the task at the first stage. First Solution To be able to follow the discussion.create  a .NET console application and copy the below code  inside program.cs class file //################ using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; namespace NumberSeriesGapFinder {     class Program     {         static void Main(string[] args)         {             string inputNumbers;       ...