Learning to Love Ember’s Dependency Injection

Ember is an opinionated framework. Like other such frameworks, it takes a while to learn how the pieces are designed to fit together and how you can structure your code to be harmonious with it, rather than fight it.

What I’ve found is that often, it helps to think outside the boxes ember provides you: controllers, routes, components, etc. Ember’s dependency injection is a great tool for helping you with this.

The Basics

Objects are named in Ember’s dependency injection container are given a full name that consists of two parts: the type of object, and a name. For example, route:application would be the name for your ApplicationRoute. Or controller:question.edit for your QuestionEditController. You’re not limited to Ember’s predefined categories, however; you are free to make up your own type. Read more on Learning to Love Ember’s Dependency Injection…

This Code Is Untestable! (Part 2, for Developers)

In my last post, I argued that “untestable code” is code that cannot be efficiently unit tested. Today I’d like to walk through my top five anti-patterns that make writing unit tests painful. If you aren’t in the habit of writing unit tests, hopefully these tips will help you build more testable code and see how test-driven development can help drive clean design.

1. Lack of Dependency Injection

Let’s start by considering a worst-case single line method:

class CrisisHandler
  def code_one
    MissileControl.new.launch_nukes
  end
end

What’s so bad about this code? Well, let’s assume that this is a pretty important feature to get right, and that your code had better work as expected. We definitely want to be able to validate this method. Read more on This Code Is Untestable! (Part 2, for Developers)…

Generating Files with JavaScript and Ember.js

My colleague Jason Porrit recently wrote about loading and processing files with Ember.js. Today, I’ll cover two techniques for going in the opposite direction: generating files with JavaScript in the browser. I used these techniques while working with Jason on an Ember.js app, so my examples are geared toward Ember.js, but the techniques themselves can be used with any JavaScript framework (or no framework at all).

In a typical web app, files are generated on a server and then downloaded by the browser (or other client). We decided to turn that idea on its head, shifting the responsibility to the browser. This was a clear win in the case of our Ember.js app because we already had nearly all of our presentation logic in the client-side JavaScript code. Our server is merely responsible for providing data upon request. It’s the responsibility of the client-side code to present that data to the user, whether in a web page or as a downloadable file. Read more on Generating Files with JavaScript and Ember.js…

Practical Abstraction in Ember.js

Ember provides a LOT of powerful abstractions. With all that power, it can be difficult to know what tools will best add meaningful functionality to your app while gaining leverage for the future. The Ember docs will walk you through the roles of templates, controllers, models, and all the other pieces you’d expect in a modern JavaScript app framework, but what they don’t cover are tactics for using some of the deeper concepts of Ember (and functional JavaScript programming) to help you write less, but better, code.

Here are a few tools we’re using on my current project, a non-trivial Ember.js application, to do just that.

Read more on Practical Abstraction in Ember.js…

Conject – Modern Dependency Injection in Ruby

Dependency Injection is relevant in Ruby. I say this because solving problems with highly decomposed systems of collaborating, narrow-purpose objects is still the best way I know, if I want to drive my code with tests and be able to change it later. DI tools help enable this type of design by carrying the burden of object instantiation and locking it outside our actual domain code.

(There’s a strange history of opinions revolving around DI in Ruby, and they’re worth discussing… sometime soon.)

I’ve been having a hard time finding a good DI tool for Ruby. I’m ready to move forward from DIY and enjoy some of the great conveniences that tools like Guice provide, such as automatic instantiation of object trees based on constructor and type info. So I wrote Conject, and though it’s still young, it’s working and showing promise.

  • gem install conject

Read more on Conject – Modern Dependency Injection in Ruby…

The Guava EventBus on Guice

Guava’s EventBus provides a publish-subscribe event mechanism which allows objects to communicate with each other via the Observer Pattern. The EventBus shies away from the traditional “Event Listener” pattern seen in Java where an object implements a particular interface and then explicitly subscribes itself with another object.

In a recent project we chose to use the EventBus in conjunction with Guice (a dependency injection library) and have had a lot of success with it. Specifically, objects in our system only have to express what events they care about without being required to explicitly register with the EventBus or any other object. Read more on The Guava EventBus on Guice…

Meta Class Dependency Injection in Objective-C

In a previous post we announced Objection, a dependency injection framework created for Objective-C. Since then, we have been actively using Objection in our iOS projects and we have added a few new features: Protocol Bindings, Eager Singletons, and Meta Class bindings. Meta class bindings are useful when there is an external dependency that is implemented using only class methods.

Read more on Meta Class Dependency Injection in Objective-C…