question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal for easier way to inject via DI into a view-model

See original GitHub issue

I find Aurelia to be super efficient in terms of development, but there is one thing that takes up a few seconds of my time is dependency injection by needing to specify a constructor and use passed in injectables as params, then alias them to the current class.

To inject the Event Aggregator, you do this at present (I use static inject, but many use the inject decorator):

import { EventAggregator } from 'aurelia-event-aggregator';
 
export class MyViewModel {
    static inject = [EventAggregator];

    constructor(ea) {
        this.ea = ea;
     }
}

I am proposing a new decorator which does what the above code does, but looks like this:

@injectable(EventAggregator)
export class MyViewModel {

}

Under the hood, things would work the same. The decorator would simply be specifying the injected instances the same way @inject does and then would be creating new properties on the class itself.

The proposed decorator would have two arguments:

  • injectable: For example EventAggregator - this is what you would pass to @inject() at present
  • alias (optional): This would allow you to overwrite the convention of taking the injectable class name and making it camelCased. If this is not specified in the case of EventAggregator it would be injected into your class as: this.eventAggregator = eventAggregator

This is an example of an alias being provided (overriding default convention):

@injectable(EventAggregator, 'ea')
export class MyViewModel {

}

The above example would inject the Event Aggregator into the class as this.ea.

Why?

We already have flexible injection options and if you’re a TypeScript user, you can @autoinject and can hoist params on the constructor to the class, but not everyone uses TypeScript and it still doesn’t solve the problem of needing to define a constructor, in many cases just to pass DI instances into the class.

This is a case of me being lazy and wanting to clean up my view-models, because in many cases I never do anything inside of a constructor other than pass in DI instances.

Caveats

From what I can discern, there would be no issues with something like this. Although, I would be surprised if this hasn’t been thought of before and perhaps there are technical challenges with doing such a thing preventing a decorator like this ever being feasible. Happy to hear from others smarter than myself to chime in whether or not what I am proposing is crazy or possible.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
stsjecommented, Dec 13, 2017

What you are describing is property injection. I don’t like the idea of implementing a new injectable decorator, but maybe instead we could do property injection like we know it from autofac, guice etc.?

TypeScript will be easy enough, since it already knows the type of the object

export class MyViewModel {
  @inject eventAggregator : EventAggregator;
  @inject({ resolver: Lazy }) httpClient : HttpClient;
}

JavaScript you have to tell the decorator the type

export class MyViewModel {
  @inject(EventAggregator) eventAggregator;
  @inject({ type: HttpClient, resolver: Lazy }) httpClient;
}
1reaction
EisenbergEffectcommented, May 2, 2018

Closing this. There’s a community plugin that hooks into DI and adds property injection via the inject decorator. https://github.com/devaur/aurelia-property-injection

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Inject ViewModel With Dagger & What Might Go Wrong
There are several methods. The easiest way is to inject view model providers into a factory and map them manually: Add this factory ......
Read more >
Android ViewModel - Manual Dependency Injection Made Easy
In their guide to manual dependency injection the Android team lays out approaches to manual DI for View Models. They offer both the...
Read more >
3 Ways to Tackle Assisted Injection for ViewModels
Embark on a journey that will let you manage assisted dependency injection for view models. I'll go through 3 working solutions.
Read more >
Using DI to Inject the Dependencies into ViewModel with MS ...
A simpler way for just vm's is to use a custom IViewModelFactory implementation. Basically: To resolve views => IViewLocator ...
Read more >
How to Inject ViewModel using Dagger 2 - TechYourChance
In this article, I'll show you how to inject ViewModel instances using Dagger 2 dependency injection framework.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found