How to build your own portfolio website in .NET core and React

In this tutorial, we will build a personal portfolio website that will show case all of your professional skills. In this example, my website will contain software development skills, projects, previous jobs, etc. We will building a Single Page Application (SPA) in React.js and .NET Core. I recommend having some sort of programming knowledge, but it’s not required since all code will be provided. We will build each page and component step by step, then we will deploy to Microsoft Azure. I will keep this tutorial pretty straight forward so you can get this up and running. If you have any questions or feedback, please leave a comment

Click here to see what you will be building!

Click here if you would like to fork the project

Project Set up

  1. Download Visual Studio Community edition here.
  1. When you get to the Visual Studio Installer, Check ASP.NET Core Web Application and click Next. This could take awhile to install.
  1. Open Visual Studio and click Create a New Project. Select ASP.NET Core Web Application. Choose a Project name, and click Create.
  1. Choose the React.js template and click Create. 
  1. Once your project is created, select the run button at the top. This will take a few minutes since it has build all of the npm depedencies.
  1. Once it builds, you should get the following page in your browser! Let’s go over each folder to get an understanding.

Folder Structure Rundown

As mentioned before, this project will use React.js and .NET Core (C#). For those who don’t know, React is a JavaScript library created by Facebook and is used for building powerful UI components. React uses a programming language called JSX which stands for JavaScript XML. JSX allows you to write HTML in React. This will make more sense as we work on each page.
Let’s take a look at the folder structure and make some sense of it.

As you can see, the public directory contains index.html and manifest.json. You may have another file in there, you can just delete it if you want. Let’s do one thing, move index.js to your public directory.

index.html – Contains the root node HTML element. This is essentially a container of the entire application. ()

index.js – Renders the React Components BrowserRouter and App inside the HTML element “root”.

The src folder contains all of the JavaScript files that we will use. These JavaScript files are individual components of our application. You will get a better understanding later once we create our own.

The Controllers folder contains our Web API Controllers that handles HTTP requests to give data back to the front end of the application. We typically would retrieve data from a Database but in this case we will just retrieve from a JSON file. That way if you want to add more data such as Education or Jobs, you just update the JSON file.

The Pages folder contains cshtml pages, we will not be creating or touching these files.

The Program.cs and Startup.cs are required to run the application.

Now that we had an high level overview of the application, let’s move on to developing our personal resume! We will start with the About component.

Advertisement

Deploy .NET Core application 3.1 to Microsoft Azure for free

Microsoft Azure is the easiest way to deploy .NET Core applications and shouldn’t require any cost if you stay under the free credits. You will need to create a Microsoft Azure account and provide a credit card, but you will be using the free plan. If you already have an account and have used 12 month free credits, we will use pay as you go pricing, but will select the free tier server. This is good for applications that won’t see a ton of load, ex: your personal portfolio page. Let’s get started!

  1. Once you create an account, go to Microsoft Portal quickstart.

2. Select the first option, Create a Web App.

3. Select the first option, Build and host a web app with Azure Web Apps.

4. Choose Free Trial for subscription. Select Create new for Resource Group and name it whatever you want.

NOTE: If your Free Trial ended, choose pay as you go subscription, we are still going to choose the free tier.

5. Under Instance Details, enter a name, this will be in your site url. Select your Runtime stack, if you recently downloaded .NET Core, you most likely have 3.1, but it’s good to double check.

6. Under App Service Plan, Enter a name and if your sku and size is not Free F1, Select Change Size. Under Dev/Test, select the F1 plan.

7. Select Review and Create and create your instance.

8. Go to your project in Visual Studio, right click your project, select Publish.

9. Select New, and under App Service, Choose Select Existing. Then select your project.

10. Select Publish! Your app will now deploy to your site url. Leave any comments below.

Prototype Design Pattern

The Prototype pattern is a Creational Design Pattern that avoids costly creation of objects. The concept of this is to copy an existing object versus creating a new object all together. This is typically done using the clone() method from the Clonable interface. This design pattern is useful when creating objects becomes too costly or they take too much time.

Although you will be creating copies of an object, each instance will be unique. This is what’s called a Deep Copy. A deep copy will return a brand new object to a new instance, rather than a reference to the original object (Shallow Copy).  Shallow Copies are dangerous because you could change the parameters in one object and it would reflect in both the copied and original object. The construction of the objects will be handle by a Registry class.

We will implement a Device example of the Prototype pattern in Java. We will create an abstract class called Device that implements Clonable from the Java API. The DeviceRegistry  class will contain a static method that returns a cloned instance of Device.



1. Create an abstract class called Device. This will contain getters and setters for id and name. This will contain abstract methods powerOn() and addDevice() that will be overridden by sub-classes.

public abstract class Device implements Cloneable {
    private int id;
    private String deviceName;

    abstract void powerOn();
    abstract void addDevice();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}


2. Add sub-classes Iphone and Pixel.

public abstract class Device implements Cloneable {
    private int id;
    private String deviceName;

    abstract void powerOn();
    abstract void addDevice();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
public class Pixel extends Device {
    @Override
    void powerOn() {
        System.out.println("Powered On Pixel");
    }

    @Override
    void addDevice() {
        System.out.println("Added Pixel Device");
    }
}


3. Create the Registry class that will contain a getDevice() method that will clone the Device object into a new instance.

package Prototype;

import java.util.HashMap;
import java.util.Map;

public class DeviceRegistry {

    private static Map<Integer, Device> devices = new HashMap();

    /**
     * Clones a Device object into a new instance by deviceId.
     *
     * @param deviceId - Id of device we want to clone.
     * @return
     */
    public static Device getDevice(Integer deviceId) {
        Device device = null;
        try {
            device = (Device) (devices.get(deviceId)).clone();
        } catch (CloneNotSupportedException ex) {

        }
        return device;
    }

    /**
     * Loads devices into objects. In a real application we would pull data from a database.
     */
    public static void loadDevices() {
        Iphone iphone = new Iphone();
        iphone.setId(1);
        iphone.setDeviceName("Iphone 8 Plus");
        devices.put(iphone.getId(), iphone);

        Pixel pixel = new Pixel();
        pixel.setId(2);
        pixel.setDeviceName("Google Pixel 4");
        devices.put(pixel.getId(), pixel);
    }
}

4. Create the client class. This will clone a Device instance into Iphone and Pixel.

public class ProtoypeClient {
    public static void main(String[] args) {
        DeviceRegistry.loadDevices();

        Device iphone = DeviceRegistry.getDevice(1);
        System.out.println(iphone);
        System.out.println(iphone.getDeviceName());
        iphone.addDevice();
        iphone.powerOn();
        System.out.println();

        Device pixel = DeviceRegistry.getDevice(2);
        System.out.println(pixel);
        System.out.println(pixel.getDeviceName());
        pixel.addDevice();
        pixel.powerOn();

    }
}
Prototype.Iphone@1b6d3586
Iphone 8 Plus
Added Iphone
Powered On Iphone

Prototype.Pixel@4554617c
Google Pixel 4
Added Pixel Device
Powered On Pixel

Process finished with exit code 0

Abstract Factory Design Pattern In Java

The Abstract Factory design pattern is a creational design pattern that contains a factory class which creates other factories. It is typically called the Factory of factories. There is an abstract Factory class that other factories inherit in order to implement their own factory method. 

A real world example of this would be a Database or Cache implementation. Let’s say you need to make database updates but need to update a SQL Server, MongoDB, and an Oracle database. These would use similar operations but would require a different implantation in order to connect, update, add and delete data from the database. Abstract Factory would come in handy here because you could have separate factories for a relational and non-relational database.

We are going to implement an example of the Abstract Factory Pattern with a Database implementation in Java. We will create an abstract class DatabaseFactory and 2 concrete classes RelationalDBFactory and NonRelationalDBFactory. The DatabaseFactory class will have a static method getFactory()that returns DatabaseFactory. The concrete classes will return a Database object depending on the type. 


1.  Create the abstract class DatabaseFactory. This contains a static method getFactory(String factory) that returns the correct factory object based on the parameter. getDatabase(String database) is an abstract method that the sub-classes will override.

public abstract class DatabaseFactory {

    /**
     * Returns the correct Factory object based on the type.
     * 
     * @param factory
     * @return
     */
    public static DatabaseFactory getFactory(String factory) {
        switch (factory) {
            case "Relational":
                return new RelationalDBFactory();
            case "NonRelational":
                return new NonRelationalDBFactory();
            default:
                return null;
        }
    }

    public abstract Database getDatabase(String database);
}

2. Create the sub-classes RelationalDBFactory and NonRelationalDBFactory. They contain their own version of getDatabase()  that will return a database object such as Mongodb, NoSQL, SQLServer, and MySQL.

public class NonRelationalDBFactory extends DatabaseFactory {

    /**
     * Returns a Database object of either Mongodb or NoSQL which are Non-relational databases.
     * @param database
     * @return
     */
    @Override
    public Database getDatabase(String database) {
        switch (database){
            case "Mongodb":
                return new MongoDB();
            case "NoSQL":
                return new NoSQL();
            default:
                return null;
        }
    }
}
public class RelationalDBFactory extends DatabaseFactory {

    /**
     *  Returns a Database object of either SQLServer or MySQL which are Relational databases.
     *
     * @param database
     * @return
     */
    @Override
    public Database getDatabase(String database) {
        switch (database){
            case "SQLServer":
                return new SQLServer();
            case "MySQL":
                return new MySQL();
            default:
                return null;
        }
    }
}


3. Create a Database interface and 4 sub-classes that implement their own connect(), add(), update(), and delete() methods. We don’t care if they are Relational or Non-relational at this point since the factory classes handle that logic.

public interface Database {
    void connect();
    void add();
    void update();
    void delete();
}
public class MongoDB implements Database {
    @Override
    public void connect() {
        System.out.println("Connected to MongoDB Non-Relational Database!");
    }

    @Override
    public void add() {
        System.out.println("Added data to MongoDB Non-Relational Database!");
    }

    @Override
    public void update() {
        System.out.println("Updated data in MongoDB Non-Relational Database!");
    }

    @Override
    public void delete() {
        System.out.println("Deleted data from MongoDB Non-Relational Database!");
    }
}
public class MySQL implements Database {
    @Override
    public void connect() {
        System.out.println("Connected to MySQL Relational Database!");
    }

    @Override
    public void add() {
        System.out.println("Added data to MySQL Relational Database!");
    }

    @Override
    public void update() {
        System.out.println("Updated data in MySQL Relational Database!");
    }

    @Override
    public void delete() {
        System.out.println("Deleted data from MySQL Relational Database!");
    }
}
public class NoSQL implements Database {
    @Override
    public void connect() {
        System.out.println("Connected to NoSQL Non-Relational Database!");
    }

    @Override
    public void add() {
        System.out.println("Added data to NoSQL Non-Relational Database!");
    }

    @Override
    public void update() {
        System.out.println("Updated data in NoSQL Non-Relational Database!");
    }

    @Override
    public void delete() {
        System.out.println("Deleted data from NoSQL Non-Relational Database!");
    }
}
public class SQLServer implements Database {
    @Override
    public void connect() {
        System.out.println("Connected to SQL Server Database");
    }

    @Override
    public void add() {
        System.out.println("Added data to SQL Server Database!");
    }

    @Override
    public void update() {
        System.out.println("Updated data for SQL Server Database!");
    }

    @Override
    public void delete() {
        System.out.println("Deleted data from SQL Server Database!");
    }
}


4. Now we can create the client class and use our factory implementation. We call the static method getFactory(“Relational”) to retrieve our DatabaseFactory method. Then we get the database object that we want to use, SQLServer and MySQL. We then do the same thing for NonRelational.

public class AbstractFactoryClient {
    public static void main(String[] args) {
        //Returns a Relational DatabaseFactory object.
        DatabaseFactory relationalFactory = DatabaseFactory.getFactory("Relational");

        //Returns a SQLServer Database object.
        Database sqlServer = relationalFactory.getDatabase("SQLServer");
        sqlServer.connect();
        sqlServer.add();
        sqlServer.update();
        sqlServer.delete();
        System.out.println();

        //Returns a SQLServer Database object.
        Database mySql = relationalFactory.getDatabase("MySQL");
        mySql.connect();
        mySql.add();
        mySql.update();
        mySql.delete();
        System.out.println();

        //Returns a Non-Relational DatabaseFactory object.
        DatabaseFactory nonRelationalFactory = DatabaseFactory.getFactory("NonRelational");

        //Returns a Mongodb Database object.
        Database mongoDb = nonRelationalFactory.getDatabase("Mongodb");
        mongoDb.connect();
        mongoDb.add();
        mongoDb.update();
        mongoDb.delete();
        System.out.println();

        //Returns a NoSQL Database object.
        Database noSql = nonRelationalFactory.getDatabase("NoSQL");
        noSql.connect();
        noSql.add();
        noSql.update();
        noSql.delete();
        System.out.println();
    }
}
Connected to SQL Server Database
Added data to SQL Server Database!
Updated data for SQL Server Database!
Deleted data from SQL Server Database!

Connected to MySQL Relational Database!
Added data to MySQL Relational Database!
Updated data in MySQL Relational Database!
Deleted data from MySQL Relational Database!

Connected to MongoDB Non-Relational Database!
Added data to MongoDB Non-Relational Database!
Updated data in MongoDB Non-Relational Database!
Deleted data from MongoDB Non-Relational Database!

Connected to NoSQL Non-Relational Database!
Added data to NoSQL Non-Relational Database!
Updated data in NoSQL Non-Relational Database!
Deleted data from NoSQL Non-Relational Database!


Process finished with exit code 0

Facade Design Pattern

 The Facade pattern is a Structural design pattern that provides a simple interface to hide the complex system underneath. It reduces dependencies to the client into a simplified interface. The client does not see what’s “under the hood” by reducing the dependencies into a simplified interface.

Facade makes APIs easier to use. Examples include a database connection API such as JDBC or a Redis caching service. You can create the underlying system to handle all of the logic that interacts with the database. Then you can have a Facade class that is called from client to Add, Update, or Select data from the database.

Let’s look at a simple implementation that allows the Client to view Upperbody and LowerBody workouts.

Here we are going to create a Workout interface with methodssetWorkout() and viewWorkout() and concrete classes LowerBody and UpperBody.  We then add the WorkoutFacade which interfaces the Workout implementations. The Client has no idea about the LowerBody and UpperBody classes. It just knows the two methods in WorkoutFacade, viewUpperBodyWorkout() and viewLowerBodyWorkout(). Let’s look at each class step by step.

  1. Create the Workout interface.
public interface Workout {

    void setWorkout();

    void viewWorkout();
}
  1. Create a simple Exercise class that contains an id and name. This is to store the exercises as an object.
public class Exercise {
    private int id;

    private String name;

    public Exercise(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

}
  1. Create the Concrete classes LowerBody and UpperBody that inherit Workout. In the setWorkout method we are adding hard coded exercises to a list. In viewWorkout(), we run an enhanced for loop and print out the Exercises names.

import java.util.ArrayList;
import java.util.List;

public class LowerBody implements Workout {

    private List<Exercise> exercises = new ArrayList<>();

    /**
     * Add lower body exercises to an ArrayList
     */
    @Override
    public void setWorkout() {
        exercises.add(new Exercise(1, "Squats"));
        exercises.add(new Exercise(2, "Calf raises"));
        exercises.add(new Exercise(3, "Lunges"));
    }

    /**
     * Loop through the list of exercises and print them out
     */
    @Override
    public void viewWorkout() {
        System.out.println("LowerBody Workout");
        for(Exercise exercise : exercises){
            System.out.println("Exercise: " + exercise.getName());
        }
    }
import java.util.ArrayList;
import java.util.List;

public class UpperBody implements Workout {

    private List<Exercise> exercises = new ArrayList<>();

    /**
     * Add upper body exercises to an ArrayList
     */
    @Override
    public void setWorkout() {
        exercises.add(new Exercise(1, "Bench Press"));
        exercises.add(new Exercise(2, "Push Ups"));
        exercises.add(new Exercise(3, "Inline Bench Press"));
    }

    /**
     * Loop through the list of Exercises and print them out
     */
    @Override
    public void viewWorkout() {
        System.out.println("UpperBody Workout");
        for(Exercise exercise : exercises){
            System.out.println("Exercise: " + exercise.getName());
        }
    }
}
  1. Create the WorkoutFacade class that will hide all of the logic from the Client. Here we create viewUpperBodyWorkout and viewLowerBodyWorkout that will call setWorkout and viewWorkout. The client does not access Workout, UpperBody, or LowerBody.
public class WorkoutFacade {

    /**
     * Creates an instance of UpperBody and calls setWorkout() and viewWorkout()
     */
    public void viewUpperBodyWorkout() {
        Workout workout = new UpperBody();
        workout.setWorkout();
        workout.viewWorkout();
    }

    /**
     * Creates an instance of LowerBody and calls setWorkout() and viewWorkout()
     */
    public void viewLowerBodyWorkout() {
        Workout workout = new LowerBody();
        workout.setWorkout();
        workout.viewWorkout();
    }
}
  1. Last but not least, we will create the WorkoutClient. We create an instance of WorkoutFacade and call viewUpperBodyWorkout() and viewLowerBodyWorkout(). This will produce the following output.
public class WorkoutClient {

    public static void main(String[] args){
        WorkoutFacade workoutFacade = new WorkoutFacade();

        workoutFacade.viewUpperBodyWorkout();
        System.out.println();
        workoutFacade.viewLowerBodyWorkout();
    }
}
UpperBody Workout
Exercise: Bench Press
Exercise: Push Ups
Exercise: Inline Bench Press

LowerBody Workout
Exercise: Squats
Exercise: Calf raises
Exercise: Lunges

Adapter Design Pattern

The Adapter pattern is a Structural design pattern that works as a bridge between multiple interfaces.

The Adapter Pattern will contain a class that will interface over another class that is not compatible in your application, and will make it compatible. The Adapter pattern is useful for connecting new code to a legacy application. For example, a new data source that contains similar data but different naming conventions and types. The Adapter class will return the data to the client the same way as our original data source.

Here’s another example that might be useful. Let’s say you want to connect an HDMI device to an old Box TV. Box TVs don’t have HDMI inputs, so you would need an adapter to convert HDMI to VGA. This is how this design pattern works. You have an application (TV) and a new class (HDMI) which can’t inherit from the same interface as RCA cables. You would build an Adapter class that interfaces over HDMI that can be used by TV in order to make this connection work.

Let’s look at another example in Java.

Here we have a Company interface that contains 4 methods to be implementedWe have a CompanyDB class that inherits Company. These are essentially Getters for id, name, description and foundedDate. Let’s say we get another data source to pull from in our application CompanyOtherDB (Not a recommended class name) . This contains similar data but different names and types. We can’t directly inherit Company from this class because there are different names and types. The Adapter pattern will take care of this for us. Let’s look at each class step by step.

  1. Create the Company Interface.
public interface Company {
    int getId();
    String getName();
    String getDescription();
    String getFoundedDate();
}
  1. Create the CompanyDB class that inherits Company. Add the private variables for id, name, description and foundDate. Add the constructor as well.
public class CompanyDB implements Company {

    private int id;
    private String name;
    private String description;
    private String foundedDate;

    public CompanyDB(int id, String name, String description, String foundedDate) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.foundedDate = foundedDate;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getFoundedDate() {
        return foundedDate;
    }
}
  1. Create the class CompanyOtherDB. This class acts as an interface to other data source we mentioned above. I’m sure when you look at this class, there are ways to make this class inherit Company, but that’s not the point of the pattern. The goal is here to not change any legacy code, because remember, we are adding this class after the fact.
public class CompanyOtherDB {

    private String cid;
    private String companyName;
    private String companyInformation;
    private String dateFounded;

    public CompanyOtherDB(String cid, String companyName, String companyInformation, String dateFounded) {
        this.cid = cid;
        this.companyName = companyName;
        this.companyInformation = companyInformation;
        this.dateFounded = dateFounded;
    }

    public String getCid() {
        return cid;
    }

    public String getCompanyName() {
        return companyName;
    }

    public String getCompanyInformation() {
        return companyInformation;
    }

    public String getDateFounded() {
        return dateFounded;
    }
}
  1. Create CompanyAdapter that inherits Company. In this class, we create a private instance of CompanyOtherDB and set this in the Constructor. In the override methods, we return the properties from CompanyOtherDB as if they work with Company. 
/**
 * Inherits Company
 */
public class CompanyAdapter implements Company {

    //Instance of CompanyOtherDB
    private CompanyOtherDB companyOtherDB;

    //Takes an instance as parameter
    public CompanyAdapter(CompanyOtherDB companyOtherDB) {
        this.companyOtherDB = companyOtherDB;
    }

    /**
     * Returns the cid as an integer instead of a string
     *
     * @return
     */
    @Override
    public int getId() {
        return Integer.parseInt(companyOtherDB.getCid());
    }

    /**
     * Returns companyName as Company.name
     * @return
     */
    @Override
    public String getName() {
        return companyOtherDB.getCompanyName();
    }

    /**
     * Returns companyInformation as Company.description
     * @return
     */
    @Override
    public String getDescription() {
        return companyOtherDB.getCompanyInformation();
    }

    /**
     * Returns dateFounded as Company.foundedDate
     * @return
     */
    @Override
    public String getFoundedDate() {
        return companyOtherDB.getDateFounded();
    }
}
  1. In the CompanyClient, we create an instance of Company and CompanyOtherDB. We pass companyOtherDB into CompanyAdapter and it will return us an instance of Company. That is how the Adapter Design Pattern works.
public class CompanyClient {

    public static void main(String[] args){
        //Instance of company
        Company company = new CompanyDB(1, "Amazon", "Online shopping", "1994");

        //Instance of companyOtherDB
        CompanyOtherDB companyOtherDB = new CompanyOtherDB("2", "Best Buy", "Electronics Retailer", "1966");

        //This won't work!
        //Company company2 = new CompanyOtherDB("3", "Facebook", "Social Media and Technology", "2004");
        //Error: Incompatible Types

        Company newCompany = new CompanyAdapter(companyOtherDB);

        System.out.println("Company Id from Original DB: " + company.getId());
        System.out.println("Company Name from Original DB: " + company.getName());
        System.out.println("Company Description from Original DB: " + company.getDescription());
        System.out.println("Company Founded Date from Original DB: " + company.getFoundedDate());
        System.out.println();
        System.out.println("Company Id from New DB: " + newCompany.getId());
        System.out.println("Company Name from New DB: " + newCompany.getName());
        System.out.println("Company Description from New DB: " + newCompany.getDescription());
        System.out.println("Company Founded Date from New DB: " + newCompany.getFoundedDate());
    }
}
Company Id from Original DB: 1
Company Name from Original DB: Amazon
Company Description from Original DB: Online shopping
Company Founded Date from Original DB: 1994

Company Id from New DB: 2
Company Name from New DB: Best Buy
Company Description from New DB: Electronics Retailer
Company Founded Date from New DB: 1966

Factory design pattern in Java with Example

The Factory pattern is a parameter driven creational design pattern. It is one of the most popular design patterns in Java. The Factory pattern does not expose the object instantiation logic to the client by deferring to the sub classes. All the client knows about is common interface that the factory exposes.

Lets implement an example of the Factory pattern for a simple workout application that lets you create a specific workout. We are going to create a Workout interface that contains a createWorkout() method. We will then create two concrete sub classes that inherit Workout, Upperbody and Lowerbody. We will then create a WorkoutFactory class that will be called from the client main method. WorkoutFactory will instantiate an object for us depending on the parameter that was passed into getWorkout(workoutType).

1. Create the Workout interface.

public interface Workout {
    void createWorkout();
}

2. Create the UpperBody and LowerBody sub classes that inherit Workout.

public class LowerBody implements Workout {

    @Override
    public void createWorkout() {
        System.out.println("Created Lower Body Workout that includes:");
        System.out.println("1. Squat");
        System.out.println("2. Lunges");
        System.out.println("3. Calf raises");
    }
}
public class UpperBody implements Workout {

    @Override
    public void createWorkout() {
        System.out.println("Created Upper Body Workout that includes:");
        System.out.println("1. Bench Press");
        System.out.println("2. Push ups");
        System.out.println("3. Incline Bench Press");
    }
}

3. Create the WorkoutType enum to avoid using hard coded strings.

public enum WorkoutType {

    UPPERBODY, LOWERBODY;
}

4. Create a WorkoutFactory class that will contain a static method getWorkout(workoutType). This will be static so we don’t have to create an instance of WorkoutFactory. Instead, we just reference from the class name in the client. This method will create a object for us based on the parameter that is passed.

public class WorkoutFactory {

    public static Workout getWorkout(WorkoutType workoutType) {
        switch (workoutType) {
            case UPPERBODY: {
                return new UpperBody();
            }
            case LOWERBODY: {
                return new LowerBody();
            }

            default: {
                return null;
            }
        }
    }
}

5. In your client/main method, use the WorkoutFactory class to get the workout based on the WorkoutType. The client only cares about the Workout object and doesn’t directly interface with any sub classes here.

public class Main {

    public static void main(String[] args) {
        //Retreives the UpperBody object
        Workout workout = WorkoutFactory.getWorkout(WorkoutType.UPPERBODY);
        
        //Calls createWorkout from UpperBody
        workout.createWorkout();

        //Retrieves the LowerBody object
        workout = WorkoutFactory.getWorkout(WorkoutType.LOWERBODY);

        //Calls createWorkout from LowerBody
        workout.createWorkout();
    }
}


6. Verify the output.

Created Upper Body Workout that includes:
1. Bench Press
2. Push ups
3. Incline Bench Press
Created Lower Body Workout that includes:
1. Squat
2. Lunges
3. Calf raises

 That’s the basic idea of the Factory pattern. This is a simple example but this pattern can get quite complex in a enterprise size application. This pattern is almost the opposite of the Singleton pattern. Singleton returns the same instance and has no interface or subclasses whereas the Factory pattern returns multiple instances and contains sub classes and interfaces.

Thread Safe Singleton Design Pattern in Java.

Singleton is a Creational Design Pattern that allows you to only have one object at a time. This object will be static, so a new instance won’t be created every time you retrieve the object. In the singleton pattern, we create a getInstance() method that retrieves an instance of the class for you versus creating an object yourself. I will show you a simple example and how to make this example thread safe.

public class SingletonClass {

    //SingletonClass instance is created at compile time
    private static SingletonClass singletonClass = new SingletonClass();

    public double value;

    //Constructor cannot be created directly because it is private
    //value is set to 0 when class is compiled
    private SingletonClass(){
        value = 0;
    }

    //The Client(Main method) will call getInstance() to get the instance instead of creating an object
    public static SingletonClass getInstance(){
        return singletonClass;
    }
}
In the SingletonClass,  we initialize the singletonClass at compile time since we are setting the variable immediately. We have a value that we can update directly since it is a public variable. We made the constructor private so that the class cannot be instantiated from the client program (In our case, the main method below). You can get the SingleClass instance using SingletonClass.getInstance() versus SingletonClass instance = new SingleClass()

Lets look at the main method.

public class Main {
    public static void main(String[] args) {
      
        SingletonClass singletonClass1 = SingletonClass.getInstance();
        singletonClass1.value += 5;

        System.out.println("SingletonClass1 value: " + singletonClass1.value);

        SingletonClass singletonClass2 = SingletonClass.getInstance();
        singletonClass2.value += 5;

        System.out.println("SingletonClass2 value: " + singletonClass2.value);

        SingletonClass singletonClass3 = SingletonClass.getInstance();
        singletonClass3.value += 5;

        System.out.println("SingletonClass2 value: " + singletonClass1.value);

    }
}

Here we instantiated SingletonClass into 3 seperate variables. Since the instance is static, each variable will contain the same instance. As you can see, we use each variable to increment the value by 5. Since it maintains the same instance, it will print out as 5, 10, 15 instead of 5, 5, and 5.

There are 2 potential issues with this code though.

  1. The instance is not lazily loaded. This means that the instance is created at compile time versus when we need to get the instance. When the class is loaded, the instance is created when we initialize the variable. 
    private static SingletonClass singletonClass = new SingletonClass();

We only want to create the instance when we call getInstance() the very first time. Let’s make this change:
public class SingletonClass {

    //SingletonClass instance is initialized to null at compile time
    private static SingletonClass singletonClass = null;

    public double value;

    private SingletonClass(){
        value = 0;
    }

    public static SingletonClass getInstance(){
        //The singletonClass is initialized when we call getInstance() the very first time.
        if (singletonClass == null){
            singletonClass = new SingletonClass();
        }
        return singletonClass;
    }
}
As you can see, we declared the instance as null instead of a new SingletonClass(). Then we add a null check in the getInstance() method. This will only initialize the instance when call getInstance() the first time (when we actually need it).


      2. This class is not thread safe.

This means that if multiple threads are running and want to call getInstance() at the same time. Then there is a chance that 2 threads will get past the null check and singletonClass = new SingletonClass(); will get called twice.

 public static SingletonClass getInstance(){
        //This synchronized block will not allow 2 threads to get inside. One thread will enter the
        //block while the second thread waits for it to complete.
        if(singletonClass == null){
            synchronized (SingletonClass.class){
                if (singletonClass == null){
                    singletonClass = new SingletonClass();
                }
            }
        }
        return singletonClass;
    }

Here we added a synchronized block that will only allow 1 thread in at a time. 1 thread will wait while another is in the block, checks if the instance is null, and exits. I added a separate null check inside because 2 threads could get past the null check the first time getInstance is called. We also don’t add synchronized outside the null check because we would only have one thread checking if it’s null at a time. This would be detrimental to performance.

Steps: Upgrade Mongodb version 3.2 to 4.2 on Ubuntu 16.04

Updating MongoDB servers are pretty straight forward but require you to update version to version (3.2 -> 3.4 -> 3.6 -> 4.0 -> 4.2). This is a very easy process and we will go step by step.

NOTE: The MongoDB data will not be affected by this process, avoid removing the /data folder which contains all mongo records.

SSH into your MongoDB instance.

MongoDB 3.2 -> 3.4

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-3.4.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.2.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.2.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “3.4”})

MongoDB 3.4 -> 3.6

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-3.6.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.4.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.4.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “3.6”})

MongoDB 3.6 -> 4.0

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.6.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.6.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “4.0”})

MongoDB 4.0 -> 4.2

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.6.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-4.0.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “4.2”})


That’s it! Simple steps to update to each version of Mongo on any environment. 

How to unit test a function that converts a ResultSet into a list using Mockito.

If you have experience using JDBC driver in Java, you understand that when you call a Stored Procedure it will return you a result set.

If you want to convert this into an object, you will have to loop through the result set and explicitly create the list of objects, like so.

package resultSet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ResultSetParser {

    public List<Employee> processEmployeeresults(ResultSet resultSet) throws SQLException {
        List<Employee> employeeList = new ArrayList<>();
        while(resultSet.next()){
            Employee employee = new Employee();
            employee.setId(resultSet.getInt("Id"));
            employee.setFirstName(resultSet.getString("FirstName"));
            employee.setLastName(resultSet.getString("LastName"));
            employee.setTitle(resultSet.getString("Title"));
            employee.setBirthdate(resultSet.getString("BirthDate"));

            employeeList.add(employee);
        }
        return employeeList;
    }
}

Mockito is a very useful test framework in this instance. We will mock the ResultSet, call the function, and verify a few properties match.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.util.Assert;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class ResultSetParserTest {

    @Mock
    ResultSet resultSet;

    @Test
    public void testEmployeeResultSet(){
        try {
            createEmployeeResultSet();
            ResultSetParser parser = new ResultSetParser();
            List<Employee> employeeList = parser.processEmployeeresults(resultSet);
            //Grab the first Result Set
            Employee employee = employeeList.get(0);

            Assert.notEmpty(employeeList, "Employees were not parsed.");
            Assert.isTrue(employee.getId() == 1, "Employee Id is not equal to 1");
        } catch (SQLException e) {

        }
    }

    private void createEmployeeResultSet() throws SQLException {
        Mockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
        Mockito.when(resultSet.getInt("Id"));
        Mockito.when(resultSet.getString("FirstName"));
        Mockito.when(resultSet.getString("LastName"));
        Mockito.when(resultSet.getString("Title"));
        Mockito.when(resultSet.getString("BirthDate"));
    }
}

There you have it, an easy solution for mocking and testing result sets. The resultSet.next().thenReturn(true).thenReturn(false); essentially opens the ResultSet for one iteration, so there will only be one record in the ResultSet.