<?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[Stories by Güven Seçkin on Medium]]></title>
        <description><![CDATA[Stories by Güven Seçkin on Medium]]></description>
        <link>https://medium.com/@guven.seckin.4?source=rss-25eeef4d8f67------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qBvK9btODORvS__VXQYTCw.jpeg</url>
            <title>Stories by Güven Seçkin on Medium</title>
            <link>https://medium.com/@guven.seckin.4?source=rss-25eeef4d8f67------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:01:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@guven.seckin.4/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using Spring @Retryable with Feign Client Methods]]></title>
            <link>https://engineering.getmidas.com/using-spring-retryable-with-feign-client-methods-9f77e509ad55?source=rss-25eeef4d8f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f77e509ad55</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[retry]]></category>
            <category><![CDATA[feign]]></category>
            <category><![CDATA[aspect-oriented]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Güven Seçkin]]></dc:creator>
            <pubDate>Tue, 09 Nov 2021 11:56:06 GMT</pubDate>
            <atom:updated>2021-11-09T11:56:06.411Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4cyr7FUkfwMPRtI--ax0nw.jpeg" /><figcaption><a href="https://unsplash.com/photos/94GiZLiWD8Y">https://unsplash.com/photos/94GiZLiWD8Y</a></figcaption></figure><p>At Midas, we use Feign Client for communication between our microservices, and we prefer it due to its ease of use and extensibility.</p><p>However, we faced a minor issue when trying to make the calls <em>retryable</em>. Feign itself has its own <strong><em>Retryer</em></strong> mechanism to configure retries, but there is no way to configure it on a per-method basis as we’d like. Unfortunately, it’s also not possible to use the <strong>Spring @Retryable</strong> annotation together with the Feign annotations on the same method, due to Spring AOP dynamic proxy error.</p><p>To solve this problem, we used Aspect-Oriented Programming and wrote our own custom annotation: <strong>@FeignRetry</strong>. This annotation works like a wrapper for <strong>@Retryable</strong> and shares the same specs with it to avoid confusion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5f402e7b109f4a3231bcb6733a4b020/href">https://medium.com/media/c5f402e7b109f4a3231bcb6733a4b020/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c03c76ca26d5971170cf3cae31e33a7/href">https://medium.com/media/7c03c76ca26d5971170cf3cae31e33a7/href</a></iframe><p>As you can see below, the <strong>FeignRetryAspect</strong> is prepared to wrap feign client methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cac0d35ba4f27ec755b8c09a668aa8e1/href">https://medium.com/media/cac0d35ba4f27ec755b8c09a668aa8e1/href</a></iframe><blockquote>PS: Use Component annotation to register aspect to spring</blockquote><p>There are two methods of using Spring Retry. The first, and most commonly used method is the <strong>@Retryable </strong>annotation, while the second method is the <strong>RetryTemplate</strong>. We preferred to use Spring <strong>RetryTemplate</strong>.</p><p>The intent of this aspect is to catch the method that is annotated with FeignRetry, pass configuration to Spring RetryTemplate, and call the service according to the configuration.</p><p>As you can see, the annotation provides two different backoff policies: <em>ExponentialBackOffPolicy</em> and <em>FixedBackOffPolicy. </em>The main idea of <em>FixedBackOffPolicy</em> is to retry calls with fixed interval delays, while <em>ExponentialBackOffPolicy</em> has exponentially increased delays. You can learn more about these policies <a href="https://docs.spring.io/spring-batch/docs/current/reference/html/retry.html">here</a>.</p><p>Also, we need a retry policy to configure. SimpleRetryPolicy is used to accomplish this. Feign Client wraps connection refused and time out errors into RetryableException so we included this exception in the policy-map alongside with ClientException, which can occur if we send a call to an API that is not ready to receive calls yet. This will cause our aspect to trigger retry according to configurations for connection refusal, timeout exceptions, and also client exception which is added for <em>load balancer is not available</em> error.</p><h3><strong>Usage of @FeignRetry</strong></h3><p>The usage is as simple as putting the annotation on the Feign Client methods that we want the Retry mechanism to be active on. As mentioned before, the usage of the custom aspect is similar to Spring’s own @Retryable annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bccca9ea86df61e5d2e890e570ab46e/href">https://medium.com/media/1bccca9ea86df61e5d2e890e570ab46e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70d34b0819e9f7f8408ab1c7f9ce4f8b/href">https://medium.com/media/70d34b0819e9f7f8408ab1c7f9ce4f8b/href</a></iframe><blockquote>PS: To enable retry for spring, you should use @EnableRetry annotation into application class</blockquote><h3>Monitoring</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6g2y1ATrLch1RuCNCoSQWw.jpeg" /><figcaption><a href="https://unsplash.com/photos/4dKy7d3lkKM">https://unsplash.com/photos/4dKy7d3lkKM</a></figcaption></figure><p>As you can see above, at line 32 of FeignRetryAspect, we are logging the service calls made by this aspect so we can monitor them later. In that log, we have method name, max attempt count, delay, and most importantly, retry count. With each retry, this count will be incremented. This way, we will be able to measure how many times each service call was retried.</p><p>At Midas, we use Splunk for log monitoring purposes, and we wanted to create a dashboard to monitor the service calls that use this aspect. Here is our Splunk search query that creates a table for this purpose:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/366e7bb5a6030f1b10eec9d964fdd374/href">https://medium.com/media/366e7bb5a6030f1b10eec9d964fdd374/href</a></iframe><p>First, the contents of the log will be parsed into some fields with Regex, such as API name, method name, retry count, etc. After that, we filter the calls (using the <em>where </em>statement) where the retry count is greater than one, which means the call is retried. After that, we use the <em>stats </em>statement to calculate the total count per API+ method basis. Finally, API name, method, and delay are joined by using <em>eval</em> statement in order to can visualize this result in Splunk easier. We can now add the table created by this query into a dashboard to monitor our retryable Feign calls.</p><p>To sum up, we prepared a custom retry annotation to use on each Feign Client method separately. Therefore, we were able to use different retry policies for different methods. Besides, with the Splunk dashboard, we are able to understand API call performance.</p><p><strong><em>References:</em></strong></p><ul><li><a href="https://docs.spring.io/spring-batch/docs/current/reference/html/retry.html">Retry</a></li><li><a href="https://docs.splunk.com/Documentation/SplunkLight/7.3.6/Examples/Createadashboard">Create a dashboard</a></li><li><a href="https://docs.spring.io/spring-framework/docs/4.3.15.RELEASE/spring-framework-reference/html/aop.html">11. Aspect Oriented Programming with Spring</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f77e509ad55" width="1" height="1" alt=""><hr><p><a href="https://engineering.getmidas.com/using-spring-retryable-with-feign-client-methods-9f77e509ad55">Using Spring @Retryable with Feign Client Methods</a> was originally published in <a href="https://engineering.getmidas.com/">Midas Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The difference between Expression and Statement]]></title>
            <link>https://medium.com/@guven.seckin.4/the-difference-between-expression-and-statement-89e74596e546?source=rss-25eeef4d8f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/89e74596e546</guid>
            <category><![CDATA[expression]]></category>
            <category><![CDATA[statement]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Güven Seçkin]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 11:01:00 GMT</pubDate>
            <atom:updated>2020-09-16T11:01:00.782Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Akr9Jc_r6wLow0N2OZlEQQ.png" /></figure><p>As developers, we live in a world that is a kind of abstraction of the real world. The best example of this abstraction is the software language. Compare to human language, software languages consist of different tokens. There isn’t any word, subject, or adverb like human language. However, all languages should consist of different components. In the software world, languages consist of functions, statements, or expressions, etc.</p><p>Almost all developers are able to understand which part of the code is a function. Because functions are explicitly declared in some software languages. For instance, in Python, the function should be declared with <em>def </em>keyword. In addition to that, in Javascript, functions can be defined with <em>function</em> keyword.</p><p>Well, how can a developer understand the statement or expression in the bunch of program code?</p><p>In this article, I am going to explain what is the difference between expression and statement in software languages. To illustrate the difference, Go and C languages will be used as an example.</p><p>Before diving the difference between C and Go languages, let me explain the definition of statement and expression.</p><blockquote>“A statement performs some action such as printing values, looping, or if statement. On the other hand, expressions also produce values and we can assign these values to new variables.”</blockquote><blockquote>“Go is a compiled, concurrent, garbage-collected, statically typed language developed at Google. For detail, you may consider reading Rob Pike’s <a href="https://talks.golang.org/2012/splash.article#TOC_16">keynote talk</a>.”</blockquote><blockquote>“Rob Pike is one of the designer of Go.”</blockquote><p>There are many differences between Go and C. However, I am going to talk about two differences to give an example of our topic.</p><p>Now, we can begin with the first difference,</p><p>In Go,</p><blockquote>++ and -- are statements not expressions</blockquote><p>Unlike Go, in C language, increment and decrement operators are expressions. Therefore, we able to assign produced values to new variables like below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8970665262a426e2cf6313abb650981e/href">https://medium.com/media/8970665262a426e2cf6313abb650981e/href</a></iframe><p>When this code executed, there will be <strong>no</strong> error. Because the increment operator is an expression in C language. The printed value will be 10 because the postfix increment operator is used. Because of this, at first, the initial value of <strong>a</strong> is assigned to <strong>b</strong>.<strong> </strong>After that, <strong>a</strong> will be increased to 11. Therefore b is initialized with the first value of a.</p><p>If we use the prefix increment operator, the value of <strong>b</strong> will be the same with the value of <strong>a</strong>. Because the assignment operator will be executed after the increment operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cc73fc499eda2a8d8dd8ade81340118/href">https://medium.com/media/0cc73fc499eda2a8d8dd8ade81340118/href</a></iframe><p>In this case, the printed value will be 11.</p><p>On the other hand, in Go, increment, and decrement operators are not expressions. That&#39;s why, if we try to assign this value, our code will not be compiled. We will get a syntax error. Because there is not a returned value from this operation.</p><p>Let’s see the <strong>incorrect</strong> Go code. ⚠️</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23a43acb3e50f4041daa19a064eb707b/href">https://medium.com/media/23a43acb3e50f4041daa19a064eb707b/href</a></iframe><p>How can we use increment operator in Go? 🤔</p><p>As mentioned, in Go, the increment operator is not expression. The operator affects the operand directly. It will not produce any value. Therefore, we have only a postfix increment operator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbe8ed5041cee7f39c027b4d274880d1/href">https://medium.com/media/cbe8ed5041cee7f39c027b4d274880d1/href</a></iframe><p>The printed value will be 2.</p><p>One more difference to compare to C,</p><blockquote>The assignment is not an expression</blockquote><p>As mentioned, expressions generate values. In C, when we assign a value to a variable, this assignment produces value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e5d99a9ca33001fb655f0d130af67ce/href">https://medium.com/media/4e5d99a9ca33001fb655f0d130af67ce/href</a></iframe><p>In this code block, the value of <strong>a</strong> is assigned to <strong>p</strong> as below.</p><blockquote>p = a</blockquote><p>In C, as mentioned, the assignment operator produces a value that is assigned value. In this case, the value of <strong>t </strong>will be 10. Therefore, the printed value is 10.</p><p>However, in Go, the assignment operator is not an expression. ⚠️</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/432fc4edc968deca6432a408d5b9e7b0/href">https://medium.com/media/432fc4edc968deca6432a408d5b9e7b0/href</a></iframe><p>If we try to compile this code block, we will get a syntax error. Because the assignment operator is not expression.</p><p>To sum up, expression and statement are an important part of software language. To understand the difference between these two terms helps us to fathom the intention of the language.</p><p><strong>References</strong>:</p><ul><li><a href="https://talks.golang.org/2012/splash.article#TOC_16">Go at Google: Language Design in the Service of Software Engineering</a></li><li><a href="https://golang.org/">The Go Programming Language</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89e74596e546" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Mapping Libraries — Part II]]></title>
            <link>https://medium.com/iyzico-engineering/using-mapping-libraries-part-ii-a2ce6178ba1?source=rss-25eeef4d8f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/a2ce6178ba1</guid>
            <category><![CDATA[mapping]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Güven Seçkin]]></dc:creator>
            <pubDate>Tue, 04 Aug 2020 10:27:10 GMT</pubDate>
            <atom:updated>2020-08-04T10:34:10.432Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java Bean Mapping — Usage of Mapping Libraries, Part II</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*bY5tknhQVmddPh5r8I_B-A.png" /></figure><p>In the first part of this article, I introduced the idea of mapping objects and how we can map them. Instead of coding each line of mapping process by ourselves, we may prefer using a library to automate it.</p><p>Let’s find a new library for mapping our objects.</p><p><strong>Model Mapper</strong></p><p>Our first library is going to be Model Mapper. Let’s dive into the idea behind this library.</p><p>Model Mapper consists of two processes Matching Process and Mapping Process. The matching process is understanding which field will be mapped to the other one. The mapping process is mapping source to destination.</p><p>First, we should add Model Mapper to our project as a bean.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/635da34866389e3a7b88f34e3fdf0436/href">https://medium.com/media/635da34866389e3a7b88f34e3fdf0436/href</a></iframe><p>When we inject Model Mapper to our service, spring will give us this bean.</p><p>Let’s map our object using Model Mapper. I will write some tests to be an example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f13dc97b2eed592141c3bc02b5d9f20/href">https://medium.com/media/0f13dc97b2eed592141c3bc02b5d9f20/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6052952dcf9bb696fdcd0bd1d8c95437/href">https://medium.com/media/6052952dcf9bb696fdcd0bd1d8c95437/href</a></iframe><p>As seen above, new fields have been added to the User entity and User Request Object in order to give different examples.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d756685e0852404838a43d6765fa54f/href">https://medium.com/media/6d756685e0852404838a43d6765fa54f/href</a></iframe><p>There is a map function that belongs to the Model Mapper. The first argument of the function is source object and the last parameter is destination class information. With these parameters, Model Mapper is able to understand which object will be converted to the other one.</p><p>There is also a nice feature. When you check the token field, you will realize that this type of field is different in both objects. The token is boxed long type in User Request whereas that is String type in User entity. The Model Mapper can also be used to overcome this kind of conversions.</p><p>How does model mapper achieve this conversion? 🤔</p><p>There is a term called NameTransformers which is used for extraction field name from getter and setter functions. Model Mapper omits “get” keyword from the source object and “set” keyword from the destination object. In our case, the username is extracted from its getter and setter in order to obtain a pure field name. After extracting, NameTokenizers are used to tokenize properties for matching.</p><p>We can customize our mappings with some configurations which are represented by the Model Mapper. To do this, we have TypeMap.</p><p>TypeMap is an expression map that helps us to customize our mappings. Let’s see the usage of TypeMap.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e617918b633f61fc8216d43b6fff3b1/href">https://medium.com/media/6e617918b633f61fc8216d43b6fff3b1/href</a></iframe><p>TypeMap should be added to Model Mapper in the configuration part. Therefore, when we inject the Model Mapper, this mapping can be used.</p><p>At first, we need to create TypeMap with source and destination classes.</p><p>Then we can create a map using getter-setter methods. If you confuse double columns, you may want to consider checking the instance method reference in java. With this approach, we can extend Model Mapper mapping skills. In this case, It was explicitly declared that City should be mapped to the Address field.</p><p>Now, we have learned how can we create a mapping table. However, what if we want to add more functionality during the mapping process rather than just map them?</p><p>We may need to save our company field with a “Company” keyword. To do this, there should be a converter that adds Company keyword to the end of the company.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c87c188a95c38f0096efcf9b4bfe0331/href">https://medium.com/media/c87c188a95c38f0096efcf9b4bfe0331/href</a></iframe><p>In addition to TypeMap, the Model Mapper also has its own Converter. Thanks to this Converter, we are able to add more functionality during the mapping process. In this case, It clearly indicates that, while mapping companies, the converter should be used instead of default one. You may also prefer skipping some fields for mapping or proving destination values from yourself. For more detail, please check the article.</p><p><a href="http://modelmapper.org/user-manual/property-mapping">Property Mapping</a></p><p><strong>Performance Of Mapping Libraries</strong></p><p>As a programmer, we are not only responsible for clean and understandable code but also for effective and high-performance code. In an interview, I came across this kind of question. After a short search, I found an article about the performance of mapping frameworks. At first glance, it was obvious that Model Mapper has a problem in terms of performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rp9m5jr8SYtmPrUuLwRYhg.png" /><figcaption>Less is better</figcaption></figure><p>After this disappointment, I had to find a new library which does not only helps to map but also be high performing. At the top of the table, there is one more mapping library which is MapStruct.</p><p>What differences between Model Mapper and MapStruct make such a noticeable performance difference? 🤔</p><p>Model mapper uses reflection for mapping. This process is expensive and impacts our performance. In contrast, MapStruct uses a compile-time code generation approach. With this approach, we let the library generate the code for us. When we build our codes, MapStruct will generate codes according to our definitions. Let’s dive into a new library!</p><p><strong>MapStruct</strong></p><p>As mentioned, MapStruct generates mapping codes at the compile time. Therefore, it is the same code that is written by us as performance.</p><p>Well, How MapStruct generate these codes at the compile time? 🤔</p><p>Annotation processing!</p><p>Annotation processing is integrated into the java compiler. Passing processor flag to javac, we are able to invoke annotation processors. The processor is only able to generate new source files rather than modifying existing sources. If you are interested in the topic, I suggest checking this book.</p><p>“<em>Core Java Volume 2, Advanced Features at Chapter 8.6.3 Using Annotation to Generate Source Code”.</em></p><p>MapStruct uses this approach in order to generate our mapping codes. Now let’s see some code.</p><p>I would like to recap our Request DTO and the Entity object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f0f21fa35d49fca84b79ce6ad397b2f/href">https://medium.com/media/8f0f21fa35d49fca84b79ce6ad397b2f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa80a7bfb4d1cf3e3ae96a1e7d59de05/href">https://medium.com/media/aa80a7bfb4d1cf3e3ae96a1e7d59de05/href</a></iframe><p>These are our Mapping objects. Basically, in order to define our mapping logic, we should define an interface. MapStruct will implement this interface and generate our concrete methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99466be36a668fda3b2e127e22e2b5fc/href">https://medium.com/media/99466be36a668fda3b2e127e22e2b5fc/href</a></iframe><p>We define our mappings with abstract methods. As could be seen, we should also annotate our interface as “Mapper” and component model also should be added for dependency injection.</p><p>After the compilation, there will be some concrete classes under <em>target/generated-sources/annotations/…</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f0b4bc4b9f91b9ff704237ca271ea5f/href">https://medium.com/media/0f0b4bc4b9f91b9ff704237ca271ea5f/href</a></iframe><p>This code is generated by MapStruct according to our definitions. The library can understand the source and destination from the parameter and the return values. Besides, we are able to also implement list mapping easily. List conversions will use one to one mapping into a for-loop.</p><p>It also adds a component annotation for spring because we explicitly declared that this component belongs to spring.</p><blockquote>componentModel = “spring”</blockquote><p>Now, let’s add more functionality to our mapper.</p><p>As could be seen in the object, I added a new field called Status. The purpose of this field is to keep the user status. When the user register, we should initialize the user with active status. You may want to achieve this during the mapping process.</p><p>After mapping all parts, we also add some definitions. To do this we have an annotation offered by MapStruct.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27dc7ebc0a21e5092baf0f6456a48056/href">https://medium.com/media/27dc7ebc0a21e5092baf0f6456a48056/href</a></iframe><p>As of Java 8, we are able to add concrete methods to our interfaces. To do this, we use static or default methods.</p><p>With the AfterMapping annotation, this function will be called at the end of the mapping process from the concrete class. We should also add MappingTarget annotation to indicate which object is targeted. Therefore, after the mapping, we can initialize the Status field with the default value.</p><p>Let’s look at another example. Suppose that we should save our companies with the Company keyword to the database.</p><p>How can we achieve this kind of mapping in MapStruct? 🤔</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3ab885d4f267f45e5231eb5c6371147/href">https://medium.com/media/c3ab885d4f267f45e5231eb5c6371147/href</a></iframe><p>To do this, we should add Mappings annotation which could be consist of many mappings. In the mapping annotation, we should add source and the target value. Then, we can define our own intent during the mapping process. In this case, we are able to use Named annotation in order to give a specific name to our default function. After that, we are able to indicate our logic to the mapping. In the concrete class, the function will be called and set the return value of the function to the Company field as below.</p><blockquote>user.setCompany( mapCompany( userRequest ) );</blockquote><p>With this approach, you are able to define your own mappings rule for specific fields.</p><p>We can also define some mappings for specific types. I mean, for all Long to String conversions, we may need a specific mapping for them.</p><p>To do this, we have to add <em>uses</em> parameter to the Mapper annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98b0610565cced2862de4d41144fa013/href">https://medium.com/media/98b0610565cced2862de4d41144fa013/href</a></iframe><p>As could be seen above, Token Mapper was used for mappings. You are able to add more classes inside of curly brackets.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0b1a63663724aa15e1ed78d1423e864/href">https://medium.com/media/d0b1a63663724aa15e1ed78d1423e864/href</a></iframe><p>In this case, all Long to String field conversions, this concrete method will be used. MapStruct is able to understand this conversion from the parameter type and the return value type of the method.</p><p>It is time to map the User entity to the User Response object. Let’s see different examples!</p><p>Suppose that, we have to add another parameter to our mapper function because we would like to set this parameter to the destination field.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0ff3f241a9a35ec2273289912e1c1db/href">https://medium.com/media/d0ff3f241a9a35ec2273289912e1c1db/href</a></iframe><p>As you may have noticed, AccountType has been added to the response object. How could we pass AccountType as a parameter to our mapper function like below?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd27a40b805f766ec9f187c0f3bd6c97/href">https://medium.com/media/cd27a40b805f766ec9f187c0f3bd6c97/href</a></iframe><p>To achieve this, we have to learn one more Annotation. Context!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c4ceb82c87fb5bebd2d8a62d6cd97db/href">https://medium.com/media/7c4ceb82c87fb5bebd2d8a62d6cd97db/href</a></iframe><p>We can add more parameters to our mapper function with Context annotation. With AfterMapping, AccountType was able to set as in the example! Thanks to Context annotation, we are able to bind parameters to each other. Instead of setting statically, this approach is recommended.</p><p>Wait!</p><p>This is an abstract class! Could we use abstract classes instead of interfaces?</p><p>The answer is yes. As of Java 8, we are able to use interfaces instead of using abstract classes because of default and static methods. There may be one reason for using abstract classes rather than using interfaces. If we have to inject other services to our mapper, we should use abstract classes.</p><p>During the mapping process, we may need to use other services for retrieving data. In our case, there is a country service which consists of one function. With this function, we are able to retrieve the country name from the country id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/681ebd73700c69650a0f2ea233644ca5/href">https://medium.com/media/681ebd73700c69650a0f2ea233644ca5/href</a></iframe><p>For now, adding County keyword to the end of country ID will be enough.</p><p>How could we inject this service to our mapper?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac9b1810172b97353739cbfde7c6a3c9/href">https://medium.com/media/ac9b1810172b97353739cbfde7c6a3c9/href</a></iframe><p>There is one more parameter that belongs to Mapping annotation. We are able to write our own expressions manually in this field. For country name mapping, this expression will be used.</p><p>For two reasons, I did not find this approach sufficient. First off, Autowire was used for dependency injection which is field injection. The other reason is expression seems too manual. We have to write all expression as a String. It is prone to making mistakes.</p><p>Fortunately, If there is a compile-time error in our mapping definitions, we will produce an error.</p><p>You may also consider checking more usage of these mapping libraries. Links have been added to the references.</p><p>For all codes and configurations, the <a href="https://github.com/guvense/BEAN-MAPPING-L">GitHub URL</a> has been attached.</p><p><strong><em>References</em>:</strong></p><ul><li><a href="http://modelmapper.org/user-manual/how-it-works/">Model Mapper How It Works</a></li><li><a href="http://modelmapper.org/user-manual/configuration/">Model Mapper Configuration</a></li><li><a href="https://www.baeldung.com/java-performance-mapping-frameworks">Performance of Java Mapping Frameworks</a></li><li><a href="https://mapstruct.org/">MapStruct</a></li><li><a href="https://mapstruct.org/documentation/stable/reference/html/">MapStruct 1.3.1 Final Reference Guide</a></li><li>Effective Java by Joshua Bloch: Consider Static Factory Methods instead of constructions Chapter 2 Item 1</li><li>Core Java Volume 2 Advanced Features by Cay S. Horstmann Chapter 8.6.1 Annotation Processors</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2ce6178ba1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/iyzico-engineering/using-mapping-libraries-part-ii-a2ce6178ba1">Using Mapping Libraries — Part II</a> was originally published in <a href="https://medium.com/iyzico-engineering">iyzico.engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Bean Mapping — Part I]]></title>
            <link>https://medium.com/iyzico-engineering/java-bean-mapping-part-i-ecbbae42e64d?source=rss-25eeef4d8f67------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecbbae42e64d</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[mapping]]></category>
            <dc:creator><![CDATA[Güven Seçkin]]></dc:creator>
            <pubDate>Tue, 04 Aug 2020 10:25:35 GMT</pubDate>
            <atom:updated>2020-08-04T11:14:38.286Z</atom:updated>
            <content:encoded><![CDATA[<h3>Java Bean Mapping — Introduction, Part I</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*iXSDhVFdOyuX3Ryvex0fkg.png" /></figure><p>During the program execution, data can be converted to different data types or different objects. The reason behind this conversion can be data hiding or a business decision.</p><p>For instance, let’s consider we are working on an application and suppose the first step of this application is to register new users, one of the data points that we want to represent can be the age of the user. What kind of user information helps us to represent user age dynamically? If we can save the age statically, It would depend on the year that the that was submitted. However, when we save the birth date of the user, It can easily represent age with a date difference conversion. To do this, we need to convert this data.</p><p>This is why there should be many objects for each layer. First, we should have a request object that helps us to get data from users. After this operation, we should return an object in order to represent some information to the request sender. The information can either be updated with successful status or updated data. To achieve this, we need a response object. These object types are request and response called DTO (Data Transfer Object). The idea behind this abstraction is to give some specific intent to these objects.</p><p>DTO’s should not contain any business logic, they should be used only for transferring data between two applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*MKcZzSHzNlvekw8J59Jwrw.png" /></figure><p>In this article, I’ am going to show how can these objects convert each other in different ways.</p><p>The first part of the article consists of best practices and an introduction to the bean mapping. For the usage of mapping libraries, you may consider starting from the “Using Mapping Libraries” section.</p><p><em>For advanced programmers, the second part of the article is more useful!</em></p><p>Let’s define a model that consists of user information. Lombok was used in order to avoid complexity.</p><p><em>Lombok is a compile-time annotation that minimizes our code size.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c484508edecad94d13c88458907cbce4/href">https://medium.com/media/c484508edecad94d13c88458907cbce4/href</a></iframe><p>To save these fields to our database, we need data that comes from real users. To do this, we should offer a new object for our client as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13839c292ad325e09147a03270b6ac0b/href">https://medium.com/media/13839c292ad325e09147a03270b6ac0b/href</a></iframe><p>To convert a User Request object to User Entity, we need to initialize our entity with data that comes from our request object. How could we initialize our objects?</p><p>Construction!</p><p>Well, let’s add two constructions to our user entity.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68e329cb04778449ad68771966fd6b78/href">https://medium.com/media/68e329cb04778449ad68771966fd6b78/href</a></iframe><p>As seen above, we have two different construction to initialize our user object. The company field will be optional because not all of our users might not work in a company.</p><p>However, these codes seem not well defined. At first glance, a programmer was able to not understand the intents of these two constructions. Besides, if we have lots of optional fields, it would be construction hell.</p><p>In fact, this situation has a name.</p><p><strong>Telescoping Construction!</strong></p><p>There are many anti-patterns that are used by many developers in order to save the day. However, programmers should avoid these kinds of patterns so that they are able to write clean and understandable code.</p><p>To find a solution, we need a better approach that comes from Effective Java.</p><p>The first item of the second chapter of the book is covering our needs.</p><p>“Consider static factory methods instead of constructions”</p><p>From this suggestion, we may consider creating static methods in order to define our intent. This pattern called <strong>Static Factory Method.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d650975f057b9a041cbc6f05c1ff5ae1/href">https://medium.com/media/d650975f057b9a041cbc6f05c1ff5ae1/href</a></iframe><p>There could be two reasons for this approach.</p><p>First, we define our intention explicitly so that a programmer who uses our function, can understand our intent from the function’s names.</p><p>Second, we hide our construction and we could make some changes to data before initializing our user object using constructions. For instance, we may prefer to join the first name and last name as a full name.</p><p>After that, we could write our converter class with static factory methods. Thanks to well well-thought-out function names, the clients can easily use our functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0bdf03a3cec395f93f56877dc69e8de/href">https://medium.com/media/b0bdf03a3cec395f93f56877dc69e8de/href</a></iframe><p>And then we inject converter class to our service layer in order to convert User Request to the User entity.</p><p>Before moving onto mapper libraries, there is a better way to convert our object.</p><p>There is one more way for mapping, we can use the<strong> Java Bean Convention</strong>. This convention explicitly declared that we have to access private fields with special functions. These are called getters and setters. Using these special functions, we can convert our objects easily.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40937b7198dc60a972f51b5dc235bd41/href">https://medium.com/media/40937b7198dc60a972f51b5dc235bd41/href</a></iframe><p>In this example, the User Request object is passed as a parameter to our converter function. Then fields that come from the request object are mapped to our entity using getter and setter functions. After that, this component could be injected into the service layer for converting operation.</p><p>In the next section, mapping libraries will be introduced and examples will be given about the usage.</p><p><a href="https://iyzico.engineering/using-mapping-libraries-part-ii-a2ce6178ba1">Using Mapping Libraries — Part II</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecbbae42e64d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/iyzico-engineering/java-bean-mapping-part-i-ecbbae42e64d">Java Bean Mapping — Part I</a> was originally published in <a href="https://medium.com/iyzico-engineering">iyzico.engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>