@jems/di
Version:
An implementation of IoC pattern based on dependency injection that allows you to granulate and decouple your libraries or applications. Wrote using SOLID principles and a variety OOP patterns implementations.
211 lines (210 loc) • 10.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var instanceServicingStrategy_1 = require("../servicing-strategies/instanceServicingStrategy");
var builderFunctionServicingStrategy_1 = require("../servicing-strategies/builderFunctionServicingStrategy");
var constantServicingStrategy_1 = require("../servicing-strategies/constantServicingStrategy");
var perCallDeliveryStrategy_1 = require("../delivery-strategies/perCallDeliveryStrategy");
var singletonDeliveryStrategy_1 = require("../delivery-strategies/singletonDeliveryStrategy");
var perResolutionDeliveryStrategy_1 = require("../delivery-strategies/perResolutionDeliveryStrategy");
var containerizedDeliveryStrategy_1 = require("../delivery-strategies/containerizedDeliveryStrategy");
var ancestorsAreValidator_1 = require("../metadata-validators/ancestorsAreValidator");
var injectedExactlyIntoTypesValidator_1 = require("../metadata-validators/injectedExactlyIntoTypesValidator");
var injectedExactlyIntoAliasesValidator_1 = require("../metadata-validators/injectedExactlyIntoAliasesValidator");
var injectedIntoTypesValidator_1 = require("../metadata-validators/injectedIntoTypesValidator");
var injectedIntoAliasesValidator_1 = require("../metadata-validators/injectedIntoAliasesValidator");
var __1 = require("..");
/**
* Represents a syntax that allow setup the dependecy resolution behavior.
*/
var BehaviorSyntax = /** @class */ (function () {
/**
* Creates a new behavior syntax that allow setup the dependecy resolution behavior.
* @param dependencyMetadata Represents the dependency metadata to affect in a fluently way.
*/
function BehaviorSyntax(dependencyMetadata) {
if (!dependencyMetadata.validators) {
dependencyMetadata.validators = [];
}
this._dependencyMetadata = dependencyMetadata;
}
/*********************************
* The Dependencies (With Syntax)
*********************************/
/**
* Setup the aliases that will repsents the argument names with the given aliases.
* @param aliases Represents the aliases that will repsents the argument names.
* @return A syntax extension to setup the serving, delivery and conditions.
*/
BehaviorSyntax.prototype.with = function (aliases) {
if (!this._dependencyMetadata.isArgumentable) {
throw new __1.Errors.InvalidDataError('Can not add arguments names to a not argumentable dependency.');
}
this._dependencyMetadata.argumentsNames = aliases;
return this;
};
/*********************************
* The servicing (As Syntax)
*********************************/
/**
* Setup the current alias bind to be served as an instace.
* @return A syntax extension to setup the delivery and conditions.
*/
BehaviorSyntax.prototype.asInstance = function () {
this._dependencyMetadata.servicingStrategy = new instanceServicingStrategy_1.InstanceServicingStrategy();
return this;
};
/**
* Setup the current alias bind to be served as the result of the referenced function..
* @return A syntax extension to setup the delivery and conditions.
*/
BehaviorSyntax.prototype.asBuilderFunction = function () {
this._dependencyMetadata.servicingStrategy = new builderFunctionServicingStrategy_1.BuilderFunctionServicingStrategy();
return this;
};
/**
* Setup the current alias bind to be served as a constant.
* @return A syntax extension to setup the delivery and conditions.
*/
BehaviorSyntax.prototype.asConstant = function () {
this._dependencyMetadata.servicingStrategy = new constantServicingStrategy_1.ConstantServicingStrategy();
return this;
};
/**
* Setup the current alias bind to be served with the given servicing strategy.
* @param servicingStrategy Represents the servicing strategy to serve the reference.
* @return A syntax extension to setup the delivery and conditions.
*/
BehaviorSyntax.prototype.as = function (servicingStrategy) {
this._dependencyMetadata.servicingStrategy = servicingStrategy;
return this;
};
/*********************************
* The delivery modes (In Syntax)
*********************************/
/**
* Setup the current alias bind to be served in each call.
* @return A syntax extension to setup conditions.
*/
BehaviorSyntax.prototype.inPerCallMode = function () {
this._dependencyMetadata.deliveryStrategy = new perCallDeliveryStrategy_1.PerCallDeliveryStrategy();
return this;
};
/**
* Setup the current alias bind to be served once per each resolution process.
* @return A syntax extension to setup conditions.
*/
BehaviorSyntax.prototype.inPerResolutionMode = function () {
this._dependencyMetadata.deliveryStrategy = new perResolutionDeliveryStrategy_1.PerResolutionDeliveryStrategy();
return this;
};
/**
* Setup the current alias bind to be served only once.
* @return A syntax extension to setup conditions.
*/
BehaviorSyntax.prototype.inSingletonMode = function () {
this._dependencyMetadata.deliveryStrategy = new singletonDeliveryStrategy_1.SingletonDeliveryStrategy();
return this;
};
/**
* Setup the current alias bind to be served once per container.
* @return A syntax extension to setup conditions.
*/
BehaviorSyntax.prototype.inContainerizedMode = function () {
this._dependencyMetadata.deliveryStrategy = new containerizedDeliveryStrategy_1.ContainerizedDeliveryStrategy();
return this;
};
/**
* Setup the current alias bind to be delivered with the given delivery strategy.
* @param deliveryStrategy Represents the delivery strategy to deliver the reference.
* @return A syntax extension to setup conditions.
*/
BehaviorSyntax.prototype.inMode = function (deliveryStrategy) {
this._dependencyMetadata.deliveryStrategy = deliveryStrategy;
return this;
};
/*********************************
* The validators (When Syntax)
*********************************/
/**
* Setup the current alias bind to be valid when the target be an ancestor of the given types.
* @param types Represents the types that must be the ancestor of the bind.
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.whenAncestorsAre = function () {
var types = [];
for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i];
}
this._dependencyMetadata.validators
.push(function (resolutionContext, dependencyMetadata) { return ancestorsAreValidator_1.default.apply(void 0, [resolutionContext, dependencyMetadata].concat(types)); });
return this;
};
/**
* Setup the current alias bind to be valid when the metadata is being injected into the given aliases.
* @param aliases Represents the aliases where the bind must be injected
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.whenInjectedIntoAliases = function () {
var aliases = [];
for (var _i = 0; _i < arguments.length; _i++) {
aliases[_i] = arguments[_i];
}
this._dependencyMetadata.validators
.push(function (resolutionContext, dependencyMetadata) { return injectedIntoAliasesValidator_1.default.apply(void 0, [resolutionContext, dependencyMetadata].concat(aliases)); });
return this;
};
/**
* Setup the current alias bind to be valid when the metadata is being injected into the given types.
* @param types Represents the types where the bind must be injected.
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.whenInjectedIntoTypes = function () {
var types = [];
for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i];
}
this._dependencyMetadata.validators
.push(function (resolutionContext, dependencyMetadata) { return injectedIntoTypesValidator_1.default.apply(void 0, [resolutionContext, dependencyMetadata].concat(types)); });
return this;
};
/**
* Setup the current alias bind to be valid when the metadata is being injected exactly into the given aliases.
* @param aliases Represents the aliases where the bind must be exactly injected
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.whenInjectedExactlyIntoAliases = function () {
var aliases = [];
for (var _i = 0; _i < arguments.length; _i++) {
aliases[_i] = arguments[_i];
}
this._dependencyMetadata.validators
.push(function (resolutionContext, dependencyMetadata) { return injectedExactlyIntoAliasesValidator_1.default.apply(void 0, [resolutionContext, dependencyMetadata].concat(aliases)); });
return this;
};
/**
* Setup the current alias bind to be valid when the metadata is being injected exactly into the given types.
* @param types Represents the types where the bind must be exactly injected.
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.whenInjectedExactlyIntoTypes = function () {
var types = [];
for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i];
}
this._dependencyMetadata.validators
.push(function (resolutionContext, dependencyMetadata) { return injectedExactlyIntoTypesValidator_1.default.apply(void 0, [resolutionContext, dependencyMetadata].concat(types)); });
return this;
};
/**
* Setup the current alias bind to be valid when the given validator is successfully.
* @param validator Represents a custom validator.
* @return A syntax extention to setup conditions.
*/
BehaviorSyntax.prototype.when = function (validator) {
this._dependencyMetadata.validators
.push(validator);
return this;
};
return BehaviorSyntax;
}());
exports.BehaviorSyntax = BehaviorSyntax;