Reader small image

You're reading from  Building Microservices with .NET Core

Product typeBook
Published inJun 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781785887833
Edition1st Edition
Languages
Right arrow
Authors (3):
Gaurav Aroraa
Gaurav Aroraa
author image
Gaurav Aroraa

Gaurav Aroraa has done M.Phil in computer science. He is a Microsoft MVP, life time member of Computer Society of India (CSI), certified as a scrum trainer/coach, XEN for ITIL-F and APMG for PRINCE-F and PRINCE-P. Gaurav serves as a mentor at IndiaMentor, open source developer, contributor to TechNet Wiki co-founder of Innatus Curo Software LLC. In 19+ years of his career, he has mentored thousands of students and industry professionals. You can tweet Gaurav on his twitter handle @g_arora
Read more about Gaurav Aroraa

Lalit Kale
Lalit Kale
author image
Lalit Kale

Lalit Kale is a technical architect and consultant with more than 12 years of industry experience. Lalit has helped clients achieve tangible business outcomes through the implementation of best practices in software development. He is a practitioner of TDD and DDD, and a big believer in agile and lean methodologies. He has worked with several organizations, from start-ups to large enterprises, in making their systems successful, be it in-house or mission critical, with clients in the USA, the UK, Germany, Ireland, and India. His current interests include container technologies and machine learning using Python. He holds a bachelor's degree in engineering (IT).
Read more about Lalit Kale

Manish Kanwar
Manish Kanwar
author image
Manish Kanwar

Manish Kanwar completed his masters of science in computer applications from MD University, India, and is a cofounder of Innatus Curo Software LLC, with a presence in India. He has been working in the IT industry across domains for the last 17 years. He started exploring .NET right from the first release and has been glued to it ever since. His range of experience includes global wealth management (financial service industry, USA), life insurance (insurance industry, USA), and document management system (DMS), ECMS, India. Manish does his bit for the community by helping young professionals through the IndiaMentor platform.
Read more about Manish Kanwar

View More author details
Right arrow

Chapter 2. Building Microservices

In the previous chapter, we discussed the problems of a layered monolith architecture. In this chapter, we will discuss how we can refactor them from the existing system and build separate microservices for product and order. In this chapter, we will cover the following topics:

  • Size of microservices
  • What makes a good service?
  • Domain-driven design (DDD) and its importance for microservices
  • The concept of Seam
  • Communication between microservices
  • Revisiting the case study--Flix One

Size of microservices


Before we start building our microservices, we should be clear about a few of its basic aspects, such as what factors to consider while sizing our microservices and how to ensure their isolation from the rest of the system.

As the name suggests, microservices should be micro. A question arises: what is micro? Microservices is all about size and granularity. To understand this better, let's consider the application discussed in Chapter 1What are Microservices?

We wanted the teams working on this project to stay synchronized at all times with respect to their code. Staying synchronized is even more important when we make a release of the complete project. For this, we needed to first decompose our application/specific parts into smaller functionalities/segments of the main service. Let's discuss the factors that need to be considered for high-level isolation of microservices:

  • Risk due to requirement changes: Changes in the requirements of one microservice should be independent...

What makes a good service?


Before microservices were conceptualized, whenever we thought of enterprise application integration, middleware looked like the most feasible option. Software vendors offered Enterprise Service Bus (ESB), and it was one of the options as middleware.

Besides considering these solutions, our main priority should be inclined toward the architectural features. When microservices arrived, middleware was no more a consideration. Rather, the focus shifted to contemplation on business problems and how to tackle those problems with the help of the architecture.

In order to make a service that can be used and maintained easily by developers and users, it would require the service to have the following features (we can also consider these as characteristics of good services):

  • Standard data formats: Good services should follow standardized data formats while exchanging with other components, services, or systems. The most popular data formats, also mostly used, in the .NET stack...

DDD and its importance for microservices


Domain-Driven Design (DDD) is a methodology and a process of designing complex systems. In this section, we will briefly discuss DDD and how it is important in the context of microservices.

Domain model design

 The main objective of domain design is to understand the exact domain problems and then draft a model that can be written in any set of language/technologies. For example, in our Flix One bookstore application, we need to understand Order Management and Stock Management.

Here are a few characteristics of the domain-driven model:

  • A domain model should focus on a specific business model and not across multiple business models
  • It should be reusable
  • It should be designed in a way that it should be called in the loosely coupled way, unlike the rest of the system
  • It should be designed independently of persistence implementations.
  • It should be pulled out from a project to another location, so it should not be based on any infrastructure framework.

Importance...

The concept of Seam


At the very core of microservices lies the capability to work on a specific functionality in isolation from the rest of the system. This translates into all the advantages discussed earlier, such as reduced module dependency, code reusability, easier code maintenance, and better deployment.

In my opinion, the same attributes that were attained with the implementation of microservices should be maintained during the process of implementation. Why should the whole process of moving monoliths to microservices be painful and not be as rewarding as having the microservices itself? Just remember that the transition can't be done overnight and would need meticulous planning. Many capable solution architects have differed from my approach while presenting their highly capable teams. The answer lies not just in the points already mentioned but the risk to the business itself at the same time.

This is very well attainable. However, we must identify our way to the path correctly in...

Communication between microservices


In the preceding section, we separated our Order module into Order Services and discussed how we can break down the foreign key relationship between ORDER and PRODUCT tables.

In a monolithic application, we have a single repository that queries the database to fetch the records from both ORDER and PRODUCT tables. However, in our upcoming microservice application, we will segregate repositories between Order Service and Product Service. With each service having its respective database, each one would access its own database only. Order Service would only be able to access order Database, whereas Product Service would be able to access product Database only. Order Service should not be allowed to access product Database and vice versa. Refer to the following image:

Note

We will discuss communication between microservices in Chapter 3, Integration Techniques, in detail.

In the preceding figure, we see that our UI is interacting with Order Service and Product...

Revisiting the case study--Flix One


In the preceding chapter, we took an example of an imaginary company, Flix One Inc., operating in the e-commerce domain and having its own .NET monolithic application: Flix One book store. We have already discussed:

  • How to segregate the code
  • How to segregate the database
  • How to denormalize the database
  • How to begin transitioning
  • The available refactoring approaches 

In this section, we will start writing/transitioning .NET monolith to a microservice application.

Prerequisites

We will use the following tools and technologies while transitioning our monolithic application to the microservice-styled architecture:

  • Visual Studio 2015 or later
  • C# 6.0
  • ASP.NET Core MVC/Web API
  • Entity Framework
  • SQL Server

Transitioning to our product service

We already have our product module in place. We are going to pull back this module now and start with a new ASP.NET Core MVC project. For this, follow all the steps we discussed in the preceding sections and in Chapter 1, What Are Microservices...

Summary


In this chapter, we discussed different factors that can be used to identify and isolate microservices at a high level. We also discussed the various characteristics of a good service. Talking about DDD, we saw what its importance is in the context of microservices.

Further, we analyzed how we can correctly achieve vertical isolation of microservices through various parameters in detail. While we tried to draw upon our previous understanding of the challenges posed by a monolithic application and its solution in microservices, we saw that we can use factors such as module interdependency, technology utilization, and team structure to identify seams and perform the transition from a monolithic architecture to microservices in an organized manner.

It became apparent that the database can pose a clear challenge in the process. However, we identified how we can still perform this with a simple strategy and the possible approaches to do this. We then discussed that with the foreign keys...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Microservices with .NET Core
Published in: Jun 2017Publisher: PacktISBN-13: 9781785887833
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Gaurav Aroraa

Gaurav Aroraa has done M.Phil in computer science. He is a Microsoft MVP, life time member of Computer Society of India (CSI), certified as a scrum trainer/coach, XEN for ITIL-F and APMG for PRINCE-F and PRINCE-P. Gaurav serves as a mentor at IndiaMentor, open source developer, contributor to TechNet Wiki co-founder of Innatus Curo Software LLC. In 19+ years of his career, he has mentored thousands of students and industry professionals. You can tweet Gaurav on his twitter handle @g_arora
Read more about Gaurav Aroraa

author image
Lalit Kale

Lalit Kale is a technical architect and consultant with more than 12 years of industry experience. Lalit has helped clients achieve tangible business outcomes through the implementation of best practices in software development. He is a practitioner of TDD and DDD, and a big believer in agile and lean methodologies. He has worked with several organizations, from start-ups to large enterprises, in making their systems successful, be it in-house or mission critical, with clients in the USA, the UK, Germany, Ireland, and India. His current interests include container technologies and machine learning using Python. He holds a bachelor's degree in engineering (IT).
Read more about Lalit Kale

author image
Manish Kanwar

Manish Kanwar completed his masters of science in computer applications from MD University, India, and is a cofounder of Innatus Curo Software LLC, with a presence in India. He has been working in the IT industry across domains for the last 17 years. He started exploring .NET right from the first release and has been glued to it ever since. His range of experience includes global wealth management (financial service industry, USA), life insurance (insurance industry, USA), and document management system (DMS), ECMS, India. Manish does his bit for the community by helping young professionals through the IndiaMentor platform.
Read more about Manish Kanwar