Posts

Showing posts from June, 2021

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...