Skip to main content

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…
Recent posts

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…

Data seed for the application with EF, MongoDB or any other ORM.

Most of ORMs has moved to Code first approach where everything is derived/initialized from codes rather than DB side. In this situation, it is better to set data through codes only. We would be looking through simple technique where we would be Seeding data through Codes.

I would be using UnitOfWork and Repository pattern for implementing Data Seeding technique. This can be applied to any data source MongoDB, EF, or any other ORM or DB.

Things we would be doing.
- Creating a base class for easy usage.
- Interface for Seed function for any future enhancements.
- Individual seed classes.
- Configuration to call all seeds.
- AspNet core configuration to Seed data through Seed configuration.
Creating a base class for easy usage public abstract class BaseSeed<TModel> where TModel : class { protected readonly IMyProjectUnitOfWork MyProjectUnitOfWork; public BaseSeed(IMyProjectUnitOfWork MyProjectUnitOfWork) { MyProjectUnitOfWork = …

ASP.NET Core environment aware strongly typed configuration in MongoDB

This is targetted to save in MongoDB but the technique can be used in any targetted backend with key value setting based on IConfigurationProvider Data property.

Basically, the provided interface works based Data property having a type as IDictionary<string, string>. So, whatever class we create it has to be serialized based on same. We would simplify this by creating an abstract class to have certain serialization logic.

This is sample setting for the application. If you check the structure,  Setting is the main class and nested items are Azure, Message, Email, Twilio. The key value needs to be composed based on parent and child classes separated by colons.



Just like above, we need to populate settings from our different setting classes.

Implementation can be grouped in these sections.
- Application level configuration classes.
- Registration of configuration on ASP.NET Core.
- Environment level configuration, this would little mixed with above.
Application level configuration…