Click here to Skip to main content
15,867,568 members
Articles / All Topics
Technical Blog

Enterprise Architect - Which pattern should I use?

Rate me:
Please Sign up or sign in to vote.
4.44/5 (6 votes)
1 Jun 2017Apache5 min read 6.4K   3   2
Overview In this article, we will discuss about the number of patterns which can be used for your application. There are many types of pattern for the simple to complex application as below: Multiple-Tiers Reporting database CQRS CQRS-DDD-ES Microservice Microservice-CQRS-DDD-ES We will go

Overview

In this article, we will discuss about the number of patterns which can be used for your application.

There are many types of pattern for the simple to complex application as below:

  1. Multiple-Tiers
  2. Reporting database
  3. CQRS
  4. CQRS-DDD-ES
  5. Microservice
  6. Microservice-CQRS-DDD-ES

We will go through one by one briefly

 

Multiple-Tiers

In this pattern, we simply divide the application into 2 or multiple separated layers, each has its own purpose/ responsibility.

Commonly, we usually have those layers for simple web-api application:

  • Controller: defines access point that can be called from remote client
  • Service: handles business rules, validate information
  • Repository: performs repository I/O operation

An overview diagram about this pattern as below:

For example, With simple user creation, we can have this code in each layer as below:

Controller (UsersController.cs):
[RoutePrefix("api/users")]
public class UsersController{
	[HttpPost]
	[Route("")]
	public Guid CreateUser(CreateUserRequest request){
		UserService service = new UserService();
		return service.Create(request);
	}
}
 And Service layer (UserService class):
public class UserService{
	public Guid CreateUser(CreateUserRequest request){
		this.ValidateCreateUserRequest();
		UserRepository repository = new UserRepository();
		return repository.Add(new User(request.FirstName, request.LastName, request.Email));
	}
}
And Repository layer (UserRepository class):
public class UserRepository{
	public Guid Add(User user){
		DbContext dbContext = new DbContext();
		dbContext.Users.Add(user);
		dbContext.SaveChanges();
		return user.Id;
	}
}

 This is pseudo code, please do not try to run it.

We can see that, service layer acts as the heart of you system. all the business and validation rules were there.

Scale

In this pattern, all data goes into single database. this can increase the size of database to hundred of GB which is not good from the view of performance and maintenance.

There are some plans for scaling your database:

  • Partition: please search on internet for more information.
  • Split your table: Split big table into smaller tables using some rules, such as:  
    • Order with ID between 0 and 1.000.000 will goes into Order_1 table
    • Order with ID between 1.000.001 and 2.000.000 will goes into Order_2 table

So, we can base on ID of order to know which table should we get the order from.

  • Split your database: We will break the database of the application into smaller, isolated databases by grouping related data together. Such as:
    • All data related to Order should be located in Order database
    • All data related to Purchase should be located in Purchase database ...

Of course, Getting/ updating data that was spanned through multiple databases will be more complex. In this case, we need to use distributed transaction or event driven pattern for solving this issue.

Belong to specified type of application, we may select different plan to scale your database.

Below is the diagram of multi layers for multiple database:

All the business was implemented in service layer, so in the future, we can add new feature into for our application with out rewrite what has been done, such as: support web and mobile application also. What we need to do in this case is "Build the web/ mobile UI" and "attach" into current system. It will work well.

When should I use?

This is the right pattern for the case we have the simple business, such as CRUD user or small stand alone feature, ...

For more information, how did we use this in TinyERP, Please have a look at this link (click me). Check the api part please.

 

Reporting database

We usually applied this pattern for management application, such as: Timesheet, HRM, SCM, ....

For those types of application, we need to see many reports which collect data form many sources, tables...

We usually join between tables, perform many calculations when retrieving this data. This raises performance issues. As joining between 2 table with 10.000.000 records usually takes around 5 minutes or more is normal.

So in this case, if the system prepareed reporting data for us automatically and store it some where( in other table, other database, or files ...).

Then, we just come and get the expected data as we need and do not need to calculate. This also increase performance of our application in total.

Photo below, describing an overview about how it works:

In this pattern, we usually have separated write side and read side.

  • Write side: for all actions that can change the state of data in system, this usually implements all logic of the application in this side and store data into master repository. Then there will be scheduled tasks or subscribed events which sync changes to repository for read side.
  • Read side: this usually a thin layer, which goes to appropriated repository and get expected data was stored in expected location/ repository in expected format.

Look at scenario below and see how will it work:

User wants to create new order contains information of customer, order-line (by what, price, ...). Then manager would like to see sale report(including total number of orders, total income for current day).

The photo below describe step by step:

  1. Client app will send CreateOrderRequest to OrderController for creating new order. CreateOrderRequest contains all necessary information to create new order.
  2. OrderController sends CreateOrderRequest to OrderService, this is where the CreateOrderRequest was validated, if all business rules were passed( customer balance, stock in inventory, ...) new order will be created.
  3. New order will be saved into master repository. Customer balance, stock in inventory were also updated.
  4. There will be scheduled tasks/ subscribed events for updating these changes to read database.
  5. Manager requests to see the sale report.
  6. This request was passed to another service and continue forwarded to repository
  7. Repository just reads necessary data from read database.

We can see that, at the time of retrieving data for sale report, the application just goes to read database and get expected data. There is not extra calculation. So the operation will be executed faster, thus improve performance of the application in total.

Thank you for reading,

Note: Please like and share to your friends if you think this is usefull article, I really appreciate

 

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Architect
Vietnam Vietnam
I have more than 8 years in web development for multiple types of applications (ERP, Education System, ...).
I usually organize training/ coaching on specified topic (such as: RESTful/ WebApi, Angular2, BEM, LESS, SASS, EF, NodeJs ....). Please contact me on Skype (tranthanhtu83) or email (contact@tranthanhtu.vn) if need.
For more information about me, Please visit http://www.tranthanhtu.vn/page/about-me

Comments and Discussions

 
QuestionMixing up code base architecture and deployment architecture Pin
Gerd Wagner2-Jun-17 2:00
professionalGerd Wagner2-Jun-17 2:00 
AnswerRe: Mixing up code base architecture and deployment architecture Pin
tranthanhtu.vn2-Jun-17 3:01
professionaltranthanhtu.vn2-Jun-17 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.