shock01 / oig

Playing around with ES6, MVVM and stuff like that
1 stars 1 forks source link

Dependency Injection Concept #21

Closed shock01 closed 9 years ago

shock01 commented 9 years ago

Let oig.context be the dependency context Let component by the generic type of injectable Let controller be a specialized type of component Let service be a specialized type of component

Let oig.context.controller be a method to register controllers Let oig.context.service be a method to register services Let oig.context.component be a method to register components

Controller Let default scope be prototype

Service Let default scope be singleton

Component Let default scope be singleton

Let name be name of controller Let function be resolvable of controller Let oig.contex.controller(name function) return Scope Let Scope have two methods: singleton and prototype to override implementation Eg. oig.context.controller(name, function).singleton() oig.context.controller(name, function).prototype() (Can prototype be used, ??, maybe instance())

Let API be fluent oig.context.controller(name, function).singleton().context().service(name, function).context().service(name, function)

Registration: When calling oig.context.controller it should register an instance in the oigLocator Let name be test Let resolver be DI aware resolver oig.context.controller(name, function) Should call oigLocator.register('controller.' + name, resolver)

DI Resolvers: DISingletonResolver DIInstanceResolver

Configuring dependencies Uses comments that can be preserved by eg. uglify js Comment parsing should only be done once (cache parsing) function myController(/*!inject:myService/myService)

shock01 commented 9 years ago

function /*!@controller/Test() { } Test.prototype = { handleEvent: function (/*!@inject('event')/event) {} }

NB. Not all browsers will return comments on toString

oig.diContext.bind('test').to(Test).asController(); oig.diContext.bind('test').to(Test).asService(); oig.diContext.bind('test').to(Test).asSingleton(); oig.diContext.bind('test').to(Test).asProto();

On bind get the TypeInfo of the instance (methods, contructor, args) Then on bootstrap / load let diContext call locator to register all types with a DIAware resolvable

shock01 commented 9 years ago

Comments like /# sourceMappingURL=/path/to/file.js.map will be preserved

Maybe use

function Test(serviceA, serviceB) { /# inject=Controller:serviceA, serviceB/ }

shock01 commented 9 years ago

Another option: function Test(serviceA, serviceB) { //# inject=Controller:serviceA, serviceB }