<?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 Briankiraguri on Medium]]></title>
        <description><![CDATA[Stories by Briankiraguri on Medium]]></description>
        <link>https://medium.com/@briankiraguri?source=rss-37adf1e2518c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FphYAtc72DdIh4aN</url>
            <title>Stories by Briankiraguri on Medium</title>
            <link>https://medium.com/@briankiraguri?source=rss-37adf1e2518c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 21:32:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@briankiraguri/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[Dependency Injection (DI)]]></title>
            <link>https://medium.com/@briankiraguri/dependency-injection-di-d0412d8e9fb0?source=rss-37adf1e2518c------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0412d8e9fb0</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[ioc-container]]></category>
            <dc:creator><![CDATA[Briankiraguri]]></dc:creator>
            <pubDate>Fri, 21 Feb 2025 08:56:38 GMT</pubDate>
            <atom:updated>2025-02-21T09:19:31.473Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*BRUQL1nWf8V5Jrbtss-AjQ.jpeg" /></figure><p>In this blog, we’ll explore DI’s core concepts, its benefits and dive deep into its implementation in Java using Spring Boot.</p><p>It is a design pattern rooted in the Inversion of Control (IoC) principle. In traditional programming, a class is responsible for creating its own dependencies (e.g. instantiating objects it needs).</p><p>DI flips this, instead of a class creating its own dependencies, they are injected into it from an external source, typically at runtime decoupling the creation of objects from their usage, making code more modular, testable and maintainable.</p><h3>Key Concepts of Dependency Injection</h3><ol><li>Dependencies — These are the objects or services that a class needs to function.</li><li>Injector — The component responsible for creating and injecting the dependencies into the class.</li><li>Service — The class that provides the functionality that other classes depend on.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*e2jPH4Wi5PlLKjpzu-wQtA.jpeg" /></figure><p>Imagine a Car class that needs an Engine. Without DI, the Car would create it&#39;s own Engine.</p><pre>public class Car {<br>    private Engine engine = new Engine();<br>}</pre><p>With DI, the Engine is injected into the Car from an external source:</p><pre>public class Car {<br>    private Engine engine;<br> <br>    public Car(Engine engine) {<br>        this.engine = engine;<br>    }<br>}</pre><p>This decouples Car from specific Engine implementation, making it more flexible and easier to test.</p><h3>Benefits of Dependency Injection</h3><ol><li><strong>Loose Coupling</strong>: Dependencies are no longer hardwired, allowing components to evolve independently.</li><li><strong>Testability</strong>: You can inject mock objects during testing, isolating the unit under test.</li><li><strong>Reusability</strong>: Components become more modular and reusable across different contexts.</li><li><strong>Maintainability</strong>: Changes to dependencies (e.g., swapping an implementation) don’t require modifying the dependent class.</li><li><strong>Flexibility</strong>: Runtime configuration of dependencies becomes possible, enhancing adaptability.</li><li><strong>Lifecycle Management</strong>: DI containers can manage the lifecycle of dependencies, ensuring they are created, reused or disposed of correctly, which leads to better resource management.</li></ol><p>There are several methods to achieve Dependency Injection, and I’ll outline the most common ones below:</p><h3>1. Constructor Injection</h3><ul><li>Dependencies are provided to a class through its constructor. This is the most widely used and recommended method because it makes dependencies explicit and ensures the class cannot be instantiated without them.</li></ul><pre>// UserRepository.java<br>public interface UserRepository {<br>    String getUserData();<br>}<br> <br>// UserRepositoryImpl.java<br>@Component<br>public class UserRepositoryImpl() implements UserRepository{<br>    @Override<br>    public String getUserData() {<br>        return &quot;User data from repository&quot;;<br>    }<br>}<br> <br>// UserService.java<br>@Service<br>public class UserService {<br>    private final UserRepository userRepository;<br> <br>    @Autowired<br>    public UserService(UserRepository userRepository){<br>        this.userRepository = userRepository;<br>    }<br> <br>    public string fetchUser(){<br>        return userRepository.getUserData();<br>    }<br>}</pre><ul><li><strong>How it works</strong>: Spring detects the @Component and @Service annotations, creates beans, and injects UserRepository into UserService via the constructor.</li></ul><h4>Advantages:</h4><ol><li>Dependencies are immutable once set.</li><li>Easy to test since dependencies can be mocked or stubbed.</li><li>Clearly communicates required dependencies.</li></ol><h3>2. Setter Injection</h3><ul><li>Dependencies are injected through setter methods after the object is constructed. This allows for optional dependencies or changing dependencies at runtime.</li></ul><pre>// UserService.java<br>@Service<br>public class UserService {<br>    private UserRepository userRepository;<br> <br>    @Autowired<br>    public void setUserRepository(UserRepository userRepository){<br>        this.userRepository = userRepository;<br>    }<br> <br>    public string fetchUser(){<br>        return userRepository.getUserData();<br>    }<br>}</pre><ul><li><strong>How it works</strong>: Spring calls the setter after instantiating the bean.</li></ul><h4>Advantages:</h4><ol><li>Flexible, as dependencies can be changed after object creation.</li><li>Useful when dependencies are optional.</li></ol><h4>Disadvantages:</h4><ol><li>Dependencies can be left unset, leading to runtime errors (e.g., null pointer exceptions).</li><li>Less explicit than constructor injection.</li></ol><h3>3. Interface Injection</h3><ul><li>The dependency provides an injector method (usually defined in an interface) that the dependent class implements. The injector then passes the dependency to the class.</li></ul><pre>// UserService.java<br>public interface Injector {<br>    void injectUserRepository(UserRepository userRepository);<br>}<br> <br>@Service<br>public class UserService implements Injector{<br>    private UserRepository userRepository;<br> <br>    @Override<br>    private void injectUserRepository(UserRepository userRepository) {<br>        this.userRepository = userRepository;<br>    }<br> <br>    public string fetchUser(){<br>        return userRepository.getUserData();<br>    }<br>}</pre><ul><li><strong>How it works</strong>: Interface injection allows a class to receive its dependencies through a method defined in an interface, ensuring that an external injector provides the required dependency.</li></ul><h4>Advantages:</h4><ol><li>Highly flexible and decouples the injector from the dependent class.</li></ol><h4>Disadvantages:</h4><ol><li>Requires the dependent class to implement an interface, adding complexity.</li><li>Rarely used in modern frameworks compared to constructor or setter injection.</li></ol><h3>4. Field Injection</h3><ul><li>Uses annotations directly on fields.</li></ul><pre>// UserService.java<br>@Service<br>public class UserService {<br>    @Autowired<br>    private UserRepository userRepository;<br> <br>    public string fetchUser(){<br>        return userRepository.getUserData();<br>    }<br>}</pre><ul><li><strong>How it works</strong>: Spring uses reflection to inject the dependency directly into the field.</li></ul><h4>Advantages:</h4><ol><li>Concise and readable for small classes.</li></ol><h4>Disadvantages:</h4><ol><li>Harder to test (requires reflection or frameworks like Mockito), hides dependencies, and isn’t immutable. Not recommended for production code.</li></ol><h3>5. Java-Based Configuration</h3><ul><li>Instead of annotations, you can define beans programmatically.</li></ul><pre>@Configuration<br>public class AppConfig {<br>    @Bean<br>    public UserRepository userRepository() {<br>        return new UserRepositoryImpl();<br>    }<br> <br>    @Bean<br>    public UserService userService(UserRepository userRepository) {<br>        return new UserService(userRepository);<br>    }<br>}<br> <br>// UserService.java -- No annotations needed.<br>public class UserService {<br>    private final UserRepository userRepository;<br> <br>    public UserService(UserRepository userRepository) {<br>        this.userRepository = userRepository;<br>    }<br> <br>    public string fetchUser(){<br>        return userRepository.getUserData();<br>    }<br>}</pre><ul><li><strong>How it works</strong>: The @Configuration class tells Spring to use these methods to create and wire beans.</li></ul><h4>Advantages:</h4><ol><li>Explicit control, no classpath scanning, great for complex setups.</li></ol><h4>Disadvantages:</h4><ol><li>More verbose than annotations.</li></ol><h3>Conclusion</h3><p>Dependency Injection is a cornerstone of modern software design, and Spring Boot makes it both powerful and accessible in Java. From constructor injection to Java-Based Configuration, Spring offers flexibility to suit any project. Compared to other languages, Spring’s mature DI system stands out for its configurability and ecosystem support.</p><p>Whether you’re building a small app or an enterprise system, mastering DI will elevate your code’s quality. Try out the examples above in your next Spring Boot project, and experience the benefits firsthand!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0412d8e9fb0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Minimal Web Server in C]]></title>
            <link>https://medium.com/@briankiraguri/minimal-web-server-in-c-f02da1585e23?source=rss-37adf1e2518c------2</link>
            <guid isPermaLink="false">https://medium.com/p/f02da1585e23</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[web-server]]></category>
            <dc:creator><![CDATA[Briankiraguri]]></dc:creator>
            <pubDate>Sun, 30 Jun 2024 08:42:34 GMT</pubDate>
            <atom:updated>2025-02-21T09:18:29.969Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W-ScoiWupGSNNeXG" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I am excited to share one of the back-end challenging problems I solved recently, which is Building a Basic Web Server in C. This challenged my skills but improved my understanding of certain concepts associated with network programming and system calls. Let me walk you through the process, breaking down each step and explaining the key concepts.</p><p>The task was to create a basic web server capable of handling HTTP requests and serving files. While this might seem straightforward, implementing it in C requires a deep understanding of system calls and network programming concepts.</p><h3>The Implementation</h3><p>Let’s break down the code and explain each crucial component:</p><h3>1. Setting Up the Socket</h3><pre>int s = socket(AF_INET, SOCK_STREAM, 0);</pre><p>The socket() function creates an endpoint for communication. It returns a file descriptor that refers to that endpoint. The parameters specify:</p><ul><li>AF_INET: Use IPv4 Internet protocols</li><li>SOCK_STREAM: Provide sequenced, reliable, two-way, connection-based byte streams</li><li>0: Use the default protocol (TCP in this case)</li></ul><h3>2. Binding the Socket</h3><pre>struct sockaddr_in addr = {<br>    AF_INET,<br>    0x8813,<br>    0<br>};<br>bind(s, &amp;addr, sizeof(addr));</pre><p>The bind() function assigns the address specified by addr to the socket. In this case, we&#39;re binding to all available interfaces (0.0.0.0) on port 5000 (0x8813 in hexadecimal).</p><h3>3. Listening for Connections</h3><pre>listen(s, 10);</pre><p>The listen() function marks the socket as passive, meaning it will be used to accept incoming connection requests. The second parameter (10) specifies the maximum length of the queue of pending connections.</p><h3>4. Accepting Connections</h3><pre>while(1) {<br>    int client_fd = accept(s, 0, 0);<br>    handle_request(client_fd);<br>    close(client_fd);<br>}</pre><p>This infinite loop continually accepts new connections using the accept() function. For each new connection, it calls handle_request() to process the client&#39;s request, then closes the connection.</p><h3>5. Handling Requests</h3><pre>void handle_request(int client_fd) {<br>    char buffer[256] = {0};<br>    <br>    /* Check if recv fails or connection is closed */<br>    ssize_t bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);<br>    if (bytes_received &lt;= 0)<br>        /* Handle error or closed connection */<br>        return;<br>    <br>    /* Ensure we have enough data for the offset */<br>    if (bytes_received &lt; 6) /* 5 for offset + 1 for atleast some content */<br>        return;<br>    <br>    /* Safely extract filename */<br>    char* f = buffer + 5;<br>    char* space = strchr(f, &#39; &#39;);<br><br>    if (space == NULL)<br>        /* No space found, invalid request */<br>        return;<br>    *space = &#39;\0&#39;; /* Null terminate the filename */<br>    <br>    /* Open file with error checking */<br>    int opened_fd = open(f, O_RDONLY);<br>    if (opened_fd &lt; 0)<br>        /* File open failed */<br>        return;<br><br>    sendfile(client_fd, opened_fd, NULL, 256);<br>    close(opened_fd);<br>}</pre><p>This function does the following:</p><ol><li>Receives the client’s request using recv()</li><li>Extracts the requested filename from the HTTP GET request</li><li>Opens the requested file using open()</li><li>Sends the file contents to the client using sendfile()</li><li>Closes the file</li></ol><h3>Challenges and Learnings</h3><p>Creating this minimalist web server was not without its challenges. Some key learnings include:</p><ol><li><strong>Understanding System Calls</strong>: Working directly with system calls like socket(), bind(), and listen() gave me a deeper appreciation for the low-level operations involved in network programming.</li><li><strong>Performance Considerations</strong>: Using sendfile() for file transfer demonstrates an optimization technique, as it performs the data copying entirely within the kernel space</li></ol><h3>Conclusion</h3><p>Building this minimalist web server in C was a challenging yet rewarding experience. It reinforced my passion for back-end development and my desire to understand the intricacies of system-level programming.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f02da1585e23" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>