Sunday, February 19, 2017

Hello, Hexagonal Architecture

Hello Hexagonal Architecture


While developing a software we always craving for Architecture because we want our software adopts future changes easily.

So, Architecture stepped in for maintainability. Here maintainability mean how easily we can incorporate changes without disturbing old.

Every feature addition in a software comes with a Risk, Risk of breaking old,maintainability,rigidity,dependencies etc, There are many shades of risks
we called it Technical debt.


Technical debt has an inverse relationship of maintainability, using Architectural style we want to decrease the risk or I can say make it constant while adding new features.

We experienced that when we start writing code at that time it is easy to add features and gradually it becomes hard unless we do not use proper architecture for that software.


Hexagonal Architecture:

We will discuss Hexagonal Architecture here, in one word Hexagonal Architecture says.

Core business logic talks to other through a contract.

The core business logic means your software development logic should talk to others means Database or any JMS Queues or any Flat files or HTTP request or FTP , NOSQL etc through a contract or in Java language we can say interface.

The benefits of above type of design is whatever the input or output channels that boil down to the contract so every channel has to implement that contract/interface to talking with our software, So for our software perspective all are same as all implement the same contract so our software can deal with any types of input and output channel .

Benefits:

1, Easily incorporates any channel like Flat file,RDBMS, NoSQL, Http,Ftp,JMS etc.
2. Our software can be easily tested because it is easy to create a mock as just need to implement the contracts.

3. Adding new requirements means plugging it, or implementing the contracts.
4. Proper separation of concern.
5. Maintains IOC as Higher level and lower level talking through contract.


Sometimes we called Hexagonal architecture as Port and Adapter or Onion architecture.

As in this architecture, we have a port for each channel say for Database

The tasks we have to perform

To implement the contract to talk with our software.
As database API say JDBC is itself a contract or Hibernate is itself a framework so we need to create an adaptor GOF adapter pattern where we have to implement a strategy which converts JDBC related operation to our contract related operation.
Then plug this adaptor into our port to talk with this channel.



Outline of the design

Software Contract

package com.example.architecture.hexagonal;

public interface IPersists<T,TCOMMAND> {
   
   public void save(T t,TCOMMAND commandObject);    
   public void delete(T t,TCOMMAND commandObject);
   

}

This is the general contract for talking with our software, any output channel like File channel, RDBMS,NoSQL should implement that contract.


Here I take an example How it can talk to JPA entity which saves that data in Database.

DataBaseChannelAdapter.java
package com.example.architecture.hexagonal;

public class DataBaseChannelAdapter implements IPersists<EmployeeDomainObject,EmployeeCommand>{

   public void save(EmployeeDomainObject t, EmployeeCommand commandObject) {
     
       String underLyingJPAEntity = commandObject.getEntityClass();
       System.out.println("call save on " + underLyingJPAEntity);
     
   }

   public void delete(EmployeeDomainObject t, EmployeeCommand commandObject) {
       String underLyingJPAEntity = commandObject.getEntityClass();
       System.out.println("call delete on " + underLyingJPAEntity);
     
   }

}


As JPA Entity use some JPA related annotation so I did not include this JPA entity as part of our Domain, I use JPA framework as an Outside Channel of our software domain and DataBaseChannelAdapter takes our core domain Employee Object and takes a Command Object which tells Adapter to Which JPA entity to call and call the same.


EmployeeDomainObject

package com.example.architecture.hexagonal;

public class EmployeeDomainObject {
   
   public String name;
   public String address;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   @Override
   public String toString() {
      return "EmployeeDomainObject [name=" + name + ", address=" + address
              + "]";
   }
   
   

}


Our core Domain Employee Object it does not dependent on any Framework so I can plug any framework with it through Adapter like I can easily change DatabaseAdapter to FileAdepter to save our core domain object in File.




EmployeeCommand.java

package com.example.architecture.hexagonal;

public class EmployeeCommand{
   
   public String entityClass;

   
   
   public String getEntityClass() {
      return entityClass;
   }

   public void setEntityClass(String entityClass) {
      this.entityClass = entityClass;
   }

   @Override
   public String toString() {
      return "EmployeeCommand [entityClass=" + entityClass + "]";
   }

   
   

}





This Command object is nothing but help Adapter to convert Core Domain Object to Underlying Output channel (Database or File)

EmployeeDomainDao.java

package com.example.architecture.hexagonal;

public class EmployeeDomainDao<T,TCommand>{
   
   IPersists<T,TCommand> adapter;
   
   
   public void save(T t,TCommand commandObject)
   {
      adapter.save(t, commandObject);
   }
   
   public void delete(T t,TCommand commandObject)
   {
      adapter.delete(t, commandObject);
   }

   public IPersists<T, TCommand> getAdapter() {
      return adapter;
   }

   public void setAdapter(IPersists<T, TCommand> adapter) {
      this.adapter = adapter;
   }
   
   
   

}
This is our Core Domain Persist layer here I use the adapter as Strategy based on the Adapter it will call exact output channel and persist our Domain Object.





Now Time to Test our Software design


Main.java

package com.example.architecture.hexagonal;

public class Main {
   
   public static void main(String[] args) {
      EmployeeDomainDao<EmployeeDomainObject,EmployeeCommand> dao = new EmployeeDomainDao<EmployeeDomainObject,EmployeeCommand>();
      IPersists<EmployeeDomainObject,EmployeeCommand> adapter= new DataBaseChannelAdapter();
      dao.setAdapter(adapter);
      EmployeeDomainObject emp = new EmployeeDomainObject();
      emp.setName("Shamik Mitra");
      emp.setAddress("India,Kolkata");
     
      EmployeeCommand command = new EmployeeCommand();
      command.setEntityClass("com.employeemanagement.entity.EmployeeJpaEntity");
     
      dao.save(emp, command);
      dao.delete(emp, command);
   }

}








Output:

Object StateEmployeeDomainObject [name=Shamik Mitra, address=India,Kolkata]
call save on com.employeemanagement.entity.EmployeeJpaEntity
Object StateEmployeeDomainObject [name=Shamik Mitra, address=India,Kolkata]
call delete on com.employeemanagement.entity.EmployeeJpaEntity





Hexagonal Layers :






Hexagonal architecture,port & adapter architecture,onion Arhitechture

Picture courtesy Google



Application Domain: As I said earlier it is the software where all software related business logic, validation goes on, this is the inside module every outside module talks with it through contract.

Application/Mediation Layer:  Kind of services layer it adopts the Framework layer or ut’s outside layer and make necessary changes according to domain layer contract to talks with Domain layer or in the same way return back the result from Domain to Framework layer.
It sits between Framework and Domain layer.


Framework Layer: This layer as to input/output channel or we can say the outside world and use an adaptor to adapt the data and transform it according to our  software contract.