Nick Navarro

1608173402

Hilt ViewModels and Dependency Injection

In this video I show you how to do dependency injection into viewmodels using Hilt. In the previous video I showed you how to do field injection into an activity. Constructor injection is preferable over field injection whenever possible because then you know exactly what a class needs to be built.

Hilt makes it very easy to instantiate viewmodels in Fragments and activities. You can use the “by viewmodels()” method to maintain a viewmodel instance in a fragment. Or use the “by activityViewModels()” method to maintain a viewmodel instance between multiple fragments sharing the same host activity.

Code: https://github.com/mitchtabian/MVVMRecipeApp/tree/hilt-viewmodels

Subscribe : https://www.youtube.com/channel/UCoNZZLhPuuRteu02rh7bzsw

#web-development #hilt

What is GEEK

Buddha Community

Hilt ViewModels and Dependency Injection

Getting Started with ASP.NET Core Dependency Injection

I’ve lately been feeling my way around getting an actual, production-ready ASP.NET Core app developed, and one of the features I’ve really been digging (one of many) is native support for Dependency Injection (DI). DI is huge in modern web apps, so making it the default for ASP.NET

#asp.net core #asp.net core dependency injection #dependency #injection #programming

Seamus  Quitzon

Seamus Quitzon

1603763160

ActiveInject. Fast and Lightweight Dependency Injection Library

What is ActiveInject?

ActiveInject is a lightning-fast and powerful dependency injection library. It has a lot of tools and features to offer: support for nested scopes, singletons and transient bindings, modules, multi-threaded, and single-threaded injectors.

At the same time, it’s thoroughly optimized with all the dependencies graph preprocessing performed at startup time. According to the benchmarks, in some scenarios, ActiveInject is 5.5 times faster than Guice and hundreds of times faster than Spring DI. You can check the benchmark sources here.

ActiveInject_ is an independent technology of ActiveJ platform. It has no third-party dependencies on its own and can be used as a stand-alone DI library._

Getting Started

Let’s try the library out and bake some virtual cookies using ActiveInject. A cookie requires the following ingredients: FlourSugar and Butter. These ingredients form a Pastry which can be baked into a Cookie. Assume each of these entities has a POJO. Let’s start with a basic example:

public void provideAnnotation() {
  Module cookbook = new AbstractModule() {
     @Provides
     Sugar sugar() { return new Sugar("WhiteSugar", 10.f); }

     @Provides
     Butter butter() { return new Butter("PerfectButter", 20.0f); }

     @Provides
     Flour flour() { return new Flour("GoodFlour", 100.0f); }

     @Provides
     Pastry pastry(Sugar sugar, Butter butter, Flour flour) {
        return new Pastry(sugar, butter, flour);
     }

     @Provides
     Cookie cookie(Pastry pastry) {
        return new Cookie(pastry);
     }
  };

  Injector injector = Injector.of(cookbook);
  injector.getInstance(Cookie.class).getPastry().getButter().getName());
}

Here we’ve created an AbstractModule named cookbook that contains all the required bindings, or “recipes”, for the ingredients. Call Injector.getInstance method to get an instance of the Cookie.

How does it work from the inside? Injector provides all the required dependencies for the component recursively traversing the dependencies graph in a postorder way. So it first created SugarButter and Flour, the next was Pastry, and finally a Cookie.

#java #dependency injection #java library #dependency injection tutorial

Dependency Injection Implementation in Core Java

Implement your own lightweight Dependency Injection in core Java without using any framework.

Overview

This article will guide you to understand and build a lightweight Java application using your own Dependency Injection implementation.

Dependency Injection … DI… Inversion Of Control…IoC, I guess you might have heard these names so many times while your regular routine or specially interview preparation time that you wonder what exactly it is.

but if you really want to understand how internally it works then continue reading here.

#java #tutorial #dependency injection #inversion of control #dependency injection implementation in core java #core java

Nick Navarro

1608173402

Hilt ViewModels and Dependency Injection

In this video I show you how to do dependency injection into viewmodels using Hilt. In the previous video I showed you how to do field injection into an activity. Constructor injection is preferable over field injection whenever possible because then you know exactly what a class needs to be built.

Hilt makes it very easy to instantiate viewmodels in Fragments and activities. You can use the “by viewmodels()” method to maintain a viewmodel instance in a fragment. Or use the “by activityViewModels()” method to maintain a viewmodel instance between multiple fragments sharing the same host activity.

Code: https://github.com/mitchtabian/MVVMRecipeApp/tree/hilt-viewmodels

Subscribe : https://www.youtube.com/channel/UCoNZZLhPuuRteu02rh7bzsw

#web-development #hilt

Myriam  Rogahn

Myriam Rogahn

1598662080

Python Dependency Injection

Writing clean, maintainable code is a challenging task. Fortunately, there are many patterns, techniques, and reusable solutions available to us to make achieving that task much easier. Dependency Injection is one of those techniques, which is used to write loosely-coupled yet highly-cohesive code.

In this post, we’ll show you how to implement Dependency Injection as you develop an app for plotting historic weather data. After developing the initial app, using Test-Driven Development, you’ll refactor it using Dependency Injection to decouple pieces of the app to make it easier to test, extend, and maintain.

By the end of this post, you should be able to explain what Dependency Injection is and implement it in Python with Test-Driven Development (TDD).

What is Dependency Injection?

In software engineering, Dependency Injection is a technique in which an object receives other objects that it depends on.

  1. It was introduced to manage the complexity of one’s codebase.
  2. It helps simplify testing, extending code, and maintenance.
  3. Most languages that allow for the passing of objects and functions as parameters support it. You hear more about Dependency Injection in Java and C#, though, since it’s difficult to implement. On the other hand, thanks to Python’s dynamic typing along with its duck typing system, it’s easy to implement and thus less noticeable. Django, Django REST Framework, and FastAPI all utilize Dependency Injection.

Benefits:

  1. Methods are easier to test
  2. Dependencies are easier to mock
  3. Tests doesn’t have to change every time that we extend our application
  4. It’s easier to extend the application
  5. It’s easier to maintain the application

For more, refer to Martin Fowler’s Forms of Dependency Injection article.

To see it in action, let’s take a look at a few real-world examples.

Plotting Historic Weather Data

Scenario:

  1. You’ve decided to build an app for drawing plots from weather history data.
  2. You’ve downloaded 2009 temperature by hour data for London.
  3. Your goal is to draw a plot of that data to see how temperature changed over time.

#injection #dependency #python