TSFunq
Funq Dependency injection container port for TypeScript
Installation
You can get the latest release and the type definitions using npm:
npm install tsfunq --save
Create Dependencies
Create the container and register a dependency
; ; container.registerTypeScriptDeveloper, new TypeScriptDeveloper;
Resolve a dependency
;
Register and resolve a dependency using a key
;; container.register"TypeScriptDeveloper", new TypeScriptDeveloper;developer = container.resolve"TypeScriptDeveloper";
Register and resolve a complex dependency
container.registerPerson, ; ;
Register an instance
;; container.registerInstancedeveloperInstance;resolved = container.resolveTypeScriptDeveloper;
Register and resolve a named dependency
; container.registerNamedTypeScriptDeveloper, "foo", new TypeScriptDeveloper;developer = container.resolveNamedTypeScriptDeveloper, "foo";developer !== container.resolveTypeScriptDeveloper; /// true
Try to resolve a dependency
The container will throw an exception if it will not be able to resolve a dependency.
You can use the tryResolve
or tryResolvedNamed
functions in order to avoid an exception:
;; if developer ; if namedDeveloper
Register and resolve a named dependency with arguments
You can register a dependency that accepts 1 to 10 parameters
; container.registerOneArgumentClass,new OneArgumentClassarg1;container.registerTwoArgumentsClass,new TwoArgumentsClassarg1, arg2;container.registerThreeArgumentsClass,new ThreeArgumentsClassarg1, arg2, arg3;container.registerFourArgumentsClass,new FourArgumentsClassarg1, arg2, arg3, arg4;
And resolve the dependencies using the appropriate arguments
container.resolveOneArgumentClass, "value";container.resolveTwoArgumentsClass, "value", 10;container.resolveThreeArgumentsClass, "value", 10, new Date;container.resolveFourArgumentsClass, "value", 10, new Date, ;
Creating child containers
By default all child containers can resolve dependencies within themselves and their parent.
;;; container.registerTypeScriptDeveloper, new TypeScriptDeveloper;resolved = child.resolveTypeScriptDeveloper; container.resolveTypeScriptDeveloper; // Will raise an exception
Controlling the lifetime of an instance
The lifetime of an instance can be a singleton or per call (transient)
You can control the lifetime using the TSFunq.ReuseScope
enum.
By default all registrations are marked using the TSFunq.ReuseScope.container
scope.
// transientcontainer.registerTypeScriptDeveloper, new TypeScriptDeveloper.reusedWithinTSFunq.ReuseScope.none; // singleton container.registerTypeScriptDeveloper, new TypeScriptDeveloper.reusedWithinTSFunq.ReuseScope.hierarchy; // singleton per containercontainer.registerTypeScriptDeveloper, new TypeScriptDeveloper.reusedWithinTSFunq.ReuseScope.container;
Disposing registered instances
You can let the container to handle disposal of instances using the TSFunq.Owner
enum.
You need to register a dependency using call .
Only instances with dispose
function can be managed within the container.
By default all registrations are marked using the TSFunq.Owner.container
scope.
; // The container is responsible of disposing the instancecontainer.registerTypeScriptDeveloper, new TypeScriptDeveloper.ownedByTSFunq.Owner.container; // The container is not responsible of disposing the instance container.registerTypeScriptDeveloper, new TypeScriptDeveloper.ownedByTSFunq.Owner.external;
Lazy resolution
You can use the lazyResolve
or lazyResolveNamed
functions in order to resolve a facotry instead of an instance:
;;; container.registerTypeScriptDeveloper, new TypeScriptDeveloper;factory = container.lazyResolveTypeScriptDeveloper, new TypeScriptDeveloper;instance = factory; container.registerNamedTypeScriptDeveloper, "foo", new TypeScriptDeveloper;factory = container.lazyResolveNamedTypeScriptDeveloper, "foo", new TypeScriptDeveloper;instance = factory;
Resolving circular dependencies
You can use the initializedBy
method which run after the resolution phase.
;;; container.registerPresenter, new Presenterc.resolveView;container.registerView, new View .initializedByv.presenter = c.resolvePresenter; view = container.resolveView;presenter = container.resolvePresenter;
Changing the default ReuseScope/Owner
; container.defaultOwner = TSFunq.Owner.external;container.defaultReuse = TSFunq.ReuseScope.none;
License
Like Funq itself, this code is licensed under the Microsoft Public License (Ms-PL)
Copyright � 2015 Sagi Fogel