JavaScriptã§åãDIã³ã³ãããä½ã£ã¦ã¿ãã
JavaScriptã§åãDIã³ã³ãããä½ã£ã¦ã¿ã¾ããã(â計画)
- ä¸è¬çãªDIã³ã³ãããæã¤æ©è½ããµãã¼ã
- ã¤ã³ã¹ã¿ã³ã¹ç®¡ç(Singleton or Prototype)ãããããã£ã¤ã³ã¸ã§ã¯ã·ã§ã³ãåæåã¡ã½ããã®å®è¡ãªã©
- ã³ã³ãã¼ãã³ãå®ç¾©ã¯JavaScript APIã§ã
- ã¢ããã¼ã·ã§ã³(ãã©ã)ã§ã®ã³ã³ãã¼ãã³ãå®ç¾©ãå¯è½ã
- AOPæ©è½ãæä¾ãã¾ãã
ãã¦ã³ãã¼ã
container.js(コンテナ本体) - ver 0.1.0
テストケースの実行結果はこちら。
-
- Yahoo Test Utilityã使ã£ã¦ãã¾ãã
- åä½ã¯IE6ã¨FireFox2ã§ç¢ºèªãã¦ãã¾ãã
使ãæ¹
åºæ¬çãªä½¿ãæ¹
- ã³ã³ãããä½æãã¾ãã
- å¼æ°ã§ã³ã³ãã¼ãã³ããå®ç¾©ããé¢æ°ãæå®ãã¾ãã
- é¢æ°ã§ã¯ãå¼æ°ã§æ¸¡ãããBinderãå©ç¨ãã¦ãã³ã³ãã¼ãã³ããå®ç¾©ãã¾ãã
- container.Container#get(name)ã§ãã³ã³ããããã³ã³ãã¼ãã³ããåå¾ãã¾ãã
- ã³ã³ãã¼ãã³ãã¯ååã§é¢é£ä»ãããã¾ããå¼æ°ã§ååãæå®ãã¦ã³ã³ãã¼ãã³ããåå¾ãã¾ãã
var stdout = document.getElementById( "stdout" ); // ã¯ã©ã¹ function Kitten() {} Kitten.prototype = { getName: function () { return this.name; } }; // ã³ã³ãããä½æã // å¼æ°ã§ã³ã³ãã¼ãã³ããå®ç¾©ããé¢æ°ã渡ãã var c = new container.Container( function( binder ) { // Kittenã¯ã©ã¹ã"mii"ã«é¢é£ä»ãã // ã¾ããã³ã³ãã¼ãã³ãã®"name"ããããã£ã«"mii"ãã¤ã³ã¸ã§ã¯ã·ã§ã³ binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii" } ); }); // ã³ã³ããããã³ã³ãã¼ãã³ããåå¾ããã var mii = c.get( "mii" ); stdout.innerHTML += mii.getName();
ã³ã³ãã¼ãã³ãã®ç»é²
ãã¯ã©ã¹æå®ããããªãã¸ã§ã¯ãæå®ããããããã¤ãæå®ãã®3ã¤ãããã¾ãã
- ã¯ã©ã¹æå®
- ã³ã³ãã¼ãã³ããã¯ã©ã¹(ã³ã³ã¹ãã©ã¯ã¿é¢æ°)ã§æå®ãã¾ãã
- ã³ã³ã¹ãã©ã¯ã¿é¢æ°ãå®è¡ãã¦ä½æããããªãã¸ã§ã¯ããã³ã³ãã¼ãã³ãã¨ãªãã¾ãã
- ã³ã³ã¹ãã©ã¯ã¿é¢æ°ã«æ¸¡ãå¼æ°ã¯æå®ã§ãã¾ãã(ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯æªãµãã¼ã)ãã³ã³ã¹ãã©ã¯ã¿ã¯å¸¸ã«å¼æ°ãªãã§å®è¡ããã¾ãã
- ãªãã¸ã§ã¯ãæå®
- ã³ã³ãã¼ãã³ããæ¢åã®ãªãã¸ã§ã¯ãã§æå®ãã¾ãã
- ãããã¤ãæå®
- ã³ã³ãã¼ãã³ããä½æããé¢æ°(ãããã¤ãé¢æ°)ãæå®ãã¾ãã
- ãããã¤ãé¢æ°ãå®è¡ãã¦ä½æããããªãã¸ã§ã¯ããã³ã³ãã¼ãã³ãã¨ãªãã¾ãã
- ãããã¤ãé¢æ°ã«ã¯å¼æ°ã¨ãã¦ã³ã³ããã渡ããã¾ãã
var c = new container.Container( function( binder ) { // ã¯ã©ã¹æå® binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii" } ); // ãªãã¸ã§ã¯ãæå® binder.bind( "tora" ).toInstance( new Kitten( "tora" ) ); // ãããã¤ãæå® binder.bind( "shiro" ).toProvider( function( container ){ return new Kitten( "shiro" ); } ); });
ã³ã³ãã¼ãã³ãã®ã¹ã³ã¼ã
ãã·ã³ã°ã«ãã³ãã¨ããããã¿ã¤ããããµãã¼ããã¾ããããã©ã«ãã¯ãã·ã³ã°ã«ãã³ãã§ãã
- ã·ã³ã°ã«ãã³
- è¤æ°ååãã³ã³ãã¼ãã³ããåå¾ããå ´åãååã®get()ã§ä½æããã³ã³ãã¼ãã³ãã常ã«è¿ãã¾ãã
- å ·ä½çã«ã¯ãã³ã³ããå ã«ã³ã³ãã¼ãã³ãã®ã¤ã³ã¹ã¿ã³ã¹ããã£ãã·ã¥ããget()ã§ååå¾ãããå ´åã«ãã£ãã·ã¥ãããã°ãããè¿ããã¨ããåä½ããã¾ããã
- ãã£ãã·ã¥ã¯container.Container#destroy()ã§ç ´æ£ããã¾ãã
- è¤æ°ååãã³ã³ãã¼ãã³ããåå¾ããå ´åãååã®get()ã§ä½æããã³ã³ãã¼ãã³ãã常ã«è¿ãã¾ãã
- ãããã¿ã¤ã
- ã³ã³ãã¼ãã³ãåå¾ã®ãã³ã«ãã³ã³ãã¼ãã³ããåä½æãã¾ãã
var c = new container.Container( function( binder ) { // ã·ã³ã°ã«ãã³ binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii" }).scope( container.Scope.Singleton ); // ãããã¿ã¤ã binder.bind( "tora" ).to( Kitten ).inject( { "name": "tora" }).scope( container.Scope.Prototype ); });
ããããã£ã¤ã³ã¸ã§ã¯ã·ã§ã³
ããããã£ã¤ã³ã¸ã§ã¯ã·ã§ã³(ãã£ã¼ã«ãã«å¤ãç´æ¥è¨å®)ã®ã¿ãµãã¼ãããã¾ãã
- ããã·ã¥ã§æå®ãããå¤ããã®ã¾ã¾ã¤ã³ã¸ã§ã¯ã·ã§ã³ããã¾ãã
- ã³ã³ããã«ç»é²ãããã³ã³ãã¼ãã³ããã¤ã³ã¸ã§ã¯ã·ã§ã³ããã«ã¯ãcontainer.component(name) ã¾ã㯠container.components(name) ã使ç¨ãã¾ãã
- container.component(name)ã®å ´åãcontainer.Container#get(name)ããå¤ãã¤ã³ã¸ã§ã¯ã·ã§ã³ããã¾ãã
- container.components(name)ã®å ´åãcontainer.Container#gets(name)ããå¤ãã¤ã³ã¸ã§ã¯ã·ã§ã³ããã¾ãã
var c = new container.Container( function( binder ) { // ããããã£ã¤ã³ã¸ã§ã¯ã·ã§ã³ã®è¨å® binder.bind( "mii" ).to( Kitten ).inject( { // ããã·ã¥ã®å¤ããã®ã¾ã¾ã¤ã³ã¸ã§ã¯ã·ã§ã³ãããã "name": "mii", "getAge": function() { return this.age; } , // å®ç¾©æ¸ã¿ã³ã³ãã¼ãã³ãã®ã¤ã³ã¸ã§ã¯ã·ã§ã³æå® "kitten" : container.component("kitten"), "kittens" : container.components("kittens") }); binder.bind( "kitten" ).to( Kitten ).inject( { "name":"tora"}); binder.bind( "kittens" ).to( Kitten ).inject( { "name":"kuro"}); binder.bind( "kittens" ).to( Kitten ).inject( { "name":"shiro"}); });
åæåé¢æ°ã»ç ´æ£é¢æ°ã®å®è¡
ã³ã³ãã¼ãã³ããä½ææã«å¼ã°ããé¢æ°(åæåé¢æ°)ã¨ç ´æ£æã«å¼ã°ããé¢æ°(ç ´æ£é¢æ°)ãç»é²ã§ãã¾ãã
- åæåé¢æ°ã¯ãã³ã³ãã¼ãã³ããä½æãããããããã£ã¤ã³ã¸ã§ã¯ã·ã§ã³ãããå¾ãã¤ã³ã¿ã¼ã»ãã¿ã®è¨å®åã«å®è¡ããã¾ãã
- ç ´æ£é¢æ°ã¯container.Container#destroy()ãå®è¡ããéã«ä»¥ä¸ã®æ¡ä»¶ãæºããå ´åãå®è¡ããã¾ãã
- ã³ã³ãã¼ãã³ããã·ã³ã°ã«ãã³ã§ãããã¨ã
- ã³ã³ãã¼ãã³ããdestroy()å®è¡æã«ä½ææ¸ã¿ã§ãããã¨ã
- åæåé¢æ°ã»ç ´æ£é¢æ°ã«ã¯ãå¼æ°ã¨ãã¦ã³ã³ãã¼ãã³ãã¨ã³ã³ããã渡ããã¾ãã
var c = new container.Container( function( binder ) { // åæåé¢æ°ã»ç ´æ£é¢æ°ã®è¨å® binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii" }).initialize( function( obj, container ) { // ã³ã³ãã¼ãã³ããåæåããå¦ç }).destroy( function( obj, container ) { // ã³ã³ãã¼ãã³ããç ´æ£ããå¦ç }); });
ã¤ã³ã¿ã¼ã»ãã¿ã®é©ç¨
ã³ã³ãã¼ãã³ãã®ã¡ã½ããã«ã¤ã³ã¿ã¼ã»ãã¿ãè¨å®ã§ãã¾ãã
- ã¤ã³ã¿ã¼ã»ãã¿ã¯é¢æ°ã§æå®ãã¾ãã
- ã¤ã³ã¿ã¼ã»ãã¿ãé©ç¨ããé¢æ°ã¯container.Matcherã§æå®ãã¾ãã
- ã¤ã³ã¿ã»ãã¿ã¯è¤æ°è¨å®ã§ãã¾ãã
var c = new container.Container( function( binder ) { var builder = binder.bind( "mii" ).to( Kitten ).inject( { "name": "mii" }); // ã¤ã³ã¿ã¼ã»ãã¿ã®é©ç¨ builder.intercept( function( methodInvocation ) { methodInvocation.getMethodName(); // å®è¡ãããã¡ã½ããåãåå¾ methodInvocation.getThis(); // ã³ã³ãã¼ãã³ããåå¾ methodInvocation.getArguments()[0] = "foo"; // å¼æ°ãåå¾&å¤æ´ //ãå ã®ã¡ã½ãããå®è¡ return methodInvocation.proceed(); }, // ã¤ã³ã¿ã¼ã»ãã¿ãé©ç¨ããé¢æ°ãcontainer.Matcherã§æå®ããã new container.Matcher( /get.*/, /set.*/ ) ); // ã¤ã³ã¿ã¼ã»ãã¿ã¯è¤æ°åè¨å®ã§ããã builder.intercept( function( methodInvocation ) { return methodInvocation.proceed(); }, // ãã¹ã¦ã«ãããããcontainer.Matcherãä½æããã¦ã¼ãã£ãªã㣠container.any()); });
ã¢ããã¼ã·ã§ã³
ã³ã³ãã¼ãã³ãã®è¨å®ã¯ã¢ããã¼ã·ã§ã³(ãã©ã)ã§è¡ããã¨ãå¯è½ã§ãã
- ã¢ããã¼ã·ã§ã³æå®ã¨APIæå®ã®ä¸¡æ¹ãããå ´åãAPIæå®ã§ã®è¨å®ãæå¹ã¨ãªãã¾ãã
- ãã ããã¤ã³ã¿ã¼ã»ãã¿ã®è¨å®ã«é¢ãã¦ã¯ãã追å ãã®åä½ã¨ãªãã¾ãã
// ã¯ã©ã¹ function Kitten() {} Kitten.prototype = { getName: function () { return this.name; } }; Kitten.prototype.meta ={ // ã¢ããã¼ã·ã§ã³(ãã©ã)ã§ã®ã³ã³ãã¼ãã³ãè¨å® "@Container": { // ã¤ã³ã¸ã§ã¯ã·ã§ã³ "@Inject":{ "name" : "foo" }, //åæåé¢æ°ã»ç ´æ£é¢æ° "@Initialize": function( obj, container ) {}, "@Destroy": function( obj, container ) {}, // ã¹ã³ã¼ã "@Scope": container.Scope.Singleton, // ã¤ã³ã¿ã¼ã»ãã¿ "@Intercept": [ [ function( mi ){ return mi.proceed(); }, container.any() ], [ function( mi ){ return mi.proceed(); }, new container.Matcher( /get.*/ ) ] ] } }
getã¨gets
ã³ã³ããã«ã¯åãååã®ã³ã³ãã¼ãã³ããè¤æ°ç»é²ãããã¨ãã§ãã¾ãã
- get()ã使ç¨ããå ´åãæåã«ç»é²ããå®ç¾©ãå
ã«ä½æãããã³ã³ãã¼ãã³ããè¿ããã¾ãã
- ãã¹ãã±ã¼ã¹ãªã©ã§ãã³ã³ãã¼ãã³ããã¢ãã¯ã«å·®ãæ¿ãããå ´åãæ¬çªç¨ã³ã³ãã¼ãã³ãã®å®ç¾©ã®åã«åãååã§ã¢ãã¯ãç»é²ãã¦ããã°OKã§ãã
- gets()ã®å ´åããã¹ã¦ã®å®ç¾©ãå ã«ã³ã³ãã¼ãã³ããä½æããããã®é åãè¿ããã¾ãã
// ã³ã³ãããä½æã var c = new container.Container( function( binder ) { // åãååã§è¤æ°ã®ã³ã³ãã¼ãã³ããç»é² binder.bind( "kittens" ).to( Kitten ).inject( { "name":"mii"}); binder.bind( "kittens" ).to( Kitten ).inject( { "name":"kuro"}); binder.bind( "kittens" ).to( Kitten ).inject( { "name":"shiro"}); }); // gets()ã§ã³ã³ãããããã¹ã¦ã®ã³ã³ãã¼ãã³ããåå¾ããã var kittens = c.gets( "kittens" ); stdout.innerHTML += kittens[0].getName() + "<br/>"; // "mii" stdout.innerHTML += kittens[1].getName() + "<br/>"; // "kuro" stdout.innerHTML += kittens[2].getName() + "<br/>"; // "shiro" // get()ã®å ´åãæåã«ç»é²ããã³ã³ãã¼ãã³ããè¿ãããã var kitten = c.get( "kittens" ); stdout.innerHTML += kitten.getName(); // "mii"
æ®ã
以ä¸ã®æ©è½ã«ã¤ãã¦ã¯ãã®ãã¡æ¸ãã¾ãã
- ã¡ã½ãããã¤ã³ãã£ã³ã°
- ã°ãã¼ãã«ã¤ã³ã¿ã¼ã»ãã¿ã¼
- ãã¼ã ã¹ãã¼ã¹