Posts

Consistent Response Validation - Invalid Value for types & Business Rules in Web API

Image
Fluent validation is a great library to perform validation on objects. Integrating fluent validation in .net core is very simple and minimal steps are described along with a way to make validation response consistent be it binding error or business validation error. Steps  1. Add a reference to NuGet package      <PackageReference Include="FluentValidation.AspNetCore" Version="8.4.0" /> Adding sample validator Person Validator 3. Validating Object Validating Object This works fine and the correct error message is displayed. Fluent Validation Error Message response However, consider the request where  Date of Birth  of person is passed as an invalid date. In this case, controller method gets  null  as input because of invalid type value during the model biding phase. By default below error message is thrown if model state is validated in the controller method Default Model State Error In t...

Dependency injection in WCF services

Background In Dependency Injection class dependencies are injected at runtime. For achieving this code is done for abstractions and at the runtime concrete implementation are passed/injected either by constructor/property/method injection. For example: In typical 3 layered architecture Business access layer depends on Data access layer. So instead of referencing concrete Data access layer objects, BAL refers to the abstraction which is DAL contracts and inject dependency for contract at the runtime. This give way to loose coupling and DAL can be replaced at any time without rebuilding BAL as we only need to write new implementation of DAL contracts. WCF In WCF, we write services which are consumed by client. On the server side typically 3 layered architecture is followed where in Service refers to Business layer and business layer refers to Data access layer. As discussed in previous post   this sort of binding is hard binding and has pote...

Inversion of control in 3 layer architecture

Image
While designing layered application we typically follow following structure: In this traditional approach UI layer refers to Business access layer(BAL/Controller layer) and BAL refers to Data access layer (DAL). This referring is usually hard coded at each layer leading to numerous problems simply because of the reason that we don't program to abstraction. The problem which could crop up are: Data access layer is not interchangeable. Suppose in future in stead of storing data to data we choose flat file storage, so in order to accommodate this in current layering BAL and DAL both has to rebuilt. Its not easy to test the whole system without each and every component up and running. In short its not east the fake the data layer or controller layer to test the same using automated tests. Its not easy to share the business rules across application or modules as they are dependent on the lower layers directly. Business rules are generally shared across all the systems despite...
Recently i worked on accessing google apis through Service account. It was real fun to work on open source for the reason : little literature is available for this;learned how to use oAuth2.0 protocol for authentication using JWT(JSon Web Token). Service account in google basically allows application to connect to google apis using the access token received for JWT. So manual intervention of user is eliminated which otherwise is required to authorize request. Service account basically uses following steps: Create JWT(Json web token)  Document for JWT can be found here: https://developers.google.com/accounts/docs/OAuth2ServiceAccount .  Send urlencoded post request to url  https://accounts.google.com/o/oauth2/token to get access token.   This will return a Json response containing access token.   Use this access token to call google apis. Currently only following google apis supports service account: Google Cloud Storage Google Prediction AP...

How Update Panel Works?

Image
Update panel is used quite often for doing partial post back. Here is look on how this happens behind the scenes using Update panel. Update is panel is always used in conjunction with Script Manager. Script manager is the component which basically allows partial rendering of the content, registering all the components required for asynchronous calls. Now coming to Update panel. If we place a control inside an update panel then it will trigger the partial  post back and only the controls which are inside the same update panel or other update panel for which trigger is defined is updated. If we look at the request header that is sent to server using fiddler is (see the highlighted text) With this type of header information Script manager on the server side knows which control has caused the post back and prepares the differential response of all the controls being updated. the possible response looks like: Now at the client side client run time gets this response a...

Configuring traces in WCF service

Some times we fail to find what could have caused our WCF service to fail. This can be easily tracked by powerful tracing feature which can be enabled in config file. Traces can be viewed in Trace file viewer which comes with windows sdk elegantly. For enabling trace use following in WCF service config: < system.diagnostics > < sources > < source name ="System.ServiceModel" switchValue ="Warning, ActivityTracing" propagateActivity ="true" > < listeners > < add type ="System.Diagnostics.DefaultTraceListener" name ="Default" > < filter type ="" /> add > < add name ="ServiceModelTraceListener" > < filter type ="" /> add > < listeners /> source > < sharedListeners > < add initializeData ="c:\wcfLogs\SomeService\app_...

Updating UI from another thread in windows application

While deigning windows application we sometimes require to spawn thread to do a long running operation like IO. During this course if want to update controls on UI thread for indication or messages like success or failure in case of exception. If we directly try to update Control on UI from different thread we get exception. In order to achieve same following code snippet can be used:                Invoke(new MethodInvoker(delegate()                 {                     //Change any control property.                 }));                          ...