intravenous icon indicating copy to clipboard operation
intravenous copied to clipboard

Store a class instead of an instance

Open mathieug opened this issue 8 years ago • 10 comments

How to store a class instead an instance?

mathieug avatar Oct 18 '15 17:10 mathieug

I'm interested in this as well.

If i my module's factory returns a class, it is not possible to normally use the class. intravenous forces me to call .get(…) in every place where this class is used. subclassing is also made impossible by this.

derhuerst avatar Jan 29 '16 16:01 derhuerst

@mathieug Why would you want to do it anyways?

@derhuerst Define "normally" please. You can use the class directly via require anywhere. May be you should post some more code here.

tusharmath avatar Jan 29 '16 16:01 tusharmath

@tusharmath I just think intravenous does an unnecessary thing here. Besides limiting me in how to use the class, it also introduces more complexity in the code base. I'd like it to just return the class.

derhuerst avatar Jan 29 '16 17:01 derhuerst

I don't mean to self promote, but node-ioc is something that can help you create custom factories.

const IOC = require('node-ioc')
const container = new IOC()

class SampleClass {}
// register takes a factory function for SampleClass
container.register(() => SampleClass).as(SampleClass)
container.resolve(SampleClass) // returns the class and not the instance.

tusharmath avatar Jan 30 '16 08:01 tusharmath

I would like Intravenous to Store a class but return a NEW instance on every call to container.get('..') . As currently I get an entry point class in the controller the entry point class which is actually my service has dependencies on other classes and these classes have dependencies on some other class which are injected through their constructors. Intravenous creates the whole graph and gives me the service class. The service class processes the request and returns the result this all falls in line with the strategy of keeping controller as dumb as possible. After returning the result I would like to dispose the service class as some the library classes used by one of its dependencies is leaking. So I have defined "dispose" method on all the classes and in this method set its class variables reference to null and also call the external libraries close() or end() method where ever required. But once I dispose the service class intravenous call the dispose on its cached instance of service class and in the next request or parallel request the dependencies references are all null.

vipreshjha avatar Feb 01 '16 12:02 vipreshjha

Just like @tusharmath did, I will promote my own DI framework flacon.

derhuerst avatar Feb 01 '16 13:02 derhuerst

This is probably a good place to point out that I'd recommend you take a look at the libraries shamelessly promoted by the others :)

As you may be able to tell from the commit logs I'm not really actively developing this library anymore since I don't really have the need for it anymore.

sagacity avatar Feb 01 '16 13:02 sagacity

Out of curiosity, what is the reason for why you don't need this library anymore?

ghost avatar Feb 01 '16 14:02 ghost

let me guess, you too have moved to reactive programming ? :)

tusharmath avatar Feb 01 '16 14:02 tusharmath

Well, for server-side stuff I'm doing Java mostly nowadays and client-side I think ES6 modules are a pretty decent alternative, especially combined with React/Redux. You don't need dependencies accessible from everywhere, instead you only (mostly) need them in the reducers and then they're just a require away :yum:

sagacity avatar Feb 01 '16 14:02 sagacity