Wednesday, March 23, 2016

JavaScript DateDiff

Earlier I blogged about creating a DateDiff function in JavaScript that returns calendar difference between two dates. It returns the Year, Months and Days difference between two dates. Here is the link for my previous blog


I enhanced that DateDiff function to return additional details such as Hours, Minutes and Seconds. Here is the updated code

function dateDiff(startdate, enddate) {
    //define moments for the startdate and enddate
    var startdateMoment = moment(startdate);
    var enddateMoment = moment(enddate);

    if (startdateMoment.isValid() === true && enddateMoment.isValid() === true) {
        //getting the difference in years
        var years = enddateMoment.diff(startdateMoment, 'years');

        //to calculate the months, first get the previous year and then subtract it
        startdateMoment.add(years, 'years')
        var months = enddateMoment.diff(startdateMoment, 'months')

        //to calculate the days, first get the previous month and then subtract it
        startdateMoment.add(months, 'months');
        var days = enddateMoment.diff(startdateMoment, 'days')

        //Similar to days go the previous day
        startdateMoment.add(days, 'days')
        var hours = enddateMoment.diff(startdateMoment, 'hours')

        //Getting minutes
        startdateMoment.add(hours, 'hours')
        var minutes = enddateMoment.diff(startdateMoment, 'minutes')

        //Similar to days go the previous day
        startdateMoment.add(minutes, 'minutes')
        var seconds = enddateMoment.diff(startdateMoment, 'seconds')

        return {
            years: years,
            months: months,
            days: days,
            hours: hours,
            minutes: minutes,
            seconds: seconds
        };

    }
    else {
        return undefined;
    }
}

Plunker for this DateDiff function

Friday, March 11, 2016

Microservices and Database Schemas

As you know Microservices is getting popularity, but most applications may not warrant Microservices architecture. As we don’t know how our applications scale in future, I recommend following the Microservices pattern for these applications also. When the application grows and becomes a candidate for Microservice, then we are already halfway through.


In my opinion Database partition (one database for each service) is one of the toughest parts. In order to do future proof, I recommend partitioning database utilizing the database schemas. As you know database schema is a logical separation of database objects. Using this schema approach we can mimic focused micro databases without the actual overhead of multiple database, complexity and cost.

Thursday, March 3, 2016

Configuring and Resolving Basic Dependency Injection in ASP.NET Core

As you know ASP.NET Core supports Dependency Injection out of box. In this blog, I will show the basic approach to configure and resolve the objects using dependency injection.

The dependency Injection is configured in the Startup along with other services. Here is the code to register our classes for Dependency Injection.

public void ConfigureServices(IServiceCollection services)
{
    //rest of configurations
    services.AddTransient<QuarryDomain>();
    services.AddTransient<QuarryRepository>();
}

In the above code using Transient option, I am creating the object every time I need the object. This option is for lightweight and stateless environments. If you have complicated requirements then you can use the other options such as Scoped, Singleton and Instance.

Once configured the objects can be resolved or passed using constructor injection, as shown below

public class QuarryController : Controller
{
    private QuarryDomain domain;
    public QuarryController(QuarryDomain domain)
    {
        this.domain = domain;
    }
   
    //rest of code
}

In some situations, we may need to manually resolve the objects, for that the framework provides an option to create object as shown below:

AccountDomain accountDomain = HttpContext.RequestServices.GetRequiredService<AccountDomain>();

The above is a basic usage of dependency injection, for more details, please take a look into the asp.net documentation