AngularJS + OOP is kinda sexy feature to use

Hi, I'm successfully using OOP with AngularJs for some time already (first started with angularjs with oop inheritance in action), the provided approach allows you define your classes as angular services, which you can later extend or inherit from like that:

using the described approach gives you the ability to define classes that beautifully integrate into angular infrastructure. You get all sort of nifty features from two worlds - OOP and AngularJs. Dependency injection is free for your classes, and it makes your classes simple, allows putting a lot of boilerplate controller code into some base class that can be later reused.

However

AngularJs infrastructure blocks previously described approach from spreading it's wings on all 100%. The problem occurs when you try to define recursive class definitions (i.e. recursive aggregation), say you have two class definitions like Blog and Tag

3 Answers
3

A circular dependency is always the sign of mixing of concerns, which is a really bad thing. Miško Hevery, one of the authors of AngularJS, explains a nice solution on his awesome blog. In short, you probably have a third service hidden somewhere, which is the only part of your code really needed by the two others.

Blackhole, it's very interesting post you've provided, my case with Tag and Blog proves that Misko is right, because these are sort of database entities that relate one to another as N:M and in relational bases world this kind of situation is also resolved by representing third relational table that holds reference to both Tag and Blog such as BlogTags
–
Lu4Oct 13 '13 at 13:07

I'm trying to decouple existing logic, will se what it will lead to...
–
Lu4Oct 13 '13 at 13:51

So far so good... The flight is stable, described approach really helps with identifying problematic implementation points.
–
Lu4Oct 14 '13 at 19:44

I think that your approach is correct and having circular deps in code is kinda bad thing to do, so the answer goes to you
–
Lu4Nov 18 '13 at 23:48

@Lu4 did you solve this by adding a BlogTags factory that Blog and Tag get injected with? I'd really rather not add "joining table" classes into my code base if I can help it... If my only other option is circular dependencies using $injetor.get as you pointed out, I might just stick with that.
–
BrettDec 9 '14 at 7:04

I'm answering my own question just because I've found a technical way of resolving the issue that I have originally posted about. But before that, I strongly encourage you to use Blackhole's suggestion since it allows solving a broader set of problems which are usually caused by bad architecture. Please prefer using his approach first, and return to current one in case that you know what you are doing.

So here goes:

You can use $injector service and inject required definitions at run-time, which is legal from technical point of view, but again according to this post (just imagine that it is written in 2008), this is like a black magic, do that and it will strike you back: