Skip to main content

Posts

Elegantly dealing with TimeZones in MVC Core / WebApi

In any new application handling TimeZone/DateTime is mostly least priority and generally, if someone is concerned then it would be handled by using DateTime.UtcNow on codes while creating current dates and converting incoming Date to UTC to save on servers.
Basically, the process is followed by saving DateTime to UTC format in a database and keep converting data to native format based on user region or single region in the application's presentation layer.
The above is tedious work and have to be followed religiously. If any developer misses out the manual conversion, then that area of code/view would not work.
With newer frameworks, there are flexible ways to deal/intercept incoming or outgoing calls to simplify conversion of TimeZones.
These are steps/process to achieve it. 1. Central code for storing user's state about TimeZone. Also, central code for conversion logic based on TimeZones. 2. Dependency injection for the above class to be able to use globally. 3. Creating Mo…
Recent posts

Gracefully notifying end user about application error in .Net and .Net Core versions

We generally do error logging for the application to know what error might have occurred in the application. Notifying the end user about the error is equally important. Also, it is always better to implement this at first place rather than doing at a later stage. I have seen many big company's websites ending with .NET yellow page of death. Now, it is really good that the default behavior is removed from .Net Core framework with some generic error message.

We would look how to implement generically and use in older frameworks and some tips on .Net Core as well.

The idea is to create MVC Controller for error and then redirect it from any locations like Filters or Global Exception handling. Also, we would briefly look into the global AJAX error handling system since heavy usage of AJAX in current development trend.

The few steps that we are going to do.
- Represent application errors with enum as system/application have different ways to deal with errors like though Exception or s…

Unit Of Work injection through Asp.Net Core Dependency Injection

This article is not directly related to UnitOfWork but leveraging Asp.Net Core Dependency Injection to consume Unit Of Work.

In one of the previous article about project architecture, I was not very satisfied with the approach for Unit Of Work implementation for initialization of repository even if with some advantage.

Here is old code for UnitOfWork.

public sealed partial class MyProjectUnitOfWork : UnitOfWork<DbContext>, IMyProjectUnitOfWork { public MyProjectUnitOfWork(IContextFactory<DbContext> contextFactory) : base(contextFactory) { } /// <summary> /// BookRepository holder /// </summary> private MyProject.DB.Repository.BookRepository _bookRepository; /// <summary> /// Gets the BookRepository repository. /// </summary> /// <value> /// The BookRepository repository. /// </value> MyProject.Interface.Repository.IBoo…

Visual Studio 2015 Code Analyzer and configuration with StyleCop

What is Diagnostic/Code Analyzer? To put it simply it is something similar to Resharper or Telerik JustCode. It analyzes codes while writing on code editor and suggest for improvements of codes through Squiggles, light icon and based on various severity.
How it works?
I had mentioned that it is similar to Resharper and Telerik JustCode but internal infrastructure is completely different.
It is dependent on new compiler Roslyn. Roslyn has a clever compile technique, unlike older version which makes it faster and flexible for dynamic compilation without the need of the entire code base. The structure is basically divided into two categories Syntax Tree, and Semantic Model. The syntax tree is related to syntaxes only it does not require any reference check. It can be related to DOM elements structure in case of HTML.  Syntax Tree consists of very fine details as simple as WhiteSpace under the code. The Semantic Model is kind of references of Syntax Tree in which it tries to resolve any depen…

Handling JSON DateTime format on Asp.Net Core

This is a very simple trick to handle JSON date format on AspNet Core by global settings. This can be applicable for the older version as well.

In a newer version by default, .Net depends upon Newtonsoft to process any JSON data. Newtonsoft depends upon Newtonsoft.Json.Converters.IsoDateTimeConverter class for processing date which in turns adds timezone for JSON data format.

There is a global setting available for same that can be adjusted according to requirement. So, for example, we want to set default formatting to US format, we just need this code.


services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.DateTimeZoneHandling = "MM/dd/yyyy HH:mm:ss"; });



Data seed through JSON files using EF or any ORM

Last time I had demonstrated a concept for Data Seed through any ORM or MongoDB. The idea is to feed the data to DB at first run or based on condition. The advantage is no one has to manage DB manually with SQL script which feels more natural with Code First approach and every developer can have their own copy with this approach.

I am going to extend the same idea by importing data from JSON file. There are few reasons to do through JSON file as we do not want to keep increasing our codes for data import which may end up with  Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals. Also in the case of a huge number of records it does not make sense and with newer development practice JSON feels more natural for data.

First please go to Data seed for the application with EF, MongoDB or any other ORM as I would be extending same items to allow it through JSON file.

It is always a great idea to have a base class for common items…