Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improved tests and readme and added license file

  • Loading branch information...
commit 9852018ccbd354377b7a1e2860cdc4f84127b90e 1 parent dc5f355
@cburgdorf authored
Showing with 110 additions and 13 deletions.
  1. +9 −0 LICENSE.md
  2. +89 −0 README.md
  3. +12 −13 tests/swiftcore.tests.js
View
9 LICENSE.md
@@ -0,0 +1,9 @@
+The MIT License
+
+Copyright (c) 2012 Christoph Burgdorf
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
89 README.md
@@ -132,6 +132,95 @@ test('can resolve types depending on multiple other types', function () {
});
```
+##So, how do I use singletons?
+
+```JavaScript
+test('nested singletons are only created once', function () {
+
+ var instancesOfTypeA = 0;
+
+ function TypeA() {
+ instancesOfTypeA++;
+ }
+
+ function TypeB(options) {
+ if (options.typeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+ }
+
+ TypeB.dependencies = ["typeA"];
+
+ function TypeC(options) {
+ if (options.typeA === undefined) {
+ throw "missing argument [typeA]"
+ }
+
+ if (options.typeB === undefined) {
+ throw "missing argument [typeB]"
+ }
+
+ this.test = "foo";
+ }
+
+ TypeC.dependencies = ["typeB", "typeA"];
+
+ //The "asSingleton()" tells swiftcore to only create the instance once and reuse the same single instance
+ //each time one wants to resolve "typeA"
+ swiftcore.register("typeA").withType(TypeA).asSingleton();
+
+ //We could have also registered it in this fashion but the fluent way is more descriptive
+ //swiftcore.register("TypeA", TypeA, true);
+ swiftcore.register("typeB", TypeB);
+ swiftcore.register("typeC", TypeC);
+
+ var instance = swiftcore.resolve("TypeC");
+ ok(instance !== undefined);
+ ok(instance.test === "foo");
+ //This is the interesting part. In contrast to the previous example, the constructor function of TypeA was only called once
+ equal(1, instancesOfTypeA);
+});
+```
+
+##This all looks good but I dislike that you invoke functions with one single configuration object instead of single parameters
+
+Hey, that's cool with me! Swiftcore was designed with the [open closed principle](http://en.wikipedia.org/wiki/Open/closed_principle) in mind.
+You can trivially extend it to your needs!
+
+Let's take the example of above and say you would rather have your constructor function called with multiple parameters.
+Swiftcore comes with a concept of instance providers. This means swiftcore itself does not even know what it exactly means
+to "create an instance of something". What it does know is when to ask the current instance provider for a new instance.
+The default instance provider looks like this:
+
+```JavaScript
+swiftcore.instanceProvider.constructorBased = function(registration, dependencies){
+
+ //depenencies is the configuration object with all dependencies already resolved
+ var instance = new registration.type(dependencies);
+ //instance provider should set the isInitialized flat to true to tell swiftcore that the instance has been created
+ registration.isInitialized = true;
+
+ //return the created instance
+ return instance;
+};
+```
+
+I leave it as an exercise to you to come up with an instance provider that does invoke the constructor function with atomic parameters.
+All you then have to do is to tell swiftcore to use your custom instance provider:
+
+```JavaScript
+swiftcore.defaultInstanceProvider = myCustomInstanceProvider;
+```
+
+##What's left to say
+
+All current features are backed by tests and its a good starting point to look at the tests in order to get used to swiftcore's features:
+https://github.com/cburgdorf/swiftcore.js/tree/master/tests
+
+The projects is MIT licensed and accepts pull requests
+
+
+
View
25 tests/swiftcore.tests.js
@@ -192,42 +192,41 @@ test('can resolve types depending on multiple other types', function () {
test('nested singletons are only created once', function () {
- var creations = 0;
+ var instancesOfTypeA = 0;
function TypeA() {
- creations++;
+ instancesOfTypeA++;
}
function TypeB(options) {
- creations++;
- if (options.TypeA === undefined) {
+ if (options.typeA === undefined) {
throw "missing argument [typeA]"
}
}
- TypeB.dependencies = ["TypeA"];
+ TypeB.dependencies = ["typeA"];
function TypeC(options) {
- creations++;
- if (options.TypeA === undefined) {
+ if (options.typeA === undefined) {
throw "missing argument [typeA]"
}
- if (options.TypeB === undefined) {
+ if (options.typeB === undefined) {
throw "missing argument [typeB]"
}
this.test = "foo";
}
- TypeC.dependencies = ["TypeB", "TypeA"];
+ TypeC.dependencies = ["typeB", "typeA"];
- swiftcore.register("TypeA", TypeA, true);
- swiftcore.register("TypeB", TypeB, true);
- swiftcore.register("TypeC", TypeC, true);
+ swiftcore.register("typeA").withType(TypeA).asSingleton();
+ //swiftcore.register("TypeA", TypeA, true);
+ swiftcore.register("typeB", TypeB);
+ swiftcore.register("typeC", TypeC);
var instance = swiftcore.resolve("TypeC");
ok(instance !== undefined);
ok(instance.test === "foo");
- equal(3, creations);
+ equal(1, instancesOfTypeA);
});
Please sign in to comment.
Something went wrong with that request. Please try again.