<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Monstar Lab Bangladesh Engineering - Medium]]></title>
        <description><![CDATA[Stories from the engineering team at Monstar Lab Bangladesh - Medium]]></description>
        <link>https://medium.com/monstar-lab-bangladesh-engineering?source=rss----ba5303c329d8---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Monstar Lab Bangladesh Engineering - Medium</title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering?source=rss----ba5303c329d8---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 23:55:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/monstar-lab-bangladesh-engineering" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Writing Conversational Chatbots Using Rasa]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/writing-conversational-chatbots-using-rasa-3d4ad3b79cd4?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/3d4ad3b79cd4</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[rasa-core]]></category>
            <category><![CDATA[rasa-nlu]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Shahnewaz Leon]]></dc:creator>
            <pubDate>Fri, 31 Jan 2020 09:22:35 GMT</pubDate>
            <atom:updated>2020-01-31T10:10:38.588Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yapldXZ0rP5Rd5nr" /><figcaption>Photo by <a href="https://unsplash.com/@agkdesign?utm_source=medium&amp;utm_medium=referral">Alex Knight</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In the previous <a href="https://medium.com/monstar-lab-bangladesh-engineering/writing-conversational-chatbots-the-basics-925db6d1af1e">article</a>, we talked about the basics of NLP and how a conversational chatbot might work under the hood. In this article, we’ll look into one of the most popular conversational AI frameworks <a href="https://rasa.com/">Rasa</a> and build a very simple chatbot using it.</p><h4>What Rasa provides</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6pxWKinnxSfApL4_hWGmGg.png" /><figcaption>Rasa NLU figures out the intent from a user text — rasa.com</figcaption></figure><p>Holding a conversation requires two basic abilities — Understanding what is being said and responding according to that. Rasa is an open-source AI framework that provides necessary tools for building contextualized chat assistants. Rasa Open Source includes the following modules:</p><ol><li><a href="https://rasa.com/docs/rasa/nlu/about/"><strong>Rasa NLU</strong></a><strong>: </strong>It’s the natural language processing unit for understanding the input language. It provides the tools for intent classification(what the user is talking about) and entity extraction(what are the key information).</li><li><a href="https://rasa.com/docs/rasa/core/about/"><strong>Rasa Core</strong></a><strong>: </strong>It’s the dialog engine of Rasa which takes care of deciding the next course of action based on the input from Rasa NLU. Rather than using a simple if/else condition, it uses Machine Learning to learn from previous conversations and act based on that knowledge.</li></ol><h4>How Rasa works</h4><p>Rasa works as an abstraction over the more lower-level NLP libraries such as <a href="https://spacy.io/">SpaCy</a>, <a href="http://www.nltk.org/">nltk</a>, <a href="http://scikit-learn.org/stable/">sklearn</a>, etc and provides a framework that combines tools required for building fully working contextualized conversational assistants. The Rasa NLU module turns natural language into structured data. This module is able to do two things — intent classification and entity extraction. Intent classification is a two-step process. First, we feed an NLU model with labeled training data that includes a list of known intents and corresponding example texts. Once the model is trained, the model is able to classify a new input text into one of the predefined intents. Entity extraction is the process of identifying and extracting key pieces of information in a given text. An entity can be things like time, place, name of a person, etc.</p><p>Rasa NLU uses pre-configured <a href="https://rasa.com/docs/rasa/nlu/choosing-a-pipeline/">pipelines</a> to accomplish these tasks. Among other steps, a pipeline outlines which algorithms to use to tokenize an input text, to extract the entities and to classify the intents of the text. You can use your own component for any task and build a custom pipeline to use that component. For some pipelines, you need a word-vector whereas for some others the process is <a href="https://medium.com/rasa-blog/supervised-word-vectors-from-scratch-in-rasa-nlu-6daf794efcd8">language agnostic</a> and you don’t require any. Here is the list of components used in a pre-configured pretrained_embeddings_spacy pipeline:</p><pre>language: &quot;en&quot;</pre><pre>pipeline:<br>- name: &quot;SpacyNLP&quot;<br>- name: &quot;SpacyTokenizer&quot;<br>- name: &quot;SpacyFeaturizer&quot;<br>- name: &quot;RegexFeaturizer&quot;<br>- name: &quot;CRFEntityExtractor&quot;<br>- name: &quot;EntitySynonymMapper&quot;<br>- name: &quot;SklearnIntentClassifier&quot;</pre><p>The Rasa Core module takes the output of Rasa NLU (intent and entities) and feeds them into Machine Learning models and generates a reply. It is also responsible for controlling the conversation flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*BW6rDwaNGSZP6K7mGcATMQ.png" /><figcaption>Conversation flow in a rasa-powered AI assistant from <a href="https://rasa.com/docs/rasa/user-guide/architecture/">rasa.com</a></figcaption></figure><p>A typical message flows like this internally — Whenever a new message comes in, Interpreter(NLU) processes it and extracts intent and entities from the message and feeds into the Tracker which keeps track of the conversation state. In the next step, the Policy module decides the next course of action based on the current state and the input data and invokes an Action . The information is sent to the Tracker for updating the conversation state and an appropriate response is sent back to the user.</p><h3>Building a simple chatbot</h3><h4>Installation and Setup</h4><p>There are two ways to install and setup Rasa — installing using python/pip, using pre-configured docker images. We are going to install using pip for this demonstration.</p><ol><li>Install the Python development environment. Rasa requires python3 to be available in the environment.</li></ol><p>Check if Python is already installed and configured in your machine:</p><pre>$ python3 --version<br>$ pip3 --version</pre><p>If these are not installed, <a href="https://realpython.com/installing-python/">install python3</a>.</p><p>2. Create a virtual environment (recommended)</p><pre>$ python3 -m venv --system-site-packages ./venv<br>$ source ./venv/bin/activate</pre><p>3. Install Rasa</p><pre>$ pip install -U pip<br>$ pip install rasa</pre><p>If you would rather run Rasa with Docker, check <a href="https://rasa.com/docs/rasa/user-guide/running-rasa-with-docker/">this</a> out.</p><h4>Build chatbot — Daisy</h4><p>We are going to build a chatbot called Daisy which will be able to detect if you are depressed and cheer you up with funny responses.</p><ol><li><strong>Create a new project:</strong></li></ol><pre>$ rasa init --no-prompt</pre><p>It will create a fully working chatbot on some sample data with the following files in it:</p><pre>files:<br>__init__.py<br>actions.py<br>config.yml<br>credentials.yml<br>domain.yml<br>endpoints.yml</pre><pre>directories:<br>__pycache__<br>data<br>models</pre><p>Let’s briefly explain what each file contains:</p><p>__init__.py : An empty file that helps python find our actions.</p><p>actions.py : If we want Rasa to call an external server for generating responses, we can do that here.</p><p>config.yml : We can specify our required NLU and core components here by using <a href="https://rasa.com/docs/rasa/nlu/choosing-a-pipeline/#choosing-a-pipeline">pipelines</a>.</p><p>credentials.yml: This file contains the credentials for connecting to other services such as Facebook Messenger, Microsoft Bot Framework. We just need to add the appropriate credentials for these and Rasa will take care of the rest.</p><p>data/nlu.md : Contains our NLU training data. We can define the intents we want our bot to detect and provide relevant sentences to train our model.</p><p>data/stories.md : This is required by Rasa Core to decide the conversation flow. Basically you will provide different conversation paths for different intents.</p><p>domain.yml : Our bots domain. List of intents it can detect and the list of replies it can send. We can reply with dynamic content as well by executing the python method defined in our Custom Action Server.</p><p>models/&lt;timestamp&gt;.tar.gz: The chatbot model trained on our training data.</p><p>Let’s test our Chatbot that Rasa created with default data from the terminal:</p><pre>$ rasa shell</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*IzGqku2f0daMdpGxTm281g.png" /><figcaption>Daisy with default data</figcaption></figure><p>2. <strong>Teach Daisy her own tricks</strong></p><p>Let’s teach Daisy to detect if the user is unhappy and respond with a little joke or an image.</p><p>Add the following training data in data/nlu.md</p><pre><strong>## intent:mood_unhappy</strong></pre><pre>- sad</pre><pre>- very sad</pre><pre>- unhappy</pre><pre>- bad</pre><pre>- very bad</pre><pre>- awful</pre><pre>- terrible</pre><pre>- not very good</pre><pre>- extremely sad</pre><pre>- so sad</pre><pre>- depressed</pre><pre>- down</pre><p>Register the newly created intent in domain.yml and specify the actions Daisy can take based on the intent it detects.</p><pre>intents:</pre><pre>- greet</pre><pre>- goodbye</pre><pre>- affirm</pre><pre>- deny</pre><pre>- mood_great</pre><pre>- mood_unhappy</pre><pre>actions:</pre><pre>- utter_greet</pre><pre>- utter_cheer_up_images</pre><pre>- utter_cheer_up_jokes</pre><pre>- utter_happy</pre><pre>- utter_goodbye</pre><pre>- utter_are_you_happy</pre><pre>- utter_sad</pre><pre>templates:</pre><pre>utter_greet:</pre><pre>- text: &quot;Hey! How are you?&quot;</pre><pre>- text: &quot;Hey! What&#39;s up?&quot;</pre><pre>- text: &quot;How&#39;s it going?&quot;</pre><pre>utter_cheer_up_images:</pre><pre>- text: &quot;Here is something to cheer you up:&quot;</pre><pre>image: &quot;https://i.imgur.com/nGF1K8f.jpg&quot;</pre><pre>- text: &quot;Here is something to cheer you up:&quot;</pre><pre>image: &quot;https://i.imgur.com/Tg2ZlTT.jpg&quot;</pre><pre>- text: &quot;Here is something to cheer you up:&quot;</pre><pre>image: &quot;https://i.imgur.com/x4I4vHF.jpg&quot;</pre><pre>utter_cheer_up_jokes:</pre><pre>- text: &quot;Here is a joke to cheer you up - Doctor: I&#39;m sorry but you suffer from a terminal illness and have only 10 to live. Patient: What do you mean, 10? 10 what? Months? Weeks?! Doctor: Nine.&quot;</pre><pre>- text: &quot;Here is a joke to cheer you up - My old aunts would come and tease me at weddings, &#39;Well Sarah? Do you think you’ll be next?&#39; We’ve settled this quickly once I’ve started doing the same to them at funerals.&quot;</pre><pre>- text: &quot;Here is a joke to cheer you up - I dreamed I was forced to eat a giant marshmallow. When I woke up, my pillow was gone.&quot;</pre><pre>utter_are_you_happy:</pre><pre>- text: &quot;Did that help you?&quot;</pre><pre>- text: &quot;Feeling better?&quot;</pre><pre>utter_happy:</pre><pre>- text: &quot;Great, carry on!&quot;</pre><pre>- text: &quot;Awesome!&quot;</pre><pre>- text: &quot;Great!&quot;</pre><pre>utter_sad:</pre><pre>- text: &quot;I&#39;m sorry! Hope you feel better soon&quot;</pre><pre>utter_goodbye:</pre><pre>- text: &quot;Bye&quot;</pre><pre>- text: &quot;Adios&quot;</pre><pre>- text: &quot;Ta Ta&quot;</pre><p>Finally, let’s design the conversation flow in data/stories.md:</p><pre><strong>## happy path</strong></pre><pre>* greet</pre><pre>- utter_greet</pre><pre>* mood_great</pre><pre>- utter_happy</pre><pre><strong>## sad path 1</strong></pre><pre>* greet</pre><pre>- utter_greet</pre><pre>* mood_unhappy</pre><pre>- utter_cheer_up_images</pre><pre>- utter_are_you_happy</pre><pre>* affirm</pre><pre>- utter_happy</pre><pre><strong>## sad path 2</strong></pre><pre>* greet</pre><pre>- utter_greet</pre><pre>* mood_unhappy</pre><pre>- utter_cheer_up_jokes</pre><pre>- utter_are_you_happy</pre><pre>* deny</pre><pre>- utter_cheer_up_images</pre><pre>- utter_are_you_happy</pre><pre>* affirm</pre><pre>- utter_happy</pre><pre><strong>## sad path 3</strong></pre><pre>* greet</pre><pre>- utter_greet</pre><pre>* mood_unhappy</pre><pre>- utter_cheer_up_jokes</pre><pre>- utter_are_you_happy</pre><pre>* deny</pre><pre>- utter_cheer_up_images</pre><pre>- utter_are_you_happy</pre><pre>* deny</pre><pre>- utter_sad</pre><pre><strong>## say goodbye</strong></pre><pre>* goodbye</pre><pre>- utter_goodbye</pre><p>3.<strong> Train Daisy with the newly added data:</strong></p><p>Whenever we add new training data, we need to re-train a neural network on our example stories and NLU data and generate a new model.</p><pre>$ rasa train</pre><p>4. <strong>Test Daisys’ new capabilities:</strong></p><p>Let’s test our chatbot and see if it can detect if the user’s mood is unhappy and respond based on that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*ESNS2WNe96iBBhUExPeVUQ.png" /></figure><p>Awesome! our chatbot Daisy can detect that the user was unhappy and respond with a joke to cheer him up. In this article, we have built a chatbot with very basic capabilities, but the great thing about Rasa is that it is completely open-source and you tweak it according to your own needs. You can add support for any language that is not supported out of the box by providing your custom tokenizer and customizing the NLU pipeline. You can use word vectors from any source you want or even build your own word vectors if you want your chatbot to understand your particular domain knowledge. You can execute python code and call external services to execute business codes to respond to a user. With Open Source Rasa the possibilities are limitless.</p><p>Feel free to check out our <a href="https://medium.com/monstar-lab-bangladesh-engineering">company blog</a> on medium to read more tech articles. You can visit our <a href="http://www.monstar-lab.co.bd/">website</a> to learn more about us. Till then, cheerio!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d4ad3b79cd4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/writing-conversational-chatbots-using-rasa-3d4ad3b79cd4">Writing Conversational Chatbots Using Rasa</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Conversational Chatbots — The Basics]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/writing-conversational-chatbots-the-basics-925db6d1af1e?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/925db6d1af1e</guid>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[rasa]]></category>
            <category><![CDATA[language-learning]]></category>
            <dc:creator><![CDATA[Shahnewaz Leon]]></dc:creator>
            <pubDate>Thu, 30 Jan 2020 09:50:11 GMT</pubDate>
            <atom:updated>2020-01-30T09:50:10.982Z</atom:updated>
            <content:encoded><![CDATA[<h3>Writing Conversational Chatbots — The Basics</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gu_M6mjK3qz_WQQa" /><figcaption>Photo by <a href="https://unsplash.com/@lunarts?utm_source=medium&amp;utm_medium=referral">Volodymyr Hryshchenko</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>“<strong>By 2022, 70% of white-collar workers will interact with conversational platforms on a daily basis</strong>” — Gartner</p><h4>What exactly is a conversational chatbot?</h4><p>A conversational chatbot is an AI-powered software with a text or voice interface that tries to mimic an intelligent being while interacting with the users. Rather than scrolling, clicking and filling out forms, conversation in our day to day language is the most natural way of interacting and AI-powered conversational chatbots are the most natural steps towards fulfilling that potential.</p><h4>What are the value additions?</h4><p>The two most important value additions of AI chatbots are 1. The most natural way of interaction for your customers and 2. Saving time and effort for both the business and the customer. In it’s most simple use case a conversational chatbot can answer the most frequent questions asked by the users saving them the hassle of foraging through your company website to find the relevant information to the most complicated solution where the chatbot can combine diverse knowledge-base and answer the questions tailored for that particular user. The following image from Gartner sums up beautifully how a chatbot can save you time and effort.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rNc-uwmOUKF27zN61JSGmA.png" /><figcaption>Chatbots can make users&#39; life easy! —<a href="https://www.gartner.com/smarterwithgartner/chatbots-will-appeal-to-modern-workers/"> Gartner</a></figcaption></figure><h4>Writing your own chatbot — the prerequisites</h4><p>A chatbot can tremendously help your business and your user, however, the promise of a chatbot is it’s Achilles heel. For a chatbot to be useful, it has to have the ability to understand what a user is saying and intelligently carry on the conversation. Obviously it may not sound like such a daunting task for us, after all, we have been doing this since we learn to talk. But, don’t forget to appreciate what a challenging feat this is and in the next sections hopefully, I’ll be able to give you an idea of just what it involves in writing an intelligent chatbot. Fortunately for us, in recent years, there has been tremendous advancement in the fields of Machine Learning and Artificial Intelligence which makes the job of writing your own chatbot comparatively easier.</p><h4>NLP concepts you need to know</h4><p>At the heart of it is understanding and being able to respond using natural language and the branch of Machine Learning that deals with this is Natural Language Processing. It sounds like a mouthful, but we are going to break it down into the parts we need in order to build a simple chatbot. NLP sits at the intersection of computer science, AI, ML, and computational linguistics. It provides concepts, tools, and algorithms for computers to analyze, understand, and form responses in a smart and useful way. We can use NLP to perform tasks involving natural language such as text summarization, translation, sentiment analysis, topic modeling, named entity extraction.</p><h4>Getting your text ready for NLP — text pre-processing</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qE91iTJwWy2gRswl" /><figcaption>Photo by <a href="https://unsplash.com/@toddquackenbush?utm_source=medium&amp;utm_medium=referral">Todd Quackenbush</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The first step involved in any type of NLP task is pre-processing the text data to feed into the NLP algorithms. The most commonly used pre-processing steps are Tokenization, Removing noise and stop words, Stemming, Lemmatization.</p><p><strong>Tokenization </strong>is the process of converting the input text into tokens i.e. words or sentences for further processing. The actual tokenization task depends on the text language. It can be as easy as splitting the text using whitespace characters for languages where words are separated using whitespace or as hard as writing a special algorithm by following the language rules of sentence formation where more complicated sentence making rule is used such as Japanese or Burmese.</p><p>The next step is <strong>removing noise and </strong><a href="https://algs4.cs.princeton.edu/35applications/stopwords.txt"><strong>stop words</strong></a>. Noise is anything that doesn’t help us understand the text, such as punctuation marks, emoticons, etc. There are some extremely common words or filler words which do not carry any special meaning and do not help us understand the text any better are also removed.</p><p><strong>Stemming </strong>is the process of reducing inflected words to the base form so as not to treat the different variations of the same word as different words. For example, we understand that the words ‘walking’, ‘walks’, ‘walked’ derives from the same root word ‘walk’. In this step, we reduce all these words to the root word ‘walk’.</p><p><strong>Lemmatization </strong>is an improved normalization technique that is closely related to stemming. The basic difference is that Stemming uses a crude heuristic process by simply chopping off the tails of words in the hope of reaching the base word form in the majority of the cases. Whereas, in Lemmatization we use vocabulary and morphological analysis of words keeping the context in mind. For example, ‘better’ is a derived version of the word ‘good’. Without the knowledge of the vocabulary and morphological analysis of the word, it is not possible to get to the base word ‘good’. So, in this particular case Stemming will not be successful in reducing the word. However, such cases are rare and Stemming might be the most favorable technique given the accuracy-complexity-execution time tradeoff in most cases.</p><p>Our text is now conveniently broken down into tokens, we can focus on converting it into a form which the machine can understand and can be fed into NLP algorithms. We need to represent the words in such a form which is convenient for NLP algorithms. The <a href="https://en.wikipedia.org/wiki/Bag-of-words_model">Bag of Words</a>(BoW) in one such very simple technique which is a representation of text that describes the occurrence of words within a document. BoW doesn’t preserve the ordering of the words and it also ignores the semantics of the words. There are some better alternatives that represent words as vectors (aka. <a href="https://code.google.com/p/word2vec/">word2vec</a>) and understand the semantics of the words as well.</p><p>Now that we understand the basics of what goes into building a chatbot, let’s delve into actually writing a chatbot of our own. In the next article, we will build an AI-powered chatbot using the open-source conversational AI framework <a href="https://rasa.com/docs/rasa/">Rasa</a>.</p><p>Feel free to check out our <a href="https://medium.com/monstar-lab-bangladesh-engineering">company blog</a> on medium to read more tech articles. You can visit our <a href="http://www.monstar-lab.co.bd/">website</a> to learn more about us. Till then, cheerio!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=925db6d1af1e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/writing-conversational-chatbots-the-basics-925db6d1af1e">Writing Conversational Chatbots — The Basics</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OperationQueue: Asynchronous Data Provider for list view]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/operationqueue-asynchronous-data-provider-for-list-view-ee5327ff2647?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/ee5327ff2647</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[uitableview]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Mohammed Rokon Uddin]]></dc:creator>
            <pubDate>Tue, 28 Jan 2020 04:58:31 GMT</pubDate>
            <atom:updated>2024-08-01T04:51:55.533Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Il9Ky0OcbLl-lnzzrbq5oA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@gcsgpp">Gabriel Gusmao</a> on <a href="https://unsplash.com/photos/pMmw3ynuXHw">Unsplash</a></figcaption></figure><p>In <a href="https://medium.com/monstar-lab-bangladesh-engineering/tableview-prefetching-datasource-3de593530c4a"><em>iOS TableView Prefetching DataSource using Swift</em></a><em>, </em>I talked about the prefetching API introduced<em> </em>in iOS 10. To achieve a seamless user experience, prefetching API does an excellent job. But at times we need more than prefetching datasource. One of those times is when the data we want to fetch are from multiple synchronous slow-running tasks depending on one another. Considering such cases, in this article, I explained how to group multiple heavy tasks (both sync and async) using Swift’s powerful multithreading API Operation and I called it an <strong>Asynchronous Data Provider.</strong></p><h4><strong>Prerequisite</strong></h4><p>As you are reading this article, I am hoping that you already know about GrandCentralDispatch (GCD), OpertionQueue and PrefetchingDataSource .</p><h4>Getting Started</h4><p>The sample project of this article populates a table view of filtered images. These images are the results of four heavy loading tasks, both synchronous and asynchronous. I made each task as a subclass of Operation to run these tasks in a OperationGroup and assign one as a dependency of another task.</p><h4>DataLoadOperation:</h4><p>This is the first of four tasks used to fetch images. It downloads the metadata for each image from the internet. Here we used NetworkSimulator to mimic the delay due to network call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce4b9f60a59bf5dfc2fbbab4b47e3d3f/href">https://medium.com/media/ce4b9f60a59bf5dfc2fbbab4b47e3d3f/href</a></iframe><h4>ImageDecompressionOperation:</h4><p>As the name says, this decompresses the compressed data downloaded in DataLoadOperation to UIImage</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b30ab5619988076d16b6d246ea1d622e/href">https://medium.com/media/b30ab5619988076d16b6d246ea1d622e/href</a></iframe><h4><strong>TiltShiftOperation:</strong></h4><p>The responsibility of this class is to apply a gradient with a corner radius to the UIImage passed from ImageDecompressionOperation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6eb2a8f049831a9bc0054438b09c3db9/href">https://medium.com/media/6eb2a8f049831a9bc0054438b09c3db9/href</a></iframe><h4><strong>ImageFilterOutputOperation:</strong></h4><p>This calls the completion block to return the final output.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bbd8136ea835c2f5cd914c20088a957/href">https://medium.com/media/1bbd8136ea835c2f5cd914c20088a957/href</a></iframe><h4><strong>TiltShiftImageProvider:</strong></h4><p>This is where all the magic happens. TiltShiftImageProvider. It initializes all tasks, assigns the dependencies to one other accordingly, and in the end adds this array of tasks to OperationQueue. TiltShiftImageProvider just has a single method cancel() which is called whenever we want OperationQueue to stop any ongoing task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9e01347f3aa3ef0d59eb6d21cc8d380/href">https://medium.com/media/c9e01347f3aa3ef0d59eb6d21cc8d380/href</a></iframe><h4><strong>TiltShiftTableViewController:</strong></h4><p>Inside tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) a new imageProvider is initialized and stored in a dictionary. tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) method calls configureCell(_ cell: ImageTableViewCell, at indexPath: IndexPath) to update UI If there is an image available an indexPath or else a new imageProvider has been created and update the cell as soon as the image has been returned from the completion block and finally imageProvider has been stored in the dictionary. In tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) and tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) methods we called cancel() method of each stored imageProvider to stop any ongoing task and then imageProvider has been removed from the dictionary to free up the resources.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c259be5c40f9caa574a29fc6caf737f0/href">https://medium.com/media/c259be5c40f9caa574a29fc6caf737f0/href</a></iframe><h3>Conclusion</h3><p>Operations in Swift are a very powerful way to segregate tasks into multiple classes. As OperationQueue the class is key-value coding (KVC) and key-value observing (KVO) compliant, it keeps track of the progress and dependencies of each task. The below code repo is a perfect example of how to leverage the power of OperationQueue and design an Asynchronous Data Provider.</p><p><a href="https://github.com/rokonuddin/AsyncDataProvider">rokonuddin/AsyncDataProvider</a></p><p>Thank you all for your attention 🙏🏻. feel free to <a href="https://twitter.com/RokonMohammed">tweet</a> and get <a href="https://www.linkedin.com/in/rokonuddin/">connected</a>.</p><p>Check out other articles from our engineering team:<br><a href="https://medium.com/monstar-lab-bangladesh-engineering">https://medium.com/monstar-lab-bangladesh-engineering</a></p><p>Visit our website to learn more about us:<br><a href="http://www.monstar-lab.co.bd/">www.monstar-lab.co.bd</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee5327ff2647" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/operationqueue-asynchronous-data-provider-for-list-view-ee5327ff2647">OperationQueue: Asynchronous Data Provider for list view</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reflecting on Java reflection]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/reflecting-on-java-reflection-411e8e580743?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/411e8e580743</guid>
            <category><![CDATA[java-reflection]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Rafi Sameen]]></dc:creator>
            <pubDate>Wed, 13 Nov 2019 04:53:36 GMT</pubDate>
            <atom:updated>2019-11-13T04:53:36.377Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reflecting on Java Reflection</h3><p><strong>What is Java Reflection?</strong></p><p>You might have heard about Java Reflection. I used to think that it was complex but later found it to be far simpler and awesome than I had thought. Let’s dive into it.</p><p>When Java code is compiled, it is transformed into Java bytecode. As you may already know, JVM (Java Virtual Machine) executes these bytecodes. Sometimes during this execution, some classes, interfaces, fields or methods need to be tested or changed. To serve those changes there are some APIs named Java Reflection API.</p><p>By using this API you may make changes in code during runtime by not even knowing the name of the class name, field name or method name during compile time. You will be even accessing private fields or methods using this API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lyQnEnBdnl3r2W2wHnsv0g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@omgitsyeshi?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><strong>Yeshi</strong> <strong>Kangrang</strong></a> on <a href="https://unsplash.com/s/photos/reflection?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText"><strong>Unsplash</strong></a></figcaption></figure><p>Spring Framework, Hibernate, Project Lombok and many other frameworks and libraries are generating code depending on annotation which makes life easier. This code generation is done by using the Java Reflection API. In this post, I will be discussing various classes of java.lang.reflect package and just tinke<strong>r with those a bit.</strong></p><p><strong>Example</strong>:</p><pre>URL url = <strong>new </strong>URL(<strong>&quot;https://monstar-lab.com/global/&quot;</strong>);<br>String protocol = url.getProtocol();<br><br>System.<strong><em>out</em></strong>.println(protocol);</pre><p>From the snippet above we can see that I have just made an instance of class URL and called method getProtocol() and printed the output. Now we can see that the decision of which method to call was taken at the compile time. Let’s do the same thing with Reflection API.</p><pre>Class&lt;?&gt; type = Class.<em>forName</em>(<strong>&quot;java.net.URL&quot;</strong>);<br>Constructor&lt;?&gt; constructor = type.getConstructor(String.<strong>class</strong>);<br>Method method = type.getMethod(<strong>&quot;getProtocol&quot;</strong>);<br><br>Object instance = constructor.newInstance(<strong>&quot;https://monstar-lab.com/global/&quot;</strong>);<br><br>Object methodCallResult = method.invoke(instance);<br>System.<strong><em>out</em></strong>.println(methodCallResult);</pre><p>Now in the snippet above you can see a bit more complex code than the previous one but will be giving you the same result. The interesting part is, in the compile time we did not decide which method to call. We left it to be decided on runtime.</p><p>JVM makes a java.lang.Class type instance for all the types which is a runtime representation. In the code snippet above I took the reference of a Class object of java.net.URL. java.net.URL class has six constructors. I used the constructor which takes a string argument. Then took a reference of the method which we want to call. Then made a new instance with the help of a method provided by java reflection. At the end invoked it and printed our desired result. This obviously looks complex but it demonstrates how the reflection works. We are making decisions at the runtime which is the key here.</p><h4>Use Cases:</h4><p>Let’s take a reference of java.lang.Class object. Which needs to be done always in order work with reflection API.</p><pre>Class studentClass = student.getClass();</pre><p>Using this reference we can access different names, fields, methods or constructors. For example:</p><pre>String name = studentClass.getName();</pre><p>Here getName() method returns the fully qualified name with package name. In order to get the public fields of this class getFields() method needs to be called which returns an array. If you want to get the private fields then getDeclaredFields() method needs to be called. Here is an example:</p><pre>Fields[] fields = studentClass.getDeclaredFields();<br><strong>for</strong>(Field field : fields) {<br>  System.<strong><em>out</em></strong>.println(field.getName());<br>}</pre><p>Like the above examples if you want to know the package of the class then use getPackage() method. For getting the methods use getMethods() . Similarly getConstructors() , getAnnotations() , getInterfaces() , getSuperclass() methods are also available. I guess you got the idea.</p><h4>Calling private methods</h4><p>You may also call a private method of a class using reflection API. Let’s see an example:</p><pre><strong>class </strong>StudentService {<br>  <strong>private void </strong>doSomethingPrivate(String args) {<br>    System.<strong><em>out</em></strong>.println(<strong>&quot;argument: &quot; </strong>+ args);<br>  }<br>}</pre><p>Here is a private method in StudentService class. You will not have any access directly to this method. Now let’s see another code snippet:</p><pre><strong>public class </strong>ExampleAccessingPrivateMethod {<br>  <strong>public static void </strong>main(String[] args) {<br>    <strong>try </strong>{<br>      StudentService studentService = <strong>new </strong>StudentService();<br>      Class&lt;StudentService&gt; studentServiceClass = <br>        StudentService.<strong>class</strong>;<br><br>      Method doSomethingPrivate =<br>        studentServiceClass.getDeclaredMethod(<br>        <strong>&quot;doSomethingPrivate&quot;</strong>, String.<strong>class</strong>);</pre><pre>      doSomethingPrivate.setAccessible(<strong>true</strong>);<br>      doSomethingPrivate.invoke( studentService,<br>             <strong>&quot;hello students! Let&#39;s bunk the class today&quot;</strong>);</pre><pre>    } <strong>catch </strong>(IllegalAccessException <br>             | InvocationTargetException <br>             |  NoSuchMethodException e) {<br>      e.printStackTrace();<br>    }<br>  }<br>}</pre><p>Here a studentService instance is made and object is referred. Through this reference the private method which we want to access is also taken reference of. And in order to access that particular private method, getDeclaredMethod() method is called. You can see setAccessible() method is called to set access right to this method and finally it was invoked and passed the parameter we wanted to pass. You can also dynamically load or reload a class. Not getting into that today.</p><h4>Summary</h4><p>Reflection has a lot uses. Using this particular feature, database connections or transaction management frameworks and APIs are written. It is used for creating mock objects in unit testing. Dependency injection or AOP (Aspect Oriented Programming) is also done using the reflection API. But let’s be clear on one thing, reflection is not for everything. You need to use it with balance. It is more like Opium. If used properly it will be very useful otherwise it will give you a hard time.</p><p>A lot of examples on the internet are actually available on this topic. You can easily search on google and go through those and have deeper understanding. Thanks for going through the blog. Hope it helped you.</p><ul><li><a href="https://medium.com/@rafisameen">Rafi Sameen - Medium</a></li><li><a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a></li></ul><p>Visit our website to learn more about us:<br><a href="http://www.monstar-lab.co.bd/">www.monstar-lab.co.bd</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=411e8e580743" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/reflecting-on-java-reflection-411e8e580743">Reflecting on Java reflection</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular Element: How to use Angular components in React Vue Js ?]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/angular-element-how-to-use-angular-components-in-react-vue-js-93757c92357b?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/93757c92357b</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[micro-frontends]]></category>
            <category><![CDATA[web-components]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[S M Asad Rahman]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 07:03:49 GMT</pubDate>
            <atom:updated>2019-11-12T16:01:33.042Z</atom:updated>
            <content:encoded><![CDATA[<h3>Angular Element: Use Angular components outside the Angular</h3><figure><img alt="Photo by Esteban Lopez on Unsplash" src="https://cdn-images-1.medium.com/max/1024/1*B2vxcoeYcFr7dGnXp8loSA.jpeg" /></figure><h3>Teaser:</h3><p>We all are familiar with <em>components:</em> the most powerful building block in the Angular ecosystem. Components are where the Dom gets rendered, services get injected, views get re-rendered with change detection. Components can take values as inputs and trigger events as outputs. But the problem is, this beautiful block so far works only inside an Angular ecosystem. <strong>I wish we could help humanity by using angular components even outside the angular ecosystem.</strong></p><p><strong>Yes! From Angular 7, Angular brings the feature “Angular Element”. Now an angular component can be exported as a standalone web component. We can use it exactly like a typical HTML tag like div or span, in any HTML page, CMS or even inside a React or Vue.js project.</strong></p><h3>How it works:</h3><figure><img alt="photo: https://angular.io/guide/elements" src="https://cdn-images-1.medium.com/max/600/1*wI2wCqw0DIRvTzWqs2lDeQ.png" /></figure><p>Web Component is a web standard to define a new custom HTML element in the browser. Browsers maintain a registry called <a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry"><em>customElementRegistry</em></a> . Using the method <a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define">CustomElementRegistry.define()</a> we can register a “dom string” and a class that defines its behavior.</p><p>Angular’s <a href="https://angular.io/guide/elements">@angular/elements</a> package exposes the method <a href="https://angular.io/api/elements/createCustomElement">createCustomElement</a> which converts angular component and its dependencies as a class compatible as a web component.</p><p>Currently, web standard is supported by All Chrome, Firefox, Safari and Opera browsers. We can also make it workable in other browsers using polyfills.</p><h3>Use Cases:</h3><p>There are at least 3 use cases where we can make use of exportable fully functional angular components as web components:</p><ul><li>Micro front end</li><li>Legacy code migration</li><li>Plug &amp; play web components for third-party services</li></ul><h4>Micro front end:</h4><p>Like micro-service, there is another similar concept called micro-frontend. In a micro-frontend architecture, a small full-stack team is responsible to deliver not only the APIs but also their corresponding front-end components. For example, the “product team” will create product APIs and all product-related views used in different category pages, product detail page, cart &amp; checkout page. In a case like this, all team will provide different web components and one front end team will only glue those components together in different pages.</p><h4>Migration:</h4><p>Projects written in Angular 1.x, Migration to Angular always has been a challenge. There is no easy magic spell-like <strong>ng update</strong>. The same is true for a case where someone wants to migrate their project to Angular from React or Vue. Web components is the best fit here. They can gradually convert their existing components/controllers to Angular web components and replace them from the existing system. Once all components/controllers are converted they can just replace the legacy system with the latest angular.</p><h4>Third-party services:</h4><p>Service providers can provide plug &amp; play web components to be glued in their client’s website without any further engineering needed.</p><p>So seems it is promising if we can actually ship an angular component as a web component. So let’s make our hands dirty with the actual coding.</p><h3>Example project:</h3><p>Here is an example code we write a <em>my-todo-list</em> web component. <strong>FYI you can check the entire code here in </strong><a href="https://github.com/asad-mlbd/example-ng-element"><strong>Github</strong></a><strong>.</strong></p><h4>Step 1: Write the component and service</h4><ul><li>A TodoList component to show a list of todos.</li><li>A TodoService to fetch todo’s from placeholder JSON API.</li><li>The TodoList component will take “name” as input.</li><li>TodoList component will trigger the onTodoComplete event when any todo will be marked as done.</li></ul><figure><img alt="todo-list.component.ts" src="https://cdn-images-1.medium.com/max/1024/1*xKrZU3K8G-wxmWVP6QWEEA.png" /><figcaption><a href="https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/todo-list/todo-list.component.ts">https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/todo-list/todo-list.component.ts</a></figcaption></figure><figure><img alt="todo.service.ts" src="https://cdn-images-1.medium.com/max/1024/1*Ic8Q-hLSkD10AQb8WmRPYQ.png" /><figcaption><a href="https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/todo-list/todo.service.ts">https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/todo-list/todo.service.ts</a></figcaption></figure><h4>Step 2: Tweak app.modules to export todo-list component</h4><ul><li>Since the project will be used only to export angular modules as a web component we do not need bootstrapping through the app component.</li><li>We have to declare our exportable todo-list component as entry components.</li><li>Install @angular/elements package.</li><li>Inside <strong>ngDoBootstrap </strong>hook we use <strong>createCustomElement </strong>method<strong> </strong>to compile our TodoList component as a standard web component.</li><li>TodoList component has dependency on TodoService. Since it will be used outside the angular ecosystem, we need to pass the Dependency Injector manually so that it can load dependencies.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AnKdMCe7rB9YMHtMJLz7ng.png" /><figcaption><a href="https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/app.module.ts">https://github.com/asad-mlbd/example-ng-element/blob/master/src/app/app.module.ts</a></figcaption></figure><p><strong>Step 3: Bundling</strong></p><ul><li>Now if we run build — files will be generated in “dist” folder. Since we want to use this in another ecosystem better to concat the files as one output file. So add these commands in package.json script section:-</li></ul><pre><br>&quot;concat-es5&quot;: &quot;concat -o output/output-es5.js ./dist/example-ng-element/runtime-es5.js ./dist/example-ng-element/polyfills-es5.js ./dist/example-ng-element/scripts.js ./dist/example-ng-element/main-es5.js&quot;,</pre><pre>&quot;concat-es2015&quot;: &quot;concat -o output/output-es2015.js ./dist/example-ng-element/runtime-es2015.js ./dist/example-ng-element/polyfills-es2015.js ./dist/example-ng-element/scripts.js ./dist/example-ng-element/main-es2015.js&quot;,</pre><pre>&quot;concat&quot;: &quot;npm run concat-es5 &amp;&amp; npm run concat-es2015&quot;,</pre><pre>&quot;build&quot;: &quot;ng build --prod --output-hashing=none &amp;&amp; npm run concat&quot;,</pre><ul><li>Now if we run the command <strong>npm run build </strong>two separate files <strong>output-es5.js</strong> and <strong>output-es2015.js</strong> will be generated inside the <strong>output </strong>folder<strong>.</strong></li></ul><h4>Step 4: Use as a web component</h4><ul><li>Now all is set. We can use these output files as our standalone web component like this:-</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VyBH5rpD0JsZACDMRO7MQQ.png" /><figcaption><a href="https://github.com/asad-mlbd/example-ng-element/blob/master/output/index.html">https://github.com/asad-mlbd/example-ng-element/blob/master/output/index.html</a></figcaption></figure><ul><li>If we run this HTML page, we can see the todo list will be fetched and showed.</li><li>We passed the name as input, we can see the name inside the web component — which means component’s input, change detection are working!</li><li>Once we check/uncheck any todo, event-triggered and todo data will be logged in the console. So the output event is working as well!</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QMOiQaHUvESmJI_xVwo6NQ.png" /></figure><p>So, like this, we can use an angular component in any other project. You can try yourself cloning the code from <a href="https://github.com/asad-mlbd/example-ng-element"><strong>Github</strong></a><strong>.</strong></p><p>Ref:</p><ul><li><a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements">https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry">https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry</a></li><li><a href="https://angular.io/guide/elements">https://angular.io/guide/elements</a></li></ul><p>Feel free to make your comment here or catch me on twitter <a href="https://twitter.com/asad_rahman">@asad_rahman</a>.</p><p>Visit our website to learn more about us:<br><a href="http://www.monstar-lab.co.bd/">www.monstar-lab.co.bd</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93757c92357b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/angular-element-how-to-use-angular-components-in-react-vue-js-93757c92357b">Angular Element: How to use Angular components in React Vue Js ?</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NestJS, Your Angular Window to Server-side Programming]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/nestjs-your-angular-window-to-server-side-programming-95611657ac1d?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/95611657ac1d</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[nestjs]]></category>
            <dc:creator><![CDATA[Saad Bin Amjad]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 04:29:33 GMT</pubDate>
            <atom:updated>2019-11-04T04:34:40.046Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GM5c_IKy-duICGy42EQp7w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@gaellemarcel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Gaelle Marcel</a> on <a href="https://unsplash.com/s/photos/cat-window?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The role of a software developer is getting more open-ended with time. They are more of a “Fire Fighter Developer” if not anything else. Developers need to be constantly vigilant, doing more than what they are asked for, especially when some teams need help, they are required to step in to save the day. We look into a Node.js framework using Angular lens in this article, which will serve as a perfect gateway for frontend developers to dive into the server-side programming with existing knowledge sets.</p><p>The framework is not so new given the fast adoption rate of the framework. NestJS is a platform that Angular community is proud to have, and NodeJS community was looking for in terms of highly opinionated architecture.</p><p>Node.js, born in 2009, is a runtime environment based on Chrome’s V8 JavaScript engine, and it kept growing in interest in the developer community ever since. There are several Node.js frameworks like Express.js, Meteor, Koa.js, etc. But they somehow lacked a solution to an ever-increasing problem. How can we create backend applications in JS, where large teams can work, build to scale and maintain it with time?</p><p><a href="https://twitter.com/kammysliwiec"><strong>Kamil Mysliwiec</strong></a><strong> </strong>had an answer when he introduced NestJS, the fastest-growing Node.js framework of 2018.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*13MEu5Aep3U99KhwoAKYhg.png" /></figure><p>Before we introduce NestJS, let’s look at the key takeaways of Angular a bit. If you are working in a large team, opinionated frameworks are way to go. The code becomes consistent and easily maintainable. Angular being modular, import and exporting modules allow components to be re-used in other applications, saving much more development time. Furthermore, since Angular has an impressive DI mechanism, almost all of the good programming practices are by default adopted while coding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ClSPcALQngykvEJVAIClw.png" /></figure><p><strong>NestJS Takeaways</strong></p><p>Similarly, NestJS have this Angular philosophy out of the box. Let’s run down that same list for Nest:</p><p><strong>Platform and Framework</strong></p><p>NestJS is more than a framework, as it gives developers more options outside the box, like abstractions for Microservices, WebSockets, GraphQL, etc. to name a few.</p><p><strong>Typescript</strong></p><p>Uses Typescript, which as we know is the Javascript that scales. Programming thus can be done just right with any or all of them, Object Oriented Programming, Functional Programming, and Functional Reactive Programming. But if you love vanilla JS, you can still use it.</p><p><strong>Opinionated</strong></p><p>Nest provides a set of guidelines, as to how we should use the framework, but never limits us to work with the underlying APIs like that of Express, or Fastify. Opinionated means the code is much more consistent across large teams but doesn’t necessarily imply that you can’t be creative while working with abstractions.</p><p><strong>Modular</strong></p><p>That means it is domain driven, each module holding a set of their own set of business logic. The modules are singletons by default, but we can still share the same instance of any provider between multiple modules effortlessly in Nest.</p><p><strong>Controllers</strong></p><p>Nest does not have any components like that of Angular. Instead, they have controllers that accept a request and sends a response, the core idea behind Server Side Programming.</p><p><strong>Dependency Injection</strong></p><p>Nest heavily uses decorator functions. Decorator functions provide metadata to the compiler during runtime so to ensure each dependency is resolved while building the application. Decorators ensure that SOLID principles are ensured throughout the application.</p><p>I have written in-depth about the beauty of DI, and IoC in Node.js frameworks, feel free to have a look here:</p><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/simplifying-dependency-injection-and-ioc-concepts-using-typescript-b70643f71c91">Simplifying Dependency Injection and IoC Concepts using TypeScript</a></p><p><strong>Providers</strong></p><p>Providers can be any values, class or factory that contains certain metadata related to various injection scopes. Dependency Injection is a core part of NestJS, with its own built-in IoC to resolve dependencies.</p><p><strong>CLI</strong></p><p>Life becomes easy when CLI helps you to scaffold, serve, build and bundle the application for you.</p><p>We can see that all the goodness of Angular is prevalent in NestJS. So why not we code some “Angular” in the backend? Disclaimer, even if you are not Angular developers, you can still easily pick up Nest as your backend framework, thanks to its well-crafted documentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mHqncFosaUaZN5-s30lO9A.png" /></figure><p>We will be building a few APIs for one entity. Let’s say we want to create a resource called Events, like tech community events. We will make some routes that handle requests, based on the HTTP verb, we then fetch/update data from a database, and return a response.</p><p>The full code, can be found here, but I will still go over few steps and highlight how the code was written.</p><p><a href="https://github.com/Saad-Amjad/ng-bd-demo">Saad-Amjad/ng-bd-demo</a></p><p>Install nestjs cli, we need to have <a href="https://nodejs.org/">Node.js</a> (&gt;= 8.9.0):</p><pre>npm i -g @nestjs/cli</pre><p>Creating a new project, we will name it as ng-bd-demo:</p><pre>nest new ng-bd-demo</pre><p>We will see a project got scaffolded, and if you want to run the project:</p><pre>npm run start</pre><p>While development, you want the changes to be reflected, then the command is:</p><pre>npm run start:dev</pre><p>If you explore the folder structure, it will look as a familiar territory like that of Angular. Let’s focus on the main.ts file that kicks of the entire application. The bootstrap code looks like this:</p><pre>async function bootstrap() {<br>  const app = await NestFactory.create(AppModule); <br>  await app.listen(3000);<br>}<br>bootstrap();</pre><p>Let’s create a module called events:</p><pre>nest g module events</pre><p>Create a controller called events:</p><pre>nest g controller events</pre><p>Create a service called events:</p><pre>nest g service events</pre><p>These commands will ensure that AppModule contains EventsModule, and EventsModule contains EventsController and EventsService declared as controllers and providers respectively.</p><p>We will try to create some functions that will enable us to have CRUD APIs for Events. For that, we need these decorators from <a href="http://twitter.com/nestjs/common">@nestjs/common</a>.</p><pre>import { Controller, Get, Post, Body, Param, Patch, Delete } from &#39;@nestjs/common&#39;; </pre><p>For start, let’s return a simple string for a GET index() method call:</p><pre>@Get()<br>index(): string {<br>  return &#39;Events list&#39;;<br>}</pre><p>If we run the server now, and hit GET events/ API, it will return ‘Events list’. Before we complete the other routes, let’s first make our application talk to a database. We will be using MySQL, you can choose any since Nest is database agnostic.</p><p>Nest provides TypeORM, Object Relational Mapper (ORM) for TypeScript, package out of the box, which can be installed using this command.</p><pre>npm install --save @nestjs/typeorm typeorm mysql</pre><p>After install import the TypeORM module’s forRoot() method in your app.module, and in events.module import it with TypeORM module forFeature() method.</p><p>forRoot method accepts configurations of your database connections, and forFeature() method defines the repositories for that module which imports it.</p><p>Now we create an Event entity that we will import in EventsModule like this: TypeOrmModule.forFeature([Event]) .</p><pre>import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, Timestamp, UpdateDateColumn } from &#39;typeorm&#39;;</pre><pre>@Entity()<br>export class Event {<br> <br>  @PrimaryGeneratedColumn()<br>  id: number;</pre><pre>  @Column()<br>  name: string;</pre><pre>  @Column()<br>  description: string;</pre><pre>  @CreateDateColumn({ nullable: true })<br>  created_at: Timestamp;</pre><pre>  @UpdateDateColumn({ nullable: true })<br>  updated_at: Timestamp;</pre><pre>}</pre><p>We will be using Dependency Injection in our service to use this entity in our repository.</p><pre>@Injectable()<br>export class EventsService {</pre><pre>  constructor(@InjectRepository(Event)<br>    private readonly eventRepository: Repository&lt;Event&gt;) { }</pre><pre>  async findAll(): Promise&lt;Event[]&gt; {<br>    return await this.eventRepository.find();<br>  }</pre><pre>  async find(id: number): Promise&lt;Event&gt; { <br>   return await this.eventRepository.findOne(id);<br>  }</pre><pre>}</pre><p>We are injecting the repository in the service class and the findAll() method will call the database for all the events asynchronously. You might wonder how will this entity be used to migrate a real table in the database.</p><p>When we provided configs in forRoot()method, we explicitly told TypeORM to synchronize the entities, i.e. run auto migrations for us, using the array values provided in the entities options.</p><pre>TypeOrmModule.forRoot({<br>  type: &#39;mysql&#39;,<br>  host: &#39;localhost&#39;, <br>  port: 3306, <br>  username: ‘username’, <br>  password: ‘password’,<br>  database: ‘database&#39;,<br>  entities: [Event], <br>  synchronize: true,<br>})</pre><p>The controller GET index will finally look like the following. We can now also use Param() to retrieve URL query params.</p><pre>constructor(private eventService: EventsService) { }</pre><pre>  @Get()<br>    index(): Promise&lt;Event[]&gt; {<br>    return this.eventService.findAll();<br>  }</pre><pre>  @Get(&#39;:id&#39;) <br>  show(@Param(&#39;id&#39;) id: number): Promise&lt;Event&gt; { <br>    return this.eventService.find(id);<br>  }</pre><p>But for POST, instead of query params, we need to get the payload from the body of the request. To ensure the payloads are consistent for a specific API, we will now be creating a Data Transfer Object (DTO).</p><pre>import { IsString, IsNotEmpty } from &#39;class-validator&#39;;</pre><pre>export class CreateEventDto {</pre><pre>  @IsString()<br>  @IsNotEmpty()<br>  readonly name: string;</pre><pre>  @IsString()<br>  @IsNotEmpty()<br>  readonly description: string;</pre><pre>}</pre><p>We installed ‘class-validator’ to give validation rules to the request body.</p><pre>npm i --save class-validator class-transformer</pre><p>POST store()would now look like this:</p><pre>@Post()<br>store(@Body() createEventDto: CreateEventDto): Promise&lt;Event&gt; { <br>    return this.eventService.create(createEventDto);<br>}</pre><p>and PATCH update() and DELETE delete() like this:</p><pre><br>@Patch(&#39;:id&#39;)<br>update(<br>  @Param(&#39;id&#39;) id: number, <br>  @Body() updateEventDto: UpdateEventDto): Promise&lt;Event&gt; { <br>  return this.eventService.update(id, updateEventDto);<br>}</pre><pre>@Delete(&#39;:id&#39;)<br>delete(@Param() id: number): Promise&lt;DeleteResult&gt; {<br>  return this.eventService.delete(id);<br>}</pre><p>The service now should have the methods to implement these API calls to our project, and then send it back to the appropriate client.</p><pre>async create(createPayload: CreateEventDto): Promise&lt;Event&gt; {<br>  return await this.eventRepository.save(createPayload);<br>}</pre><pre>async update(id: number, <br>  updatePayload: UpdateEventDto):     Promise&lt;Event&gt; {<br>    const event = await this.eventRepository.findOne(id);<br>    const data = Object.assign(event, updatePayload);<br>    return await this.eventRepository.save(data);<br>}</pre><pre>async delete(id: number): Promise&lt;DeleteResult&gt; {<br>  return await this.eventRepository.delete(id);<br>}</pre><p>We can either use Postman or Swagger to manipulate the APIs. For Swagger you can run this to install it in the application:</p><p>npm install --save @nestjs/swagger swagger-ui-express</p><p>Then in main.tswe bootstrap the Swagger module:</p><pre>async function bootstrap() {</pre><pre>  const app = await NestFactory.create(AppModule);                </pre><pre>  app.useGlobalPipes(new ValidationPipe()); </pre><pre>  const options = new DocumentBuilder()<br>  .setTitle(&#39;ng-bd-demo&#39;) <br>  .setDescription(&#39;Application that showcases backend in Angular&#39;)<br>  .build();</pre><pre>  const document = SwaggerModule.createDocument(app,       <br>    options);SwaggerModule.setup(&#39;api&#39;, app, document);<br>    await app.listen(3000);<br>}<br>bootstrap();</pre><p>If you run the server and go to /api, you can see Swagger documenting the list of APIs for you. We are done making 5 API calls, and pretty much that’s it for this introduction.</p><p>You import things you need, you create controllers, services, and try to send back a response to the client application. You can now export this module to any application you want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wc9RG4JV3xLVo2II2OiG8g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@yirage?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Andrii Podilnyk</a> on <a href="https://unsplash.com/s/photos/cat-nest?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I wanted to showcase how short the learning curve is for Angular developers to use NestJS. There are many more to Nest than what I described here, support for GraphQL Microservices, WebSockets, testing utilities are to name a few. Clean code, consistency, and maintenance of NodeJS based applications where you expect scaling and reusability, Nest should be your go-to platform.</p><p><a href="https://medium.com/@saadbinamjad">Saad Bin Amjad - Medium</a></p><p>Feel free to reach out to me on Twitter (<a href="https://twitter.com/saadbinamjad">@saadbinamjad</a>), and you can check some other articles posted by me on Medium and our <a href="https://medium.com/monstar-lab-bangladesh-engineering">Engineering Blog</a>. If you want to read more on some diversified topics, please checkout our company <a href="https://www.monstar-lab.co.bd/blog/">blog</a>. Thanks, till then happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95611657ac1d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/nestjs-your-angular-window-to-server-side-programming-95611657ac1d">NestJS, Your Angular Window to Server-side Programming</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simplifying Dependency Injection and IoC Concepts using TypeScript]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/simplifying-dependency-injection-and-ioc-concepts-using-typescript-b70643f71c91?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/b70643f71c91</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[inversion-of-control]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Saad Bin Amjad]]></dc:creator>
            <pubDate>Wed, 02 Oct 2019 10:55:16 GMT</pubDate>
            <atom:updated>2019-10-05T13:20:30.861Z</atom:updated>
            <content:encoded><![CDATA[<p>It is never easy to do everything by yourself. Since the beginning of time, humans truly understood, often with a huge cost, that their true power lies not in conflicts but in collaboration. Programming paradigm is also not quite different. For an application to live long, it must figure out the dependencies and try to look for ways to delegate others to serve the dependencies it needs. Not only this can solve its own problem efficiently, but it also, in turn, help other programs by presenting itself as their dependency. Dependency Injection is a crucial application design pattern for almost all the frameworks out there to create reusable, manageable and testable code. Today let’s try to simplify Dependency Injection, which is a subset of Inversion of Control principle, with TypeScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RY31Hnac3ZJ0l5LhCl371Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@fabmag?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Fabrizio Magoni</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>But first, let us set the table straight. If you have noticed any top class restaurant’s kitchen operation then you will find this article’s motivation a bit similar. In a restaurant, each Station Chef is responsible for running a specific section of the kitchen and they are being managed directly by the Head Chef or by the second-in-command Sous Chef. Station chefs can be in charge of different things respectively. A list of station chefs includes but is not limited to butcher chef, fish chef, grill chef, pantry chef, etc.</p><p>Whenever one order comes to the kitchen via a Caller, the Sous Chef simply doesn’t start preparing it all by himself. He runs down what are the things he needs to deliver, and start instructing them accordingly to each Station Chef. He expects they would handover their prepared items on a common table, where the entire dish can then be prepared or garnished for serving. This is an important takeaway that we will soon find out in this article but before that here is a rundown of the topics you will get introduced in this article.</p><pre><strong>-</strong> <strong>Dependency Injection<br>-</strong> <strong>Dependency Inversion Principle<br>-</strong> <strong>Inversion of Control<br>- Inversion of Control Container<br>- TypeScript Interfaces<br>- Decorator Functions<br>- Reflection APIs<br>- TypeScript IoC</strong></pre><p><strong>Problem Statement</strong></p><p>Let’s present a problem we are going to recreate and solve, called “Pizza making chronicles”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KUz-HMdX-Nu8qUOPFhKlyg.png" /><figcaption>I made this comic strip using <a href="https://www.pixton.com/">Pixton</a>. It is a cool app, do check out!</figcaption></figure><p>Pizza making got several dependencies to start with. Ignoring cheese and toppings (even sauce) for simplicity, we can see that pizza needs dough and dough needs flour and yeast. Flour and Yeast need water. Water needs salt. Here is a directed graph diagram that roughly shows these dependencies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*QgSxzHR9dxv0txgLnt8pgw.png" /><figcaption>The blue line shows the dependencies of dough.</figcaption></figure><p><strong>Dependency Injection</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ipgZByK6nvjPsazrA6NVVA.png" /></figure><blockquote><a href="https://laravel.com/docs/5.8/container"><em>Dependency injection is a fancy phrase that essentially means this: class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.</em></a></blockquote><p>Pizza needs dough to start with. Let’s see how does that look in a typical code base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8DzavUoqaK8allUWNn_xLA.png" /></figure><p>Soon we figure out that this Dough class is also required for making bread, so its redundant to allow both Pizza and Bread to be in charge of Dough all by themselves. So let’s delegate the creation of Dough to someone else. We inject the dependency Dough in Pizza constructor, an example of the most famous constructor based Dependency Injection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h_Yg5AvEApoG2WhB80ZBFA.png" /></figure><p>However, we passed a concrete implementation of Dough in Pizza, called DoughEntity. But customers might order different types of dough for different pizzas later.</p><p><strong>Dependency Inversion Principle</strong></p><blockquote>“<a href="https://books.google.com.bd/books?id=RaZfDwAAQBAJ&amp;pg=PA150&amp;dq=DIP+says+that+our+classes+should+depend+upon+abstractions,+not+on+concrete+details.&amp;hl=en&amp;sa=X&amp;ved=0ahUKEwiBie_3oPvkAhVJpY8KHWl9AyIQ6AEIKTAA#v=onepage&amp;q=DIP%20says%20that%20our%20classes%20should%20depend%20upon%20abstractions%2C%20not%20on%20concrete%20details.&amp;f=false">..DIP says that our classes should depend upon abstractions, not on concrete details.</a>” — Robert C. Martin</blockquote><p>Since dependency implementations can be swapped easily as they are injected during runtime, we rather inject an interface, like IDough so that we can later swap it with any concrete implementation of IDough, i.e DoughEntity like SourdoughDough, BriocheDough, ChallahDough, FocacciaDough etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOR-ys2KurNMCOonE0pw6Q.png" /></figure><p><em>You can read more about the benefits of having interface defining an action and other classes implementing the interface in another article of mine:</em></p><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/making-configurable-angular-feature-modules-using-strategy-pattern-b8f43340550a">Making Configurable Angular Feature Modules Using Strategy Pattern.</a></p><p>With dependency injections, we ensured that the chef making pizza can’t make its own dough, and with dependency inversion principle we ensured that different kinds of dough can now be used for making pizza. Same goes for dough maker. Dough maker doesn’t decide which flour and what kind of flour it will use rather it is handed to him by someone else in the charge of the pantry.</p><p>There is a slight problem with this pattern in applications. This might lead to a nested and complicated dependency graph because we keep manually initiating the dependencies and pass them up to the ones who need it. So what to do?</p><p><strong>Inversion of Control</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TZkDKkZ9zcosGO9uYbE9Sg.png" /></figure><blockquote>In <a href="https://en.wikipedia.org/wiki/Software_engineering">software engineering</a>, <strong>inversion of control</strong> (<strong>IoC</strong>) is a programming principle. IoC inverts the <a href="https://en.wikipedia.org/wiki/Control_flow">flow of control</a> as compared to traditional control flow. In IoC, custom-written portions of a <a href="https://en.wikipedia.org/wiki/Computer_program">computer program</a> receive the flow of control from a generic <a href="https://en.wikipedia.org/wiki/Software_framework">framework</a>.</blockquote><p>Dependency Injection so far looked like this: The Caller needs a pizza and announces it in the kitchen. Pizza Chef doesn’t make the dough himself, he asks it from the dough maker.</p><p>But imagine the dough maker now says, “Don’t ask me for the dough, I will keep my dough on a table when I am done myself.”</p><p>There is a Hollywood Principle, which states:</p><blockquote>“Don’t Call Us, We’ll Call You”</blockquote><p>This is what inversion of control looks like. The Pizza Chef doesn’t directly gets the dough from the dough maker, rather he will get if from an external place without him having a knowledge of who or when it was exactly kept there.</p><p><strong>Inversion of Control Container</strong></p><blockquote><a href="https://www.danclarke.com/all-about-inversion-of-control-containers">The basic idea is that on startup of your application, you can define mappings between your abstractions and your implementations — eg. between your interfaces and concrete types. Then the IoC Container library will handle creating your objects for you and will automatically inject any dependencies.</a></blockquote><p>So finally we have a kitchen table which contains the prepared items. Pizza chef takes everything from there when he needs it and starts making pizza. A programming container also behaves as such, which typically bind interfaces to implementations and serves them as dependencies when someone needs it. It will look something like this in the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBRIswZB6JlgOrvl6Lg5OQ.png" /></figure><p>Now let’s<strong> <em>code the talk</em></strong> using TypeScript. The demo code is a <strong>NodeJS </strong>application build using <strong>Express</strong> framework. We will be using <a href="http://inversify.io/"><strong>InversifyJS</strong></a>, an IoC container for TypeScript, in this project. Here are the dependencies used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gB52o36n0Bc-DSfr3PYETw.png" /></figure><p>I used lightweight <strong>InversifyJS</strong> to simply demonstrate how containers work in applications, and then it will be easier for us to understand how frameworks like <strong>Angular</strong> or <strong>Laravel</strong> uses DI and IoCs. You can check out the entire repository here:</p><p><a href="https://github.com/Saad-Amjad/inversify-di-ioc">Saad-Amjad/inversify-di-ioc</a></p><p><strong>TypeScript Interfaces</strong></p><p>Let us create an interface called Dough. I don’t like using IDough naming, so would name dough as Dough for the rest of the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tiLIIXbavHm776QichtFaw.png" /></figure><blockquote><a href="https://weblogs.asp.net/dwahlin/the-role-of-interfaces-in-typescript">TypeScript — interfaces are only used when you’re writing code (the editor can show you errors) and when you compile. They’re not used at all in the generated JavaScript.</a></blockquote><p>We usually want to code using interfaces so that we have better consistency across classes, but in the generated JavaScript, we do not have any references for it. It is simply non-existent. So how can we use Dependency Injection in TypeScript?</p><p><strong>Decorator Functions</strong></p><blockquote><a href="https://www.typescriptlang.org/docs/handbook/decorators.html">A <em>Decorator</em> is a special kind of declaration that can be attached to a class declaration, method, accessor, property, or parameter. Decorators use the form </a><a href="https://www.typescriptlang.org/docs/handbook/decorators.html"><em>@expression</em>, where </a><a href="https://www.typescriptlang.org/docs/handbook/decorators.html"><em>expression</em> must evaluate to a function that will be called at runtime with information about the decorated declaration.</a></blockquote><p>Frameworks like <a href="https://angular.io/guide/dependency-injection"><strong>Angular</strong></a> and <a href="https://github.com/nestjs/docs.nestjs.com/blob/master/content/custom-decorators.md"><strong>NestJS</strong> </a>use decorators, so to tell its own DI mechanism what are the dependencies other classes require and and how to initialise them.</p><p>These decorator functions basically add metadata to our classes. The metadata are used to gain information about what dependencies it needs during runtime and thus helps in resolving them.</p><p>In Inversify, whenever a class (it will always have @injectable decorators on them) have a dependency on an interface, we use the @inject decorator to define an identifier for the interface that will be available at runtime.</p><p><strong>Reflection APIs</strong></p><p><a href="https://en.wikipedia.org/wiki/Reflection_(computer_programming)">Reflection</a> allows inspection and modification of a code base, including its own. Unlike JavaScript, TypeScript does support experimental reflection features, though few in number. Using metadata reflection API we can standardize how we achieve details of unknown objects during runtime.</p><p>For the class Pizza, when DoughEntity dependency is called, the Injectable Decorator adds a metadata entry for the property using the Reflect.metadata function from the <a href="https://github.com/rbuckton/reflect-metadata">reflect-metadata</a> library, which gives us a array of dependencies that the class requires. It under the hood presents us with the right information about DoughEntity being a dependency for Pizza.</p><p>Here is a simplified example of the metadata generated when we pass a concrete implementation. You can find this behaviour in NodeJS frameworks like <strong>NestJS, </strong>and its decorator usage .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*vhB6so26vN9ikisTUhJQSg.png" /></figure><p>We can see that the metadata points to exact DoughEntity. But what if we injected Dough interface?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*FI8O1EjVrjlnJqzczCVOiQ.png" /></figure><p>When the code transpiles to JavaScript, we get metadata as an Object, with no way for us to say which specific object it is. We need to do something to uniquely identify them so that during runtime the proper class is resolved.</p><p>In our demo code after having coded in the interfaces, we type-hint our <em>real</em> classes instead of interfaces. We use <a href="https://javascript.info/symbol">Symbol</a> to allow identification of Dough interface with “Dough”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6m9QHWH-lzdeOAB-WCJe_A.png" /></figure><p>Let’s create a concrete implementation of Dough now and call it DoughEntity. Here you can see, we mentioned the class as injectable using @injectable decorator, and passed in interfaces in the constructor with @inject decorator so to define the identifiers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DDa2m8zmLSJDJ5Hh-Of16w.png" /></figure><p>Now we create a container that binds the interfaces to their implementations such that if Dough types are called, we get concrete implementation of DoughEntity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Abfo6UQYUO1k70dE7OR4xw.png" /></figure><p>We create the Pizza class as following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rlSVBNcPQYd_2AS_si8WsQ.png" /></figure><p>Before we do the rest of the code, here is a peek in to the future for better understanding. Since we are using decorators and reflection API, if we see the generated JavaScript code of pizza.class.ts by setting “emitDecoratorMetadata”: true in tsconfig.json, we could see that the Object we get in metadata will be of type Types.Dough.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nrpGQwJZxYVm0-_pn26GuQ.png" /></figure><p>And the generated JavaScript code of dough.entity.ts would look like this, where param orders tell us about DoughEntity dependencies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-MPkR5DCteb62AdLg7WOHg.png" /></figure><p>It surely seems that the decorators and reflection API have done its part in figuring out the dependency.</p><p>And now the moment of truth, let’s resolve a dependency here in the main.ts file and see it in action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zhA2xMUJedjnYeUz5tGFPA.png" /></figure><p>If we run the application and open the console, we can see the following output telling us the Dependency Injection worked using the IoC container.</p><p>Salt gets initiated first. Water, Flour, Yeast and finally Dough classes were successfully resolved and finally got injected into the Pizza class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*IkY32sBwSL1fud3EMXfvTQ.png" /></figure><p><strong>Conclusion</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8uM5u1zvMxAL5KrZXTgmw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@davidcainart?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">David Cain</a> on <a href="https://unsplash.com/s/photos/pizza?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>There is a reason why good kitchens operate similar to this. A real chef can vouch about it more than me for sure. They can truly deliver the best when each of the chefs has singular, separate responsibilities, each knowing exactly what, where and when to contribute in making a perfect dish for the restaurant-goers, with someone managing it, who is however not directly being involved in any of the process details.</p><p>I am really impressed with the DIs and IoCs of <a href="https://laravel.com/docs/5.8/container"><strong>Laravel</strong></a>, <a href="https://angular.io/guide/dependency-injection"><strong>Angular</strong></a> and <a href="https://docs.nestjs.com/providers"><strong>NestJS</strong></a>. It has really made both the backend and frontend code much more manageable, reusable and testable with time. The key concepts are the same in all the frameworks and are rightly so. Feel free to reach out to me on Twitter (<a href="https://twitter.com/saadbinamjad">@saadbinamjad</a>), and you can check some other articles posted by me in our <a href="https://medium.com/monstar-lab-bangladesh-engineering">Engineering Blog</a>. If you want to read more on some diversified topics, please check out our company <a href="https://www.monstar-lab.co.bd/blog/">blog</a>.</p><p>Thanks, till then happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b70643f71c91" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/simplifying-dependency-injection-and-ioc-concepts-using-typescript-b70643f71c91">Simplifying Dependency Injection and IoC Concepts using TypeScript</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why would I consider SAGA pattern on my next microservices project]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/why-would-i-consider-saga-pattern-on-my-next-microservices-project-71ea2b7238ff?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/71ea2b7238ff</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[saga]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <dc:creator><![CDATA[Sanim Khan]]></dc:creator>
            <pubDate>Mon, 23 Sep 2019 11:26:09 GMT</pubDate>
            <atom:updated>2019-09-23T11:26:09.781Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why would I consider SAGA on my next microservices project</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aXHK60NXvoFejOxj" /><figcaption>Photo by <a href="https://unsplash.com/@seefromthesky?utm_source=medium&amp;utm_medium=referral">Ishan @seefromthesky</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I used to think that I can design any microservices application using either <strong>Database Per Service</strong> or <strong>Shared Database</strong> pattern.<br><em>I was wrong, of course</em>. <br>When I came to know about SAGA, I realized what problems I could get into and when my application could fail with other patterns.</p><p>In this post I will try to answer 2 simple questions about SAGA.</p><p><strong>What is SAGA?<br></strong>A microservices architectural pattern, which provides a solution to implement business transactions which need to span multiple microservices.</p><p><strong>Why would I use SAGA?<br></strong>Well, before answering that question I want to describe some problem scenarios which will explain <strong>why SAGA is even needed</strong>.</p><h3>When existing solution is not enough</h3><p>Assume we want to build an e-commerce application. High-level microservices architecture of the system might be like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*RW-UrypV5FsCBLA_scUulg.png" /><figcaption>E-commerce architecture</figcaption></figure><p>Let’s check how we can efficiently implement transactions in our microservices system. <br>We will check if we can use <strong>Database per Service </strong>or<strong> Shared database </strong>pattern, and what are the limitations.<br>Then will explore <strong>SAGA</strong> pattern, and how it can overcome those limitations.</p><h4>Database per Service</h4><p><strong>Database per Service</strong> is a widely used and straight forward pattern to design microservices applications.<br>In this approach, each service has its own database. That means other microservices within the system can not access them directly. Data transaction between services can only occur via some well defined APIs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R9_f8CBRG5sfGu7MvQ8aTA.png" /><figcaption>Database per service pattern</figcaption></figure><p>This pattern introduces a limitation to the system- <strong>How to implement a business transaction that span multiple microservices?<br></strong>Usually microservices need data from other services to implement logic. This can create very complex and hard to maintain interactions.</p><p>For example, let’s assume in our e-commerce system orders have a stock limit. <br>It will be difficult to make sure that an order will not exceed the stock limit. As Order and Stock are in different databases, the application cannot simply use a local ACID transaction.</p><p>So it becomes difficult to maintain data consistency of the entire system as a whole with Database per service pattern.</p><h4>Shared database</h4><p><strong>Shared database </strong>pattern involves using a single monolithic database rather than using multiple service specific database.<br>This pattern tries to solve the same challenge by implementing a comparatively lenient way which is- using single database and providing all services access to it.</p><p>As developers can work in existing ways and ACID transactions can be used to enforce consistency, <strong>Shared database</strong> is considered safer for developers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CSgW6rDQgxSBFi8wli-4Bg.png" /><figcaption>Shared database pattern</figcaption></figure><p>You already have noticed the <strong>problem of Shared database</strong> pattern right?</p><p>What is the use of microservices if we are forced to implement a single DB? We can not leverage most of the benefits of microservices architecture.<br>Database or table structure change has effects on multiple services. So developers in different teams need to coordinate to change schema.</p><p>If even we have a strong and organized team, and can handle these problems with some extra work, the real problem occurs when multiple services are trying to access the same database resource at the same time. Run-time error occurs!</p><h3>What is Saga Pattern</h3><p>We can solve our above mentioned transaction design problems by implementing SAGA.</p><p><strong>SAGA</strong> is a sequence of local transactions, where the first transaction is initiated by an external source. And each local transaction-</p><ul><li>updates data within that particular service.</li><li>publishes a message or event.</li><li>the event triggers next local transaction.</li></ul><p>High level SAGA design would be something like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tM6wJGulPsYQyd3w8rIHkA.png" /></figure><p>We can implement SAGA in a couple of ways. Two most popular of them are-</p><ul><li><strong>Events/Choreography</strong> — each local transaction publishes domain events that trigger local transactions in other services.</li><li><strong>Command/Orchestration</strong> — an orchestrator (object) tells the participants what local transactions to execute.</li></ul><h4>Events/Choreography</h4><p>In this approach, a distributed transaction is created.<br>The first service executes the first transaction and publishes an event. Other services listen to this event, execute transactions and publish new events.</p><p>The transaction ends when the last <strong>local transaction</strong> is executed by the last service, and no new event is created or event is not listened by any services.</p><p>Let’s see the event flow in our e-commerce system-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*Xqo_KSNBhEwr1MOlT2KL-g.png" /><figcaption>SAGA implementation with Events/Choreography</figcaption></figure><ol><li><em>Order Service-<br>- </em>creates a new order.<br><em>- </em>sets the state as <em>pending.<br>- </em>publishes an event called <strong><em>ORDER_CREATED_EVENT</em></strong>.</li><li><em>Payment Service-<br>- </em>Listens to <strong><em>ORDER_CREATED_EVENT.<br></em></strong><em>-</em><strong><em> </em></strong>Charges the client.<br>- Publishes an event called <strong><em>BILLED_ORDER_EVENT</em></strong>.</li><li><em>Stock Service- </em><br>- Listens to <strong><em>BILLED_ORDER_EVENT.<br></em></strong><em>-</em><strong><em> </em></strong>Updates the stock<br>- Prepares the product purchased in the order.<br>- Publish event <strong><em>ORDER_PREPARED_EVENT</em></strong>.</li><li><em>Delivery Service-<br>- </em>Listens to <strong><em>ORDER_PREPARED_EVENT.<br></em></strong><em>-</em><strong><em> </em></strong>Picks up and delivers the product.<br>- Publishes event <strong><em>ORDER_DELIVERED_EVENT.</em></strong></li><li>Finally, <em>Order Service-<br>- </em>Listens to <strong><em>ORDER_DELIVERED_EVENT<br></em></strong><em>-</em><strong><em> </em></strong>Sets the state of the order as finished.</li></ol><p>In this case, if the state of the order needs to be tracked, Order Service could listen to all events and update its state.</p><h4><strong>Command/Orchestration</strong></h4><p>In this approach, a new service is created.<br>This service has the sole responsibility of telling each SAGA participant what to do and when to do it.</p><p>The SAGA orchestrator communicates with each service in a command style, and tells them which operation should be performed.</p><p>Let’s see the event flow in our e-commerce system-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5opBeAS_0Of9AvvOdEY_g.png" /><figcaption>SAGA implementation with Command/Orchestration</figcaption></figure><ol><li><em>Order Service-<br>- </em>Saves a pending order.<br>- Asks Order Saga Orchestrator (OSO) to start a <strong><em>create order transaction</em></strong>.</li><li><em>OSO-<br>- </em>Sends an <strong><em>Execute Payment</em></strong> command to <em>Payment Service.<br>- Payment Service</em> replies with a <strong><em>Payment Executed</em></strong> message.</li><li><em>OSO-<br>- </em>Sends a <strong><em>Prepare Order</em></strong> command to Stock Service.<br>- Stock Service replies with an <strong><em>Order Prepared</em></strong> message.</li><li><em>OSO-</em> <br>- Sends a <strong><em>Deliver Order</em></strong> command to Delivery Service.<br>- Delivery Service replies with an <strong><em>Order Delivered</em></strong> message.</li></ol><p>In this case, <strong>Order Saga Orchestrator</strong> knows which flow is needed to execute a <strong>Create order</strong> transaction. <br>If anything fails, OSO will initiate the rollback by sending commands to the participants to undo the previous operation.</p><h3>Rollbacks in SAGA</h3><p>Rollback is needed if a transaction fails because of violating a business rule.<br>SAGA generally handles rollback by executing a series of transactions, which at the end revert the changes that were made by the previous local transactions.</p><h4>Rollback in SAGA’s Events/Choreography design</h4><p>Rollback in a distributed transaction architecture is not very easy. It requires to implement another transaction to compensate the effect of earlier transactions.</p><p>Assume that Stock Service has failed while executing a transaction. Let’s see what the rollback would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*YNRlry_LDv4nukRlPHreRg.png" /><figcaption>Rollback in SAGA’s Events/Choreography</figcaption></figure><ol><li><em>Stock Service-<br>- </em>Produces <strong><em>PRODUCT_OUT_OF_STOCK_EVENT.</em></strong></li><li>Both <em>Order Service</em> and <em>Payment Servic</em>e-<br>- Listen to <strong><em>PRODUCT_OUT_OF_STOCK_EVENT</em></strong>.</li><li><em>Payment Service-<br>- </em>Refund the client.</li><li><em>Order Service-<br>- </em>Set the order state as failed.</li></ol><p>In SAGA, it is important to assign each transaction a unique ID. So that whenever an event is created, all listeners can identify which transaction it refers to.</p><h4>Rollback in SAGA’s Command/Orchestration design</h4><p>Rollback in Orchestration design is lot more simpler as there will always be an orchestrator to coordinate the transactions and events.</p><p>The process will be like this-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kbw-5I0Z6fbLLSQ84rQUmw.png" /><figcaption>Rollback in SAGA’s Command/Orchestration</figcaption></figure><ol><li><em>Stock Service-<br></em>Replies to OSO with an <strong><em>Out-Of-Stock</em></strong> message.</li><li><em>OSO-<br>- </em>Identifies that the transaction has failed and starts the rollback.<br>- Only one transaction (Execute payment) was executed successfully before the failure. So OSO sends <strong><em>Refund Client</em></strong> command to <em>Payment Service</em> and set the order state as failed.</li></ol><p>SAGA can be designed to make all participants reply to a fixed address. But best implementation would be to send reply address within the message. This way we can enable participants to reply to multiple orchestrators.</p><h3>Conclusion</h3><p>Most important reason of using SAGA pattern is that it maintains data consistency between multiple services without coupling them tightly. This is an extremely significant feature for a microservices architecture.</p><p>But data consistency is not the only thing we have to consider while designing a system. One of the major disadvantage of SAGA is implemtation complexity from a programmer’s perspective.<br>SAGA requires designing compensating transactions, which may make the implementation complicated.</p><p>However, SAGA is well suited to solve some specific challenges. So we should explore it when the need arises.</p><p>Check out other articles from our engineering team:<br><a href="https://medium.com/monstar-lab-bangladesh-engineering">https://medium.com/monstar-lab-bangladesh-engineering</a></p><p>Visit our website to learn more about us:<br><a href="http://www.monstar-lab.co.bd/">www.monstar-lab.co.bd</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71ea2b7238ff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/why-would-i-consider-saga-pattern-on-my-next-microservices-project-71ea2b7238ff">Why would I consider SAGA pattern on my next microservices project</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Our Journey From Heroku To Kubernetes [Part-1]]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/our-journey-from-heroku-to-kubernetes-part-1-116f4bdc44a4?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/116f4bdc44a4</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[digitalocean]]></category>
            <category><![CDATA[heroku]]></category>
            <dc:creator><![CDATA[Md. Mazharul Islam]]></dc:creator>
            <pubDate>Mon, 23 Sep 2019 11:07:09 GMT</pubDate>
            <atom:updated>2019-09-23T11:07:09.018Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ngI_AOo2CGu38KQHhhO-2A.png" /></figure><p>Our client was running their infrastructure in Heroku. As a PaaS, Heroku is awesome and you can sleep good while your application is being taken care by the best trained infra professionals.</p><p>But this happiness comes with some caveats:</p><ol><li>You have to pay for it.</li><li>You don’t have enough freedom to play with it. You have to follow their book and they will guarantee it will just work.</li><li>It doesn’t allow you to implement complex deployment process.</li><li>Multiple environment management sometimes can be very cumbersome and tricky.</li><li>Every time you switch your infrastructure you have to start from scratch.</li><li>Micro-service based applications are not well suited in this kind of environment.</li><li>Limited monitoring tools (sometimes).</li><li>It’s hard to achieve true infrastructure as code.</li></ol><p>While the first reason seems to not be an issue when you start small. As you grow, your cost can significantly hurt you in the long run. For us, the second reason was the biggest issue; even if we somehow achieved it, the first reason seemed to dawn on us.</p><p>Anyway, we decided for a change. A change will take us a long long way from here, without having to break our money bank and also it gives us just enough room to expand for the unknown future.</p><p>Moreover, our application had to achieve some very strict checks before it was deployed to new versions. The checks were:</p><ol><li>We take backups of that target environment databases.</li><li>For testing convenience, we restored production database (excluding user and sensitive information) onto staging database, to conveniently check if anything was breaking on the real world data. It gave us confidence that our newly added application is working as expected.</li></ol><p>Our application was a Ruby On Rails application. It was just a medium sized monolith with having some moving parts (which we will see as we go forward).</p><p>So when we decided to go for IaaS, there were a bunch of choices we could have made. But we decided to use <a href="https://www.digitalocean.com/">DigitalOcean</a>. Although AWS may seem to be the obvious choice, we decided to go for DO because:</p><ol><li>It provides one of the cheapest hardware/spec per dollar.</li><li>It provides managed Kubernetes.</li><li>It has very easy and developer friendly documentation, tutorials and a great community.</li><li>Although it has some <a href="https://www.digitalocean.com/docs/kubernetes/overview/#known-issues">shortcomings</a> now but it seems to be promising in the upcoming days.</li><li>It also has managed cheap Postgres Database, Load Balancer and S3 compatible storage option, which are must needs for our project.</li></ol><p>In terms of cost optimisation, on Heroku we had this following setup</p><h4>Staging</h4><ol><li>Application: Standard-1X = $25</li><li>Background Processing: Standard-1X = $25</li><li>Postgres: Standard-0 = $50</li></ol><h4>Production</h4><ol><li>Application: 3 Standard-2X = $150</li><li>Background Processing: Standard-1X = $25</li><li>Scheduler: Standard-1X = $25</li><li>Postgres: Standard-0 = $50</li></ol><p>We had cost around $350-$400 every month.</p><p>As we moved to k8s on DO, lets see what we achieved,</p><h4>Kubernetes On DigitalOcean:</h4><p>Node Pool 1(5 instance): 1vcpu + 2Gb Ram =&gt; $10 * 5 = $50</p><p>Node Pool 2(3 instance): 2vcpu + 4Gb Ram =&gt; $20 * 3 = $60</p><p>In these two node pools we separated our environment specific nodes by tags(don’t worry I will explain later about these terms later). Now see what we were able to achieve in these two node pools. I know some of the terms in below may be unknown to you, so for now just skip them:</p><ul><li>1 Cert Manager (for proving and renewing lets-encrypt free SSL for all applications)</li><li>1 Cert manager cainjector</li><li>1 Cert manager webhook</li><li>1 Docker registry (Backed by DigitalOcean Space)</li><li>1 CI/CD instance</li><li>1 Staging Postgres database(Using <a href="https://kubedb.com/">Kubedb</a>) with HA.</li><li>1 tiller instance(For using Helm Package manager)</li><li>1 Docker registry UI</li><li>1 nginx ingress pod</li><li>1 auto job cleaner (for all namespace)</li><li>1 DB auto backup task runner for staging</li><li>1 DB auto backup task runner for production</li><li>Staging application(2x)</li><li>Staging Background Job Runner(1x)</li><li>Production database with HA. 1 primary, 1 slave and 1 standby</li><li>PostgresAdmin UI.</li><li>Linkerd Service Mesh for monitoring.</li></ul><p>As you can see it’s a pretty long list. Additionally we now also have freedom to add any custom task runner anytime. Anyway, the primary goal is already achieved and we were able to cut down our infra cost like one-third. We had some additional cost apart from k8s clusters like:</p><ul><li>Load Balancer: While we used ingress controller, we had to just use one load balancer for all.</li><li>S3 storage: Some objects were already hosted in AWS s3, we decided to keep as it is. As new elements has been added in the equation like hourly database backup, new docker images etc, we planned to go for s3 compatible <a href="http://digitalocean.com/">DO</a> solution <a href="https://www.digitalocean.com/products/spaces/">space</a>.</li><li>NewRelic APM.</li><li>Mail solution cost.</li></ul><p>But these cost were there in Heroku too and is platform and infrastructure independent. So I excluded them out the equation at first.</p><p>Additionally, we have used <a href="https://helm.sh/">Helm</a> to package our application to the cluster and used <a href="https://drone.io/">Drone</a> to automate the CI/CD pipeline, which is hosted into our kubernetes pipeline itself. Naturally I guess CI/CD pipeline meant to be running into your cluster. Whenever a new deployment happens it creates new pods and jobs, whenever a deployment completes it frees the resources. Helm was meant to handle complex deployment process and it integrates with CI/CD very easily. If your deployment takes a lot of juice then you can autoscale your cluster and can use some nodes dedicatedly for build and deployment stuff only.</p><p>I have planned to cover follow things in my upcoming stories:</p><ul><li>Concept on Kubernetes components.</li><li>Setting up a Kubernetes cluster.</li><li>Setting up a private registry inside your Kubernetes cluster.</li><li>Deploying your first application inside the cluster.</li><li>Setting up ingress and load-balancer for your application.</li><li>Add free SSL certificates to your application using lets-encrypt.</li></ul><p>While there are thousands of tutorials and videos on Kubernetes basics, I will try to focus to write for the first-timers so that they can have the same experience as we had. On top of that I will also cover following sections, which I guess will be tremendously helpful (because we didn’t find good articles on this items yet).</p><ul><li>Set up CI-CD inside of your Kubernetes cluster.</li><li>Managing multiple environment?</li><li>Database inside or outside of your cluster? Things to consider.</li><li>Helm chart for your application.</li><li>Managing node pools and segregating your workloads.</li><li>Managing roles inside of your cluster.</li><li>Better deployment process to test your app more efficiently.</li></ul><p>So when you are building your app or infrastructure or anything, it’s impossible to maintain and follow the best practices at the first time, we all know that this just don’t work like this. So I am going to update my articles from time to time and will let you see that how things move on from early stage to the future. Stay tuned 😉</p><p>Check out other articles from our engineering team:<br><a href="https://medium.com/monstar-lab-bangladesh-engineering">https://medium.com/monstar-lab-bangladesh-engineering</a></p><p>Visit our website to learn more about us:<br><a href="http://www.monstar-lab.co.bd/">www.monstar-lab.co.bd</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=116f4bdc44a4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/our-journey-from-heroku-to-kubernetes-part-1-116f4bdc44a4">Our Journey From Heroku To Kubernetes [Part-1]</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dagger for Android: A Beginner’s Guide to Multibindings]]></title>
            <link>https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-to-multibindings-de3ccba5636c?source=rss----ba5303c329d8---4</link>
            <guid isPermaLink="false">https://medium.com/p/de3ccba5636c</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[architecture-components]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Avishek Khan]]></dc:creator>
            <pubDate>Tue, 17 Sep 2019 14:02:23 GMT</pubDate>
            <atom:updated>2019-09-17T15:34:21.194Z</atom:updated>
            <content:encoded><![CDATA[<p>The happy marriage between ViewModel and Dagger using @IntoMap</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*df03zDCSFsQSq9cWmmHGTA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@coffee_wanderer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Thanh Tran</a> on <a href="https://unsplash.com/search/photos/knife?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>One particular challenge we face when working with ViewModel architecture component together with Dagger is that there’s no straightforward way to provide a ViewModel dependency having non-zero argument constructor that will be conscious of the lifecycle associated with the injection target such as activity or fragment. One solution to this problem is to implement a ViewModelProvider.Factory which can be used to instantiate ViewModels in a lifecycle-aware way. This implementation of ViewModelProvider.Factory needs a Map dependency that Dagger has to satisfy. The way we achieve this resolution is by using <strong>multibindings</strong>.</p><p>Multibinding is a way of binding several objects of some given type into a <strong>collection</strong> that application code can inject without depending on the individual bindings directly. To make things easy, Dagger comes with a few annotations such as @IntoSet, @ElementsIntoSet and @IntoMap for different use cases.</p><p><strong>Note:</strong> henceforth in this post, I’m going to write Factory instead of ViewModelProvider.Factory to keep it short and easier to read.</p><p>The map multibinding is especially useful if we want to create a Factory instance. We have to bind all the ViewModels into a map using the @IntoMap annotation. Dagger will manage the map itself and provide as the dependency of Factory implementation when we tell it to. We’ll see how it can be done in the examples of this article. But before that, let’s take a step back and see why exactly we need this.</p><p><strong>Note</strong>: If you want to learn about @IntoSet and @ElementsIntoSet, you can start from <a href="https://dagger.dev/multibindings.html">here</a>.</p><h4><strong>Why @IntoMap?</strong></h4><p>There are a few ways you can create a ViewModel instance. Since it’s a <strong>lifecycle-aware</strong> component, it needs a reference of the <strong>lifecycle owner</strong> such as an activity or fragment. You can instantiate a ViewModel object from an activity or fragment in the following manner to make it lifecycle-conscious:</p><pre><strong>val</strong> viewModel = ViewModelProviders.of(this)[YourViewModel::class.java]</pre><p>Sweet! But there’s a catch — YourViewModel class cannot have any parameter in its constructor. But, this is impractical, in real-life scenarios, it can have any number of parameters such as repository, or if you’re into the <em>clean architecture,</em> one or more use cases. For non-zero argument constructors, you can instantiate the object in the age-old way:</p><pre><strong>val</strong> viewModel = YourViewModel(argA, argB)</pre><p>Can you see the problem here? The ViewModel isn’t aware of the lifecycle owner in this approach and therefore it will not survive activity recreation. This is basically the MVP pattern where ViewModel is taking the presenter’s job. This is where the Factory interface comes into play. You can implement it and override the create() function to instantiate your ViewModel (we’ll come to the details in a while) and write something like the following in your activity or fragment class:</p><pre><strong>val</strong> viewModel = ViewModelProviders.of(this, FactoryImplementation())[YourViewModel::class.java]</pre><p>Instead of creating a Factory instance every time, you can make it a singleton that Dagger will provide to our injection targets, i.e., activities and fragments. Let’s see how.</p><p>But before starting off, I would recommend you clone the <a href="https://github.com/AVI5HEK/Purrfect-App">repository</a> and checkout part-3 branch. All the examples given in this post are from this project. If you’ve been following this series, you should know by now that I created the project based on dagger-android and the latest version of Dagger as of September 2019 (v2.24) so that you can write the code described in the article and learn Dagger interactively. The stuff necessary for this part was already implemented in the previous post and the complete source code can be found in the aforementioned branch. If this is your first time here, you can check out <a href="https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-part-1-3d8b0e4de68c">part 1</a> and <a href="https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-part-2-9ed3f67c5953">part 2</a> where I covered initial setup for Dagger, providing dependencies and scoped bindings. This post is written on the assumption that you already have the basic idea about these things.</p><h4><strong>Implementing ViewModelProvider.Factory</strong></h4><p>Create a class that implements ViewModeProvider.Factory and overrides create() function. Add a Map&lt;Class&lt;out ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; parameter to its constructor as the dependency. For example, take a look inside the ViewModelFactory.kt file which contains all the implementation details. Since we want to instantiate the class only once in our app’s lifetime, annotate it with the same scope as our AppComponent’s — @Singleton:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f37b665fcda940cee6dfca95cdb6728/href">https://medium.com/media/9f37b665fcda940cee6dfca95cdb6728/href</a></iframe><p>Now it’s time to tell Dagger how to resolve this dependency by providing the map.</p><h4><strong>Creating the map key</strong></h4><p>Create an annotation class that Dagger will use to create the key of the map. For example, take a look inside ViewModelKey.kt which is used to tell Dagger that the key of the map will be a ViewModel Class instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cec00e1b499cf7fd8c1c979f1233f7f3/href">https://medium.com/media/cec00e1b499cf7fd8c1c979f1233f7f3/href</a></iframe><h4><strong>Binding into map</strong></h4><p>Create an abstract @Module class and write an abstract function inside it to bind your ViewModel into the map using @ViewModelKey. The <em>return type</em> of the function tells Dagger that the value of the map will be a Provider&lt;ViewModel&gt; instance and the key will be the Class instance of your ViewModel. Take a look inside the ViewModelModule.kt file to see it in action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/661c4d0a49565ce12f37bbfd6a8520bb/href">https://medium.com/media/661c4d0a49565ce12f37bbfd6a8520bb/href</a></iframe><p>In your own projects, write an abstract function for each of your ViewModels in this fashion. Make sure the ViewModel implementation has an @Inject-annotated constructor so that Dagger knows how to provide the instance.</p><p>Since the binding of MainViewModel is now done inside a module installed into @Singleton-scoped AppComponent, we have to disassociate MainViewModel from @ActivityScope by removing the scope annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/119a0c78fee5a255da0d27c1e94befdc/href">https://medium.com/media/119a0c78fee5a255da0d27c1e94befdc/href</a></iframe><p>We want to inject our MainActivity and CatFragment with the Factory instance. So we need to bind Factory to its implementation — ViewModelFactory:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3639e3a2a218238bea9ce3877ed8b286/href">https://medium.com/media/3639e3a2a218238bea9ce3877ed8b286/href</a></iframe><p>Add the module to AppComponent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fbb0b6cdc715567e881e20c0843f903/href">https://medium.com/media/0fbb0b6cdc715567e881e20c0843f903/href</a></iframe><h4><strong>Injecting </strong><strong>MainActivity and </strong><strong>CatFragment</strong></h4><p>Now replace the mainViewModel property with viewModelFactory of type ViewModelProvider.Factory in both MainActivity and CatFragment:</p><pre><strong>@Inject</strong><br><strong>lateinit</strong> <strong>var</strong> viewModelFactory: ViewModelProvider.Factory</pre><p>In onCreate() function of MainActivity, instantiate MainViewModel using this statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb72f0ddfdf091253a194eb4a9bd02c1/href">https://medium.com/media/bb72f0ddfdf091253a194eb4a9bd02c1/href</a></iframe><p>And in onViewCreated() of CatFragment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/978807297c90a8d53a6ee3def51bfc90/href">https://medium.com/media/978807297c90a8d53a6ee3def51bfc90/href</a></iframe><p>Now run the app. If you rotate the screen now, the activity will be recreated, but it will get the same ViewModel instance and the list already fetched from the server will be dispatched by the LiveData without any need for additional network call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sIkHWiSk4mL8v-Y995B1gQ.jpeg" /></figure><p>In case you haven’t noticed, now the CatFragment subcomponent doesn’t have any dependency on any of its parent component’s scoped bindings. Therefore, it doesn’t need to be a child of MainActivity component anymore. As long as all the activities and fragments in your projects depend only on Factory or any other @Singleton-scoped object, the subcomponent can be a child of AppComponent. That means, MainFragmentBindingModule and ActivityBindingModule can be installed independently into AppComponent. In other words, CatFragment and MainActivity subcomponents can be mutually independent sibling components. In this way, you can bind as many fragment subcomponents as you like inside the MainFragmentBindingModule. Since it’s no longer installed into the MainActivity subcomponent, you can remove the prefix ‘Main-’ from its name and simply call it FragmentBindingModule that would contain all the fragment subcomponent bindings of your project.</p><p>You can find the complete source code of this article in the branch part-3-complete:</p><p><a href="https://github.com/AVI5HEK/Purrfect-App/tree/part-3-complete">AVI5HEK/Purrfect-App</a></p><p>You can also find part 1 of the series here:</p><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-part-1-3d8b0e4de68c">Dagger for Android: A Beginner’s Guide — Part 1</a></p><p>And part 2 here:</p><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-part-2-9ed3f67c5953">Dagger for Android: A Beginner’s Guide — Part 2</a></p><p>Please do share your thoughts. You can reach out to me on Twitter: <a href="https://twitter.com/Avishek_Khan">@Avishek_Khan</a>. You might also want to check out more tech posts on our <a href="https://www.monstar-lab.co.bd/blog/">blog</a>. Until next time, happy learning!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de3ccba5636c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/monstar-lab-bangladesh-engineering/dagger-for-android-a-beginners-guide-to-multibindings-de3ccba5636c">Dagger for Android: A Beginner’s Guide to Multibindings</a> was originally published in <a href="https://medium.com/monstar-lab-bangladesh-engineering">Monstar Lab Bangladesh Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>