├── .gitattributes ├── .gitignore ├── Gruntfile.coffee ├── README.md ├── bower.json ├── build ├── dddbase.d.ts └── dddbase.js ├── dddbase.d.ts ├── dddbase.js ├── dddbase.min.js ├── definitions ├── chai │ └── chai.d.ts ├── mocha │ └── mocha.d.ts └── monapt │ └── monapt.d.ts ├── package.json ├── src ├── async_on_local_storage_repotitory.ts ├── async_on_memory_repotitory.ts ├── async_on_session_storage_repotitory.ts ├── async_repository.ts ├── entity.ts ├── identity.ts ├── on_local_storage_repository.ts ├── on_memory_repository.ts ├── on_session_storage_repository.ts └── repository.ts ├── test ├── async_on_local_storage_repository.ts ├── async_on_memory_repository.ts ├── async_on_session_storage_repository.ts ├── entity.ts ├── identify.ts ├── on_local_storage_repository.ts ├── on_memory_repository.ts └── on_session_storage_repository.ts └── testem.json /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | bower_components/ 3 | compiled/ 4 | public/ 5 | .idea/ 6 | -------------------------------------------------------------------------------- /Gruntfile.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (grunt) -> 2 | grunt.initConfig 3 | pkg: grunt.file.readJSON 'package.json' 4 | typescript: 5 | compile: 6 | src: ['src/*.ts'] 7 | dest: 'dddbase.js' 8 | options: 9 | module: 'commonjs' 10 | target: 'es3' 11 | # sourcemap: true 12 | declaration: true 13 | 14 | test: 15 | src: ['test/**/*.ts'] 16 | dest: 'compiled' 17 | options: 18 | module: 'commonjs' 19 | target: 'es3' 20 | 21 | clean: 22 | type: 23 | src: ['compiled/**/*.js', 'compiled/*'] 24 | build: 25 | src: ['build/**/*.js'] 26 | 27 | concat: 28 | dist: 29 | src: ['compiled/src/dddbase.js'] 30 | dest: 'build/dddbase.js' 31 | options: 32 | separator: ';' 33 | 34 | uglify: 35 | min: 36 | files: 37 | 'dddbase.min.js': ['dddbase.js'] 38 | ### 39 | options: 40 | mangle: 41 | expect: ['jQuery'] 42 | sourceMap: 'build/source-map.js' 43 | ### 44 | 45 | connect: 46 | preview: 47 | options: 48 | port: 9000 49 | base: 'public' 50 | 51 | regarde: 52 | src: 53 | files: ['src/**/*.*'] 54 | tasks: ['generate'] 55 | 56 | grunt.loadNpmTasks 'grunt-typescript' 57 | grunt.loadNpmTasks 'grunt-contrib-clean' 58 | grunt.loadNpmTasks 'grunt-contrib-concat' 59 | grunt.loadNpmTasks 'grunt-contrib-uglify' 60 | grunt.loadNpmTasks 'grunt-contrib-copy' 61 | grunt.loadNpmTasks 'grunt-contrib-connect' 62 | grunt.loadNpmTasks 'grunt-regarde' 63 | grunt.loadNpmTasks 'grunt-exec' 64 | 65 | grunt.registerTask 'compile', ['typescript'] 66 | grunt.registerTask 'default', ['compile'] 67 | grunt.registerTask 'build', ['typescript:compile', 'concat', 'uglify'] 68 | 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TypeScript DDD Base 2 | =================================== 3 | 4 | Domain Driven-Desgin support library for TypeScript. -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-dddbase", 3 | "version": "0.1.4", 4 | "dependencies": { 5 | "monapt": "git://github.com/yaakaito/monapt.git#>=0.2.2" 6 | }, 7 | "main": [ 8 | "dddbase.js", 9 | "dddbase.min.js", 10 | "dddbase.d.ts" 11 | ], 12 | "ignore": [ 13 | "Gruntfile.coffee", 14 | ".gitattriute", 15 | ".gitignore", 16 | "package.json", 17 | "testem.json", 18 | "definitions/", 19 | "src/", 20 | "test/", 21 | "build/" 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /build/dddbase.d.ts: -------------------------------------------------------------------------------- 1 | declare module DDD { 2 | interface Identity { 3 | getValue(): T; 4 | equals(that: Identity): boolean; 5 | } 6 | class AbstractIdentity implements Identity { 7 | private value; 8 | constructor(value: T); 9 | public getValue(): T; 10 | public equals(that: Identity): boolean; 11 | } 12 | class NumberIdentity extends AbstractIdentity { 13 | constructor(value: number); 14 | } 15 | } 16 | declare module DDD { 17 | interface Entity> { 18 | getIdentity(): Identity; 19 | equals(that: Entity>): boolean; 20 | } 21 | class AbstractEntity> implements Entity> { 22 | private identity; 23 | constructor(identity: ID); 24 | public getIdentity(): ID; 25 | public equals(that: Entity>): boolean; 26 | } 27 | } 28 | declare module DDD { 29 | interface Repository, E extends Entity>> { 30 | store(entity: E): E; 31 | deleteByEntity(entity: E); 32 | deleteByIdentity(identity: ID); 33 | } 34 | class OnMemoryRepository, E extends Entity>> implements Repository, E extends Entity>> { 35 | private entities; 36 | public resolveWithIdentity(identity: ID): Entity>; 37 | public store(entity: E): E; 38 | public deleteByEntity(entity: E): void; 39 | public deleteByIdentity(identity: ID): void; 40 | } 41 | } 42 | declare module DDD { 43 | interface Resolver, E extends Entity>> { 44 | resolve(entity: E): Resolver, E extends Entity>>; 45 | resolve(identity: Identity): Resolver, E extends Entity>>; 46 | resolve(): Resolver, E extends Entity>>; 47 | } 48 | interface AsyncRepository, E extends Entity>> { 49 | storeAsync(entity: E): Resolver, E extends Entity>>; 50 | resolveAsyncWithIdentity(identity: ID): Resolver, E extends Entity>>; 51 | deleteAsyncByEntity(entity: E): Resolver, E extends Entity>>; 52 | deleteAsyncByIdentity(identity: ID): Resolver, E extends Entity>>; 53 | } 54 | class AsyncOnMemoryRepository, E extends Entity>> extends OnMemoryRepository, E extends Entity>> implements AsyncRepository, E extends Entity>> { 55 | private Resolver; 56 | constructor(Resolver: new() => Resolver, E extends Entity>>); 57 | private createResolver(); 58 | public storeAsync(entity: E): Resolver, E extends Entity>>; 59 | public resolveAsyncWithIdentity(identity: ID): Resolver, E extends Entity>>; 60 | public deleteAsyncByEntity(entity: E): Resolver, E extends Entity>>; 61 | public deleteAsyncByIdentity(identity: ID): Resolver, E extends Entity>>; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /build/dddbase.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yaakaito/typescript-dddbase/a248e5f41cfa35a70b035843bb845a21e3beede2/build/dddbase.js -------------------------------------------------------------------------------- /dddbase.d.ts: -------------------------------------------------------------------------------- 1 | declare module DDD { 2 | class Identity { 3 | private value; 4 | constructor(value: T); 5 | public getValue(): T; 6 | public equals(that: Identity): boolean; 7 | } 8 | class NumberIdentity extends Identity { 9 | constructor(value: number); 10 | } 11 | } 12 | declare module DDD { 13 | class Entity> { 14 | private identity; 15 | constructor(identity: ID); 16 | public getIdentity(): ID; 17 | public equals(that: Entity): boolean; 18 | } 19 | } 20 | declare module DDD { 21 | interface IRepository, E extends DDD.Entity> { 22 | resolveOption(identity: ID): monapt.Option; 23 | resolve(identity: ID): E; 24 | store(entity: E): E; 25 | storeList(entityList: E[]): E[]; 26 | deleteByEntity(entity: E): IRepository; 27 | deleteByIdentity(identity: ID): IRepository; 28 | } 29 | } 30 | declare module DDD { 31 | class AsyncRepository, E extends DDD.Entity> { 32 | private core; 33 | constructor(core: DDD.IRepository); 34 | public resolve(identity: ID): monapt.Future; 35 | public store(entity: E): monapt.Future; 36 | public storeList(entityList: E[]): monapt.Future; 37 | public deleteByEntity(entity: E): monapt.Future>; 38 | public deleteByIdentity(identity: ID): monapt.Future>; 39 | } 40 | } 41 | declare module DDD { 42 | interface ILocalStorageMapper> { 43 | parse(json: Object): E; 44 | stringify(entity: E): string; 45 | } 46 | class OnLocalStorageRepository, E extends DDD.Entity> implements DDD.IRepository { 47 | constructor(mapper: ILocalStorageMapper); 48 | public parse: (json: Object) => E; 49 | public stringify: (entity: E) => string; 50 | public resolveOption(identity: ID): monapt.Option; 51 | public resolve(identity: ID): E; 52 | public store(entity: E): E; 53 | public storeList(entityList: E[]): E[]; 54 | public deleteByEntity(entity: E): OnLocalStorageRepository; 55 | public deleteByIdentity(identity: ID): OnLocalStorageRepository; 56 | } 57 | } 58 | declare module DDD { 59 | class AsyncOnLocalStorageRepository, E extends DDD.Entity> extends DDD.AsyncRepository { 60 | constructor(mapper: DDD.ILocalStorageMapper); 61 | } 62 | } 63 | declare module DDD { 64 | class OnMemoryRepository, E extends DDD.Entity> implements DDD.IRepository { 65 | private entities; 66 | public resolveOption(identity: ID): monapt.Option; 67 | public resolve(identity: ID): E; 68 | public store(entity: E): E; 69 | public storeList(entityList: E[]): E[]; 70 | public deleteByEntity(entity: E): OnMemoryRepository; 71 | public deleteByIdentity(identity: ID): OnMemoryRepository; 72 | } 73 | } 74 | declare module DDD { 75 | class AsyncOnMemoryRepository, E extends DDD.Entity> extends DDD.AsyncRepository { 76 | constructor(); 77 | } 78 | } 79 | declare module DDD { 80 | interface ISessionStorageMapper> { 81 | parse(json: Object): E; 82 | stringify(entity: E): string; 83 | } 84 | class OnSessionStorageRepository, E extends DDD.Entity> implements DDD.IRepository { 85 | constructor(mapper: ISessionStorageMapper); 86 | public parse: (json: Object) => E; 87 | public stringify: (entity: E) => string; 88 | public resolveOption(identity: ID): monapt.Option; 89 | public resolve(identity: ID): E; 90 | public store(entity: E): E; 91 | public storeList(entityList: E[]): E[]; 92 | public deleteByEntity(entity: E): OnSessionStorageRepository; 93 | public deleteByIdentity(identity: ID): OnSessionStorageRepository; 94 | } 95 | } 96 | declare module DDD { 97 | class AsyncOnSessionStorageRepository, E extends DDD.Entity> extends DDD.AsyncRepository { 98 | constructor(mapper: DDD.ISessionStorageMapper); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /dddbase.js: -------------------------------------------------------------------------------- 1 | var __extends = this.__extends || function (d, b) { 2 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 3 | function __() { this.constructor = d; } 4 | __.prototype = b.prototype; 5 | d.prototype = new __(); 6 | }; 7 | var DDD; 8 | (function (DDD) { 9 | var Identity = (function () { 10 | function Identity(value) { 11 | this.value = value; 12 | } 13 | Identity.prototype.getValue = function () { 14 | return this.value; 15 | }; 16 | 17 | Identity.prototype.equals = function (that) { 18 | if (that == null) { 19 | return false; 20 | } 21 | if (this == that) { 22 | return true; 23 | } 24 | 25 | return this.value === that.getValue(); 26 | }; 27 | return Identity; 28 | })(); 29 | DDD.Identity = Identity; 30 | 31 | var NumberIdentity = (function (_super) { 32 | __extends(NumberIdentity, _super); 33 | function NumberIdentity(value) { 34 | _super.call(this, value); 35 | } 36 | return NumberIdentity; 37 | })(Identity); 38 | DDD.NumberIdentity = NumberIdentity; 39 | })(DDD || (DDD = {})); 40 | var DDD; 41 | (function (DDD) { 42 | var Entity = (function () { 43 | function Entity(identity) { 44 | this.identity = identity; 45 | } 46 | Entity.prototype.getIdentity = function () { 47 | return this.identity; 48 | }; 49 | 50 | Entity.prototype.equals = function (that) { 51 | if (that == null) { 52 | return false; 53 | } 54 | if (this == that) { 55 | return true; 56 | } 57 | return this.identity.equals(that.getIdentity()); 58 | }; 59 | return Entity; 60 | })(); 61 | DDD.Entity = Entity; 62 | })(DDD || (DDD = {})); 63 | var DDD; 64 | (function (DDD) { 65 | var AsyncRepository = (function () { 66 | function AsyncRepository(core) { 67 | this.core = core; 68 | } 69 | AsyncRepository.prototype.resolve = function (identity) { 70 | var _this = this; 71 | return monapt.future(function (p) { 72 | p.success(_this.core.resolveOption(identity).get()); 73 | }); 74 | }; 75 | 76 | AsyncRepository.prototype.store = function (entity) { 77 | var _this = this; 78 | return monapt.future(function (p) { 79 | p.success(_this.core.store(entity)); 80 | }); 81 | }; 82 | 83 | AsyncRepository.prototype.storeList = function (entityList) { 84 | var _this = this; 85 | return monapt.future(function (p) { 86 | p.success(_this.core.storeList(entityList)); 87 | }); 88 | }; 89 | 90 | AsyncRepository.prototype.deleteByEntity = function (entity) { 91 | var _this = this; 92 | return monapt.future(function (p) { 93 | _this.core.deleteByEntity(entity); 94 | p.success(_this); 95 | }); 96 | }; 97 | 98 | AsyncRepository.prototype.deleteByIdentity = function (identity) { 99 | var _this = this; 100 | return monapt.future(function (p) { 101 | _this.core.deleteByIdentity(identity); 102 | p.success(_this); 103 | }); 104 | }; 105 | return AsyncRepository; 106 | })(); 107 | DDD.AsyncRepository = AsyncRepository; 108 | })(DDD || (DDD = {})); 109 | var DDD; 110 | (function (DDD) { 111 | var OnLocalStorageRepository = (function () { 112 | function OnLocalStorageRepository(mapper) { 113 | this.parse = mapper.parse; 114 | this.stringify = mapper.stringify; 115 | } 116 | OnLocalStorageRepository.prototype.resolveOption = function (identity) { 117 | var entity = this.resolve(identity); 118 | if (entity != null) { 119 | return new monapt.Some(entity); 120 | } else { 121 | return new monapt.None(); 122 | } 123 | }; 124 | 125 | OnLocalStorageRepository.prototype.resolve = function (identity) { 126 | var json = JSON.parse(localStorage.getItem(identity.getValue())); 127 | if (json) { 128 | return this.parse(json); 129 | } 130 | return null; 131 | }; 132 | 133 | OnLocalStorageRepository.prototype.store = function (entity) { 134 | localStorage.setItem(entity.getIdentity().getValue(), this.stringify(entity)); 135 | return entity; 136 | }; 137 | 138 | OnLocalStorageRepository.prototype.storeList = function (entityList) { 139 | for (var i in entityList) { 140 | this.store(entityList[i]); 141 | } 142 | return entityList; 143 | }; 144 | 145 | OnLocalStorageRepository.prototype.deleteByEntity = function (entity) { 146 | this.deleteByIdentity(entity.getIdentity()); 147 | return this; 148 | }; 149 | 150 | OnLocalStorageRepository.prototype.deleteByIdentity = function (identity) { 151 | localStorage.removeItem(identity.getValue()); 152 | return this; 153 | }; 154 | return OnLocalStorageRepository; 155 | })(); 156 | DDD.OnLocalStorageRepository = OnLocalStorageRepository; 157 | })(DDD || (DDD = {})); 158 | var DDD; 159 | (function (DDD) { 160 | var AsyncOnLocalStorageRepository = (function (_super) { 161 | __extends(AsyncOnLocalStorageRepository, _super); 162 | function AsyncOnLocalStorageRepository(mapper) { 163 | _super.call(this, new DDD.OnLocalStorageRepository(mapper)); 164 | } 165 | return AsyncOnLocalStorageRepository; 166 | })(DDD.AsyncRepository); 167 | DDD.AsyncOnLocalStorageRepository = AsyncOnLocalStorageRepository; 168 | })(DDD || (DDD = {})); 169 | var DDD; 170 | (function (DDD) { 171 | var OnMemoryRepository = (function () { 172 | function OnMemoryRepository() { 173 | this.entities = {}; 174 | } 175 | OnMemoryRepository.prototype.resolveOption = function (identity) { 176 | var entity = this.resolve(identity); 177 | if (entity != null) { 178 | return new monapt.Some(entity); 179 | } else { 180 | return new monapt.None(); 181 | } 182 | }; 183 | 184 | OnMemoryRepository.prototype.resolve = function (identity) { 185 | return this.entities[identity.getValue()]; 186 | }; 187 | 188 | OnMemoryRepository.prototype.store = function (entity) { 189 | this.entities[entity.getIdentity().getValue()] = entity; 190 | return entity; 191 | }; 192 | 193 | OnMemoryRepository.prototype.storeList = function (entityList) { 194 | for (var i in entityList) { 195 | this.store(entityList[i]); 196 | } 197 | return entityList; 198 | }; 199 | 200 | OnMemoryRepository.prototype.deleteByEntity = function (entity) { 201 | this.deleteByIdentity(entity.getIdentity()); 202 | return this; 203 | }; 204 | 205 | OnMemoryRepository.prototype.deleteByIdentity = function (identity) { 206 | delete this.entities[identity.getValue()]; 207 | return this; 208 | }; 209 | return OnMemoryRepository; 210 | })(); 211 | DDD.OnMemoryRepository = OnMemoryRepository; 212 | })(DDD || (DDD = {})); 213 | var DDD; 214 | (function (DDD) { 215 | var AsyncOnMemoryRepository = (function (_super) { 216 | __extends(AsyncOnMemoryRepository, _super); 217 | function AsyncOnMemoryRepository() { 218 | _super.call(this, new DDD.OnMemoryRepository()); 219 | } 220 | return AsyncOnMemoryRepository; 221 | })(DDD.AsyncRepository); 222 | DDD.AsyncOnMemoryRepository = AsyncOnMemoryRepository; 223 | })(DDD || (DDD = {})); 224 | var DDD; 225 | (function (DDD) { 226 | var OnSessionStorageRepository = (function () { 227 | function OnSessionStorageRepository(mapper) { 228 | this.parse = mapper.parse; 229 | this.stringify = mapper.stringify; 230 | } 231 | OnSessionStorageRepository.prototype.resolveOption = function (identity) { 232 | var entity = this.resolve(identity); 233 | if (entity != null) { 234 | return new monapt.Some(entity); 235 | } else { 236 | return new monapt.None(); 237 | } 238 | }; 239 | 240 | OnSessionStorageRepository.prototype.resolve = function (identity) { 241 | var item = sessionStorage.getItem(identity.getValue()); 242 | var json = item ? JSON.parse(item) : null; 243 | return json ? this.parse(json) : null; 244 | }; 245 | 246 | OnSessionStorageRepository.prototype.store = function (entity) { 247 | sessionStorage.setItem(entity.getIdentity().getValue(), this.stringify(entity)); 248 | return entity; 249 | }; 250 | 251 | OnSessionStorageRepository.prototype.storeList = function (entityList) { 252 | for (var i in entityList) { 253 | this.store(entityList[i]); 254 | } 255 | return entityList; 256 | }; 257 | 258 | OnSessionStorageRepository.prototype.deleteByEntity = function (entity) { 259 | this.deleteByIdentity(entity.getIdentity()); 260 | return this; 261 | }; 262 | 263 | OnSessionStorageRepository.prototype.deleteByIdentity = function (identity) { 264 | sessionStorage.removeItem(identity.getValue()); 265 | return this; 266 | }; 267 | return OnSessionStorageRepository; 268 | })(); 269 | DDD.OnSessionStorageRepository = OnSessionStorageRepository; 270 | })(DDD || (DDD = {})); 271 | var DDD; 272 | (function (DDD) { 273 | var AsyncOnSessionStorageRepository = (function (_super) { 274 | __extends(AsyncOnSessionStorageRepository, _super); 275 | function AsyncOnSessionStorageRepository(mapper) { 276 | _super.call(this, new DDD.OnSessionStorageRepository(mapper)); 277 | } 278 | return AsyncOnSessionStorageRepository; 279 | })(DDD.AsyncRepository); 280 | DDD.AsyncOnSessionStorageRepository = AsyncOnSessionStorageRepository; 281 | })(DDD || (DDD = {})); 282 | -------------------------------------------------------------------------------- /dddbase.min.js: -------------------------------------------------------------------------------- 1 | var __extends=this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);c.prototype=b.prototype,a.prototype=new c},DDD;!function(a){var b=function(){function a(a){this.value=a}return a.prototype.getValue=function(){return this.value},a.prototype.equals=function(a){return null==a?!1:this==a?!0:this.value===a.getValue()},a}();a.Identity=b;var c=function(a){function b(b){a.call(this,b)}return __extends(b,a),b}(b);a.NumberIdentity=c}(DDD||(DDD={}));var DDD;!function(a){var b=function(){function a(a){this.identity=a}return a.prototype.getIdentity=function(){return this.identity},a.prototype.equals=function(a){return null==a?!1:this==a?!0:this.identity.equals(a.getIdentity())},a}();a.Entity=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(){function a(a){this.core=a}return a.prototype.resolve=function(a){var b=this;return monapt.future(function(c){c.success(b.core.resolveOption(a).get())})},a.prototype.store=function(a){var b=this;return monapt.future(function(c){c.success(b.core.store(a))})},a.prototype.storeList=function(a){var b=this;return monapt.future(function(c){c.success(b.core.storeList(a))})},a.prototype.deleteByEntity=function(a){var b=this;return monapt.future(function(c){b.core.deleteByEntity(a),c.success(b)})},a.prototype.deleteByIdentity=function(a){var b=this;return monapt.future(function(c){b.core.deleteByIdentity(a),c.success(b)})},a}();a.AsyncRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(){function a(a){this.parse=a.parse,this.stringify=a.stringify}return a.prototype.resolveOption=function(a){var b=this.resolve(a);return null!=b?new monapt.Some(b):new monapt.None},a.prototype.resolve=function(a){var b=JSON.parse(localStorage.getItem(a.getValue()));return b?this.parse(b):null},a.prototype.store=function(a){return localStorage.setItem(a.getIdentity().getValue(),this.stringify(a)),a},a.prototype.storeList=function(a){for(var b in a)this.store(a[b]);return a},a.prototype.deleteByEntity=function(a){return this.deleteByIdentity(a.getIdentity()),this},a.prototype.deleteByIdentity=function(a){return localStorage.removeItem(a.getValue()),this},a}();a.OnLocalStorageRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(b){function c(c){b.call(this,new a.OnLocalStorageRepository(c))}return __extends(c,b),c}(a.AsyncRepository);a.AsyncOnLocalStorageRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(){function a(){this.entities={}}return a.prototype.resolveOption=function(a){var b=this.resolve(a);return null!=b?new monapt.Some(b):new monapt.None},a.prototype.resolve=function(a){return this.entities[a.getValue()]},a.prototype.store=function(a){return this.entities[a.getIdentity().getValue()]=a,a},a.prototype.storeList=function(a){for(var b in a)this.store(a[b]);return a},a.prototype.deleteByEntity=function(a){return this.deleteByIdentity(a.getIdentity()),this},a.prototype.deleteByIdentity=function(a){return delete this.entities[a.getValue()],this},a}();a.OnMemoryRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(b){function c(){b.call(this,new a.OnMemoryRepository)}return __extends(c,b),c}(a.AsyncRepository);a.AsyncOnMemoryRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(){function a(a){this.parse=a.parse,this.stringify=a.stringify}return a.prototype.resolveOption=function(a){var b=this.resolve(a);return null!=b?new monapt.Some(b):new monapt.None},a.prototype.resolve=function(a){var b=sessionStorage.getItem(a.getValue()),c=b?JSON.parse(b):null;return c?this.parse(c):null},a.prototype.store=function(a){return sessionStorage.setItem(a.getIdentity().getValue(),this.stringify(a)),a},a.prototype.storeList=function(a){for(var b in a)this.store(a[b]);return a},a.prototype.deleteByEntity=function(a){return this.deleteByIdentity(a.getIdentity()),this},a.prototype.deleteByIdentity=function(a){return sessionStorage.removeItem(a.getValue()),this},a}();a.OnSessionStorageRepository=b}(DDD||(DDD={}));var DDD;!function(a){var b=function(b){function c(c){b.call(this,new a.OnSessionStorageRepository(c))}return __extends(c,b),c}(a.AsyncRepository);a.AsyncOnSessionStorageRepository=b}(DDD||(DDD={})); -------------------------------------------------------------------------------- /definitions/chai/chai.d.ts: -------------------------------------------------------------------------------- 1 | declare module chai { 2 | export function expect(value: any): any; 3 | } -------------------------------------------------------------------------------- /definitions/mocha/mocha.d.ts: -------------------------------------------------------------------------------- 1 | // mocha.d.ts 2 | // 3 | // Mocha (c) 2011-2012 TJ Holowaychuk 4 | // 5 | // Hand written by Murat Girgin 6 | // based on http://visionmedia.github.com/mocha/ 7 | // 8 | 9 | declare var describe: { 10 | (testDescription: string, f: Function): any; 11 | only(testDescription: string, f: Function): any; 12 | skip(testDescription: string, f: Function): any; 13 | }; 14 | 15 | declare var context: { 16 | (testDescription: string, f?: Function): any; 17 | } 18 | 19 | declare var it: { 20 | (testDescription: string, f?: Function, done?: Function): any; 21 | only(testDescription: string, f?: Function, done?: Function): any; 22 | skip(testDescription: string, f?: Function, done?: Function): any; 23 | }; 24 | 25 | declare function before(f: Function, done?: Function): any; 26 | 27 | declare function after(f: Function, done?: Function): any; 28 | 29 | declare function beforeEach(f: Function, done?: Function): any; 30 | 31 | declare function afterEach(f: Function, done?: Function): any; 32 | -------------------------------------------------------------------------------- /definitions/monapt/monapt.d.ts: -------------------------------------------------------------------------------- 1 | ../../bower_components/monapt/monapt.d.ts -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "project", 3 | "version": "0.0.1", 4 | "devDependencies": { 5 | "grunt-contrib-clean": "~0.4.1", 6 | "grunt-contrib-concat": "~0.2.0", 7 | "grunt-contrib-copy": "~0.4.1", 8 | "grunt-typescript": "~0.2.0", 9 | "grunt-contrib-connect": "~0.3.0", 10 | "grunt-contrib-uglify": "~0.2.0", 11 | "grunt-regarde": "~0.1.1", 12 | "grunt": "~0.4.1", 13 | "grunt-exec": "~0.4.0" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/async_on_local_storage_repotitory.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export class AsyncOnLocalStorageRepository, E extends Entity> extends AsyncRepository { 9 | 10 | constructor(mapper: ILocalStorageMapper) { 11 | super(new OnLocalStorageRepository(mapper)); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/async_on_memory_repotitory.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export class AsyncOnMemoryRepository, E extends Entity> extends AsyncRepository { 9 | 10 | constructor() { 11 | super(new OnMemoryRepository()); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/async_on_session_storage_repotitory.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export class AsyncOnSessionStorageRepository, E extends Entity> extends AsyncRepository { 9 | 10 | constructor(mapper: ISessionStorageMapper) { 11 | super(new OnSessionStorageRepository(mapper)); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/async_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | 7 | module DDD { 8 | 9 | export class AsyncRepository, E extends Entity> { 10 | 11 | constructor(private core: IRepository) {} 12 | 13 | resolve(identity: ID): monapt.Future { 14 | return monapt.future(p => { 15 | p.success(this.core.resolveOption(identity).get()); 16 | }); 17 | } 18 | 19 | store(entity: E): monapt.Future { 20 | return monapt.future(p => { 21 | p.success(this.core.store(entity)); 22 | }); 23 | } 24 | 25 | storeList(entityList: E[]): monapt.Future { 26 | return monapt.future(p => { 27 | p.success(this.core.storeList(entityList)); 28 | }); 29 | } 30 | 31 | deleteByEntity(entity: E): monapt.Future> { 32 | return monapt.future>(p => { 33 | this.core.deleteByEntity(entity); 34 | p.success(this); 35 | }); 36 | } 37 | 38 | deleteByIdentity(identity: ID): monapt.Future> { 39 | return monapt.future>(p => { 40 | this.core.deleteByIdentity(identity); 41 | p.success(this); 42 | }); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/entity.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | module DDD { 4 | 5 | export class Entity> { 6 | 7 | constructor(private identity: ID) { } 8 | 9 | public getIdentity(): ID { 10 | return this.identity; 11 | } 12 | 13 | public equals(that: Entity): boolean { 14 | if (that == null) { 15 | return false; 16 | } 17 | if (this == that) { 18 | return true; 19 | } 20 | return this.identity.equals(that.getIdentity()); 21 | } 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/identity.ts: -------------------------------------------------------------------------------- 1 | module DDD { 2 | 3 | export class Identity { 4 | 5 | constructor(private value: T) { } 6 | 7 | public getValue(): T { 8 | return this.value; 9 | } 10 | 11 | public equals(that: Identity): boolean { 12 | if (that == null) { 13 | return false; 14 | } 15 | if (this == that) { 16 | return true; 17 | } 18 | 19 | return this.value === that.getValue(); 20 | } 21 | } 22 | 23 | export class NumberIdentity extends Identity { 24 | 25 | constructor(value: number) { 26 | super(value); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/on_local_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export interface ILocalStorageMapper> { 9 | parse(json: Object): E; 10 | stringify(entity: E): string; 11 | } 12 | 13 | export class OnLocalStorageRepository, E extends Entity> implements IRepository { 14 | 15 | constructor(mapper: ILocalStorageMapper) { 16 | this.parse = mapper.parse; 17 | this.stringify = mapper.stringify; 18 | } 19 | 20 | parse: (json: Object) => E; 21 | stringify: (entity: E) => string; 22 | 23 | resolveOption(identity: ID): monapt.Option { 24 | var entity = this.resolve(identity); 25 | if (entity != null) { 26 | return new monapt.Some(entity); 27 | } 28 | else { 29 | return new monapt.None(); 30 | } 31 | } 32 | 33 | resolve(identity: ID): E { 34 | var json = JSON.parse(localStorage.getItem(identity.getValue())); 35 | if (json) { 36 | return this.parse(json); 37 | } 38 | return null; 39 | } 40 | 41 | store(entity: E): E { 42 | localStorage.setItem(entity.getIdentity().getValue(), this.stringify(entity)); 43 | return entity; 44 | } 45 | 46 | storeList(entityList: E[]): E[] { 47 | for (var i in entityList) { 48 | this.store(entityList[i]); 49 | } 50 | return entityList; 51 | } 52 | 53 | deleteByEntity(entity: E): OnLocalStorageRepository { 54 | this.deleteByIdentity(entity.getIdentity()); 55 | return this; 56 | } 57 | 58 | deleteByIdentity(identity: ID): OnLocalStorageRepository { 59 | localStorage.removeItem(identity.getValue()); 60 | return this; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/on_memory_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export class OnMemoryRepository, E extends Entity> implements IRepository { 9 | private entities: Object = {}; 10 | 11 | resolveOption(identity: ID): monapt.Option { 12 | var entity = this.resolve(identity); 13 | if (entity != null) { 14 | return new monapt.Some(entity); 15 | } 16 | else { 17 | return new monapt.None(); 18 | } 19 | } 20 | 21 | resolve(identity: ID): E { 22 | return this.entities[identity.getValue()]; 23 | } 24 | 25 | store(entity: E): E { 26 | this.entities[entity.getIdentity().getValue()] = entity; 27 | return entity; 28 | } 29 | 30 | storeList(entityList: E[]): E[] { 31 | for (var i in entityList) { 32 | this.store(entityList[i]); 33 | } 34 | return entityList; 35 | } 36 | 37 | deleteByEntity(entity: E): OnMemoryRepository { 38 | this.deleteByIdentity(entity.getIdentity()); 39 | return this; 40 | } 41 | 42 | deleteByIdentity(identity: ID): OnMemoryRepository { 43 | delete this.entities[identity.getValue()]; 44 | return this; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/on_session_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | module DDD { 7 | 8 | export interface ISessionStorageMapper> { 9 | parse(json: Object): E; 10 | stringify(entity: E): string; 11 | } 12 | 13 | export class OnSessionStorageRepository, E extends Entity> implements IRepository { 14 | 15 | constructor(mapper: ISessionStorageMapper) { 16 | this.parse = mapper.parse; 17 | this.stringify = mapper.stringify; 18 | } 19 | 20 | parse: (json: Object) => E; 21 | stringify: (entity: E) => string; 22 | 23 | resolveOption(identity: ID): monapt.Option { 24 | var entity = this.resolve(identity); 25 | if (entity != null) { 26 | return new monapt.Some(entity); 27 | } 28 | else { 29 | return new monapt.None(); 30 | } 31 | } 32 | 33 | resolve(identity: ID): E { 34 | var item = sessionStorage.getItem(identity.getValue()); 35 | var json = item ? JSON.parse(item) : null; 36 | return json ? this.parse(json) : null; 37 | } 38 | 39 | store(entity: E): E { 40 | sessionStorage.setItem(entity.getIdentity().getValue(), this.stringify(entity)); 41 | return entity; 42 | } 43 | 44 | storeList(entityList: E[]): E[] { 45 | for (var i in entityList) { 46 | this.store(entityList[i]); 47 | } 48 | return entityList; 49 | } 50 | 51 | deleteByEntity(entity: E): OnSessionStorageRepository { 52 | this.deleteByIdentity(entity.getIdentity()); 53 | return this; 54 | } 55 | 56 | deleteByIdentity(identity: ID): OnSessionStorageRepository { 57 | sessionStorage.removeItem(identity.getValue()); 58 | return this; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | 5 | module DDD { 6 | 7 | export interface IRepository, E extends Entity> { 8 | resolveOption(identity: ID): monapt.Option; 9 | 10 | resolve(identity: ID): E; 11 | 12 | store(entity: E): E; 13 | 14 | storeList(entityList: E[]): E[]; 15 | 16 | deleteByEntity(entity: E): IRepository; 17 | 18 | deleteByIdentity(identity: ID): IRepository; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /test/async_on_local_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | var expect = chai.expect; 17 | 18 | describe('AsyncOnLocalStorageRepository', () => { 19 | 20 | var repository: AsyncOnLocalStorageRepository; 21 | var identity: DDD.NumberIdentity; 22 | var name: string; 23 | var person: Person; 24 | var identity2: DDD.NumberIdentity; 25 | var name2: string; 26 | var person2: Person; 27 | 28 | beforeEach(() => { 29 | repository = new AsyncOnLocalStorageRepository({ 30 | parse: (json: Object): Person => { 31 | return new Person(new NumberIdentity(json['identity']['value']), json['name']); 32 | }, 33 | stringify: (person: Person): string => { 34 | return JSON.stringify(person); 35 | } 36 | }); 37 | identity = new NumberIdentity(10); 38 | name = 'yaakaito'; 39 | person = new Person(identity, name); 40 | identity2 = new NumberIdentity(20); 41 | name2 = 'yaakaito2'; 42 | person2 = new Person(identity2, name2); 43 | 44 | }); 45 | 46 | afterEach(() => { 47 | localStorage.clear(); 48 | }); 49 | 50 | describe('#store', () => { 51 | it('should store entity, And future returns stored entity', (ok) => { 52 | repository.store(person).onSuccess(entity => { 53 | expect(entity).to.equal(person); 54 | ok(); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('#storeList', () => { 60 | it('should store entity list, And future returns stored entity list', (ok) => { 61 | var persons = [person, person2]; 62 | repository.storeList(persons).onSuccess(entityList => { 63 | expect(entityList).to.equal(persons); 64 | expect(entityList).to.be.length(2); 65 | ok(); 66 | }); 67 | }); 68 | }); 69 | 70 | describe('#resolve', () => { 71 | it('returns succeed Futrue if the entity is stored', (ok) => { 72 | repository.store(person).onSuccess(entity => { 73 | repository.resolve(identity).onSuccess(entity => { 74 | expect(entity.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 75 | expect(entity.name).to.equal(person.name); 76 | ok(); 77 | }); 78 | }); 79 | }); 80 | 81 | it('returns None if the entity is not stored', (ok) => { 82 | repository.resolve(identity).onFailure(error => { 83 | expect(error).to.not.be.null; 84 | ok(); 85 | }); 86 | }); 87 | }); 88 | 89 | describe('#deleteByEntity', () => { 90 | it('should delete stored entity if given it', (ok) => { 91 | repository.store(person).onSuccess(entity => { 92 | repository.deleteByEntity(person).onSuccess(repo => { 93 | repo.resolve(identity).onFailure(error => { 94 | expect(error).to.not.be.null; 95 | ok(); 96 | }); 97 | }); 98 | }); 99 | }); 100 | }); 101 | 102 | describe('#deleteByIdentity', () => { 103 | it('should deelte stored entity if given thats identify', (ok) => { 104 | repository.store(person).onSuccess(entity => { 105 | repository.deleteByIdentity(identity).onSuccess(repo => { 106 | repo.resolve(identity).onFailure(error => { 107 | expect(error).to.not.be.null; 108 | ok(); 109 | }); 110 | }); 111 | }); 112 | }); 113 | }); 114 | }); 115 | 116 | } 117 | -------------------------------------------------------------------------------- /test/async_on_memory_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | var expect = chai.expect; 17 | 18 | describe('AsyncOnMemoryRepository', () => { 19 | 20 | var repository: AsyncOnMemoryRepository; 21 | var identity: DDD.NumberIdentity; 22 | var name: string; 23 | var person: Person; 24 | var identity2: DDD.NumberIdentity; 25 | var name2: string; 26 | var person2: Person; 27 | 28 | beforeEach(() => { 29 | repository = new AsyncOnMemoryRepository(); 30 | identity = new NumberIdentity(10); 31 | name = 'yaakaito'; 32 | person = new Person(identity, name); 33 | identity2 = new NumberIdentity(20); 34 | name2 = 'yaakaito2'; 35 | person2 = new Person(identity2, name2); 36 | 37 | }); 38 | 39 | describe('#store', () => { 40 | it('should store entity, And future returns stored entity', (ok) => { 41 | repository.store(person).onSuccess(entity => { 42 | expect(entity).to.equal(person); 43 | ok(); 44 | }); 45 | }); 46 | }); 47 | 48 | describe('#storeList', () => { 49 | it('should store entity list, And future returns stored entity list', (ok) => { 50 | var persons = [person, person2]; 51 | repository.storeList(persons).onSuccess(entityList => { 52 | expect(entityList).to.equal(persons); 53 | expect(entityList).to.be.length(2); 54 | ok(); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('#resolve', () => { 60 | it('returns succeed Futrue if the entity is stored', (ok) => { 61 | repository.store(person).onSuccess(entity => { 62 | repository.resolve(identity).onSuccess(entity => { 63 | expect(entity).to.equal(person); 64 | ok(); 65 | }); 66 | }); 67 | }); 68 | 69 | it('returns None if the entity is not stored', (ok) => { 70 | repository.resolve(identity).onFailure(error => { 71 | expect(error).to.not.be.null; 72 | ok(); 73 | }); 74 | }); 75 | }); 76 | 77 | describe('#deleteByEntity', () => { 78 | it('should delete stored entity if given it', (ok) => { 79 | repository.store(person).onSuccess(entity => { 80 | repository.deleteByEntity(person).onSuccess(repo => { 81 | repo.resolve(identity).onFailure(error => { 82 | expect(error).to.not.be.null; 83 | ok(); 84 | }); 85 | }); 86 | }); 87 | }); 88 | }); 89 | 90 | describe('#deleteByIdentity', () => { 91 | it('should deelte stored entity if given thats identify', (ok) => { 92 | repository.store(person).onSuccess(entity => { 93 | repository.deleteByIdentity(identity).onSuccess(repo => { 94 | repo.resolve(identity).onFailure(error => { 95 | expect(error).to.not.be.null; 96 | ok(); 97 | }); 98 | }); 99 | }); 100 | }); 101 | }); 102 | }); 103 | 104 | } 105 | -------------------------------------------------------------------------------- /test/async_on_session_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | var expect = chai.expect; 17 | 18 | describe('AsyncOnSessionStorageRepository', () => { 19 | 20 | var repository: AsyncOnSessionStorageRepository; 21 | var identity: DDD.NumberIdentity; 22 | var name: string; 23 | var person: Person; 24 | var identity2: DDD.NumberIdentity; 25 | var name2: string; 26 | var person2: Person; 27 | 28 | beforeEach(() => { 29 | repository = new AsyncOnSessionStorageRepository({ 30 | parse: (json: Object): Person => { 31 | return new Person(new NumberIdentity(json['identity']['value']), json['name']); 32 | }, 33 | stringify: (person: Person): string => { 34 | return JSON.stringify(person); 35 | } 36 | }); 37 | identity = new NumberIdentity(10); 38 | name = 'yaakaito'; 39 | person = new Person(identity, name); 40 | identity2 = new NumberIdentity(20); 41 | name2 = 'yaakaito2'; 42 | person2 = new Person(identity2, name2); 43 | 44 | }); 45 | 46 | afterEach(() => { 47 | sessionStorage.clear(); 48 | }); 49 | 50 | describe('#store', () => { 51 | it('should store entity, And future returns stored entity', (ok) => { 52 | repository.store(person).onSuccess(entity => { 53 | expect(entity).to.equal(person); 54 | ok(); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('#storeList', () => { 60 | it('should store entity list, And future returns stored entity list', (ok) => { 61 | var persons = [person, person2]; 62 | repository.storeList(persons).onSuccess(entityList => { 63 | expect(entityList).to.equal(persons); 64 | expect(entityList).to.be.length(2); 65 | ok(); 66 | }); 67 | }); 68 | }); 69 | 70 | describe('#resolve', () => { 71 | it('returns succeed Futrue if the entity is stored', (ok) => { 72 | repository.store(person).onSuccess(entity => { 73 | repository.resolve(identity).onSuccess(entity => { 74 | expect(entity.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 75 | expect(entity.name).to.equal(person.name); 76 | ok(); 77 | }); 78 | }); 79 | }); 80 | 81 | it('returns None if the entity is not stored', (ok) => { 82 | repository.resolve(identity).onFailure(error => { 83 | expect(error).to.not.be.null; 84 | ok(); 85 | }); 86 | }); 87 | }); 88 | 89 | describe('#deleteByEntity', () => { 90 | it('should delete stored entity if given it', (ok) => { 91 | repository.store(person).onSuccess(entity => { 92 | repository.deleteByEntity(person).onSuccess(repo => { 93 | repo.resolve(identity).onFailure(error => { 94 | expect(error).to.not.be.null; 95 | ok(); 96 | }); 97 | }); 98 | }); 99 | }); 100 | }); 101 | 102 | describe('#deleteByIdentity', () => { 103 | it('should deelte stored entity if given thats identify', (ok) => { 104 | repository.store(person).onSuccess(entity => { 105 | repository.deleteByIdentity(identity).onSuccess(repo => { 106 | repo.resolve(identity).onFailure(error => { 107 | expect(error).to.not.be.null; 108 | ok(); 109 | }); 110 | }); 111 | }); 112 | }); 113 | }); 114 | }); 115 | 116 | } 117 | -------------------------------------------------------------------------------- /test/entity.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | 6 | 7 | class Person extends DDD.Entity { 8 | 9 | constructor(identity: DDD.NumberIdentity, public name: string) { 10 | super(identity); 11 | } 12 | } 13 | 14 | module DDD.Spec { 15 | 16 | describe('Entity', () => { 17 | 18 | var expect = chai.expect; 19 | 20 | var identity; 21 | var person; 22 | beforeEach(() => { 23 | identity = new DDD.NumberIdentity(10); 24 | person = new Person(identity, 'yaakaito'); 25 | }); 26 | 27 | it('has identity', () => { 28 | expect(person.getIdentity().getValue()).to.equal(10); 29 | }); 30 | 31 | it('has name property', () => { 32 | expect(person.name).to.equal('yaakaito'); 33 | }); 34 | 35 | describe('equals method', () => { 36 | 37 | it('should be true if given self', () => { 38 | expect(person.equals(person)).to.be.true; 39 | }); 40 | 41 | it('should be true if given entity that has equiv identity.', () => { 42 | var right = new Person(identity, 'yaakaito2'); 43 | expect(person.equals(right)).to.be.true; 44 | }); 45 | 46 | it('should be false if given null', () => { 47 | expect(person.equals(null)).to.be.false; 48 | }); 49 | 50 | it('should be false if given entity that has not equiv identity', () => { 51 | var rightIdenfity = new DDD.NumberIdentity(20); 52 | var right = new Person(rightIdenfity, 'yaakaito2'); 53 | expect(person.equals(right)).to.be.false; 54 | }); 55 | }); 56 | }); 57 | } -------------------------------------------------------------------------------- /test/identify.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | 5 | module DDD.Spec { 6 | 7 | describe('Identity', () => { 8 | 9 | var expect = chai.expect; 10 | 11 | describe('NumberIdentity', () => { 12 | 13 | var identity; 14 | beforeEach(() => { 15 | identity = new DDD.NumberIdentity(10); 16 | }); 17 | 18 | it('can create by number', () => { 19 | expect(identity.getValue()).to.equal(10); 20 | }); 21 | 22 | describe('equals method', () => { 23 | 24 | it('should be true if given self', () => { 25 | expect(identity.equals(identity)).to.be.true; 26 | }); 27 | 28 | it('should be true if given idenfity that has equiv value', () => { 29 | var right = new DDD.NumberIdentity(10); 30 | expect(identity.equals(right)).to.be.true; 31 | }); 32 | 33 | it('should be false if given null', () => { 34 | expect(identity.equals(null)).to.be.false; 35 | }); 36 | 37 | it('should be false if given idenfity that has not equiv value', () => { 38 | var right = new DDD.NumberIdentity(20); 39 | expect(identity.equals(right)).to.be.false; 40 | }); 41 | 42 | }); 43 | }); 44 | }); 45 | } -------------------------------------------------------------------------------- /test/on_local_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | var expect = chai.expect; 17 | 18 | describe('OnLocalStorageRepository', () => { 19 | 20 | var repository: OnLocalStorageRepository; 21 | var identity: DDD.NumberIdentity; 22 | var name: string; 23 | var person: Person; 24 | var identity2: DDD.NumberIdentity; 25 | var name2: string; 26 | var person2: Person; 27 | 28 | beforeEach(() => { 29 | repository = new OnLocalStorageRepository({ 30 | parse: (json: Object): Person => { 31 | return new Person(new NumberIdentity(json['identity']['value']), json['name']); 32 | }, 33 | stringify: (person: Person): string => { 34 | return JSON.stringify(person); 35 | } 36 | }); 37 | identity = new NumberIdentity(10); 38 | name = 'yaakaito'; 39 | person = new Person(identity, name); 40 | identity2 = new NumberIdentity(20); 41 | name2 = 'yaakaito2'; 42 | person2 = new Person(identity2, name2); 43 | }); 44 | 45 | afterEach(() => { 46 | localStorage.clear(); 47 | }); 48 | 49 | describe('#store', () => { 50 | it('can store entity, And can select it', () => { 51 | var stored = repository.store(person); 52 | expect(stored).to.equal(person); 53 | 54 | var resolved = repository.resolve(identity); 55 | expect(resolved.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 56 | expect(resolved.name).to.equal(person.name); 57 | }); 58 | }); 59 | 60 | describe('#storeList', () => { 61 | it('can store entity list, And can select them', () => { 62 | var persons = [person, person2]; 63 | var stored = repository.storeList(persons); 64 | expect(stored).to.equal(persons); 65 | 66 | var resolved = repository.resolve(identity); 67 | expect(resolved.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 68 | expect(resolved.name).to.equal(person.name); 69 | var resolved2 = repository.resolve(identity2); 70 | expect(resolved2.getIdentity().getValue()).to.equal(person2.getIdentity().getValue()); 71 | expect(resolved2.name).to.equal(person2.name); 72 | }); 73 | }); 74 | 75 | describe('#resolveOption', () => { 76 | it('returns Some if the entity is stored', () => { 77 | repository.store(person); 78 | 79 | var option = repository.resolveOption(identity); 80 | expect(option.isEmpty).to.be.false; 81 | expect(option.get().getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 82 | }); 83 | 84 | it('returns None if the entity is not stored', () => { 85 | var option = repository.resolveOption(identity); 86 | expect(option.isEmpty).to.be.true; 87 | }); 88 | }); 89 | 90 | describe('#deleteByEntity', () => { 91 | it('should delete stored entity if given it', () => { 92 | repository.store(person); 93 | 94 | repository.deleteByEntity(person); 95 | var resolved = repository.resolve(identity); 96 | 97 | expect(resolved).to.be.null; 98 | }); 99 | }); 100 | 101 | describe('#deleteByIdentity', () => { 102 | it('should delete stored entity if given thats identify', () => { 103 | repository.store(person); 104 | 105 | repository.deleteByIdentity(identity); 106 | var resolved = repository.resolve(identity); 107 | 108 | expect(resolved).to.be.null; 109 | }) 110 | }); 111 | }); 112 | 113 | } 114 | -------------------------------------------------------------------------------- /test/on_memory_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | 17 | var expect = chai.expect; 18 | 19 | describe('OnMemoryRepository', () => { 20 | 21 | var repository: OnMemoryRepository; 22 | var identity: DDD.NumberIdentity; 23 | var name: string; 24 | var person: Person; 25 | var identity2: DDD.NumberIdentity; 26 | var name2: string; 27 | var person2: Person; 28 | 29 | beforeEach(() => { 30 | repository = new OnMemoryRepository(); 31 | identity = new NumberIdentity(10); 32 | name = 'yaakaito'; 33 | person = new Person(identity, name); 34 | identity2 = new NumberIdentity(20); 35 | name2 = 'yaakaito2'; 36 | person2 = new Person(identity2, name2); 37 | 38 | 39 | }); 40 | 41 | describe('#store', () => { 42 | it('can store entity, And can select it', () => { 43 | var stored = repository.store(person); 44 | expect(stored).to.equal(person); 45 | 46 | var resolved = repository.resolve(identity); 47 | expect(resolved).to.equal(person); 48 | }); 49 | }); 50 | 51 | describe('#storeList', () => { 52 | it('can store entity list, And can select them', () => { 53 | var persons = [person, person2]; 54 | var stored = repository.storeList(persons); 55 | expect(stored).to.equal(persons); 56 | 57 | var resolved = repository.resolve(identity); 58 | expect(resolved).to.equal(person); 59 | var resolved2 = repository.resolve(identity2); 60 | expect(resolved2).to.equal(person2); 61 | }); 62 | }); 63 | 64 | describe('#resolveOption', () => { 65 | it('returns Some if the entity is stored', () => { 66 | repository.store(person); 67 | 68 | var option = repository.resolveOption(identity); 69 | expect(option.isEmpty).to.be.false; 70 | expect(option.get()).to.equal(person); 71 | }); 72 | 73 | it('returns None if the entity is not stored', () => { 74 | var option = repository.resolveOption(identity); 75 | expect(option.isEmpty).to.be.true; 76 | }); 77 | }); 78 | 79 | describe('#deleteByEntity', () => { 80 | it('should delete stored entity if given it', () => { 81 | repository.store(person); 82 | 83 | repository.deleteByEntity(person); 84 | var resolved = repository.resolve(identity); 85 | 86 | expect(resolved).to.be.undefined; 87 | }); 88 | }); 89 | 90 | describe('#deleteByIdentity', () => { 91 | it('should deelte stored entity if given thats identify', () => { 92 | repository.store(person); 93 | 94 | repository.deleteByIdentity(identity); 95 | var resolved = repository.resolve(identity); 96 | 97 | expect(resolved).to.be.undefined; 98 | }) 99 | }); 100 | }); 101 | 102 | } 103 | -------------------------------------------------------------------------------- /test/on_session_storage_repository.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | /// 4 | /// 5 | /// 6 | 7 | module DDD.Spec { 8 | 9 | class Person extends Entity { 10 | 11 | constructor(identity: DDD.NumberIdentity, public name: string) { 12 | super(identity); 13 | } 14 | } 15 | 16 | var expect = chai.expect; 17 | 18 | describe('OnSessionStorageRepository', () => { 19 | 20 | var repository: OnSessionStorageRepository; 21 | var identity: DDD.NumberIdentity; 22 | var name: string; 23 | var person: Person; 24 | var identity2: DDD.NumberIdentity; 25 | var name2: string; 26 | var person2: Person; 27 | 28 | beforeEach(() => { 29 | repository = new OnSessionStorageRepository({ 30 | parse: (json: Object): Person => { 31 | return new Person(new NumberIdentity(json['identity']['value']), json['name']); 32 | }, 33 | stringify: (person: Person): string => { 34 | return JSON.stringify(person); 35 | } 36 | }); 37 | identity = new NumberIdentity(10); 38 | name = 'yaakaito'; 39 | person = new Person(identity, name); 40 | identity2 = new NumberIdentity(20); 41 | name2 = 'yaakaito2'; 42 | person2 = new Person(identity2, name2); 43 | }); 44 | 45 | afterEach(() => { 46 | sessionStorage.clear(); 47 | }); 48 | 49 | describe('#store', () => { 50 | it('can store entity, And can select it', () => { 51 | var stored = repository.store(person); 52 | expect(stored).to.equal(person); 53 | 54 | var resolved = repository.resolve(identity); 55 | expect(resolved.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 56 | expect(resolved.name).to.equal(person.name); 57 | }); 58 | }); 59 | 60 | describe('#storeList', () => { 61 | it('can store entity list, And can select them', () => { 62 | var persons = [person, person2]; 63 | var stored = repository.storeList(persons); 64 | expect(stored).to.equal(persons); 65 | 66 | var resolved = repository.resolve(identity); 67 | expect(resolved.getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 68 | expect(resolved.name).to.equal(person.name); 69 | var resolved2 = repository.resolve(identity2); 70 | expect(resolved2.getIdentity().getValue()).to.equal(person2.getIdentity().getValue()); 71 | expect(resolved2.name).to.equal(person2.name); 72 | }); 73 | }); 74 | 75 | describe('#resolveOption', () => { 76 | it('returns Some if the entity is stored', () => { 77 | repository.store(person); 78 | 79 | var option = repository.resolveOption(identity); 80 | expect(option.isEmpty).to.be.false; 81 | expect(option.get().getIdentity().getValue()).to.equal(person.getIdentity().getValue()); 82 | }); 83 | 84 | it('returns None if the entity is not stored', () => { 85 | var option = repository.resolveOption(identity); 86 | expect(option.isEmpty).to.be.true; 87 | }); 88 | }); 89 | 90 | describe('#deleteByEntity', () => { 91 | it('should delete stored entity if given it', () => { 92 | repository.store(person); 93 | 94 | repository.deleteByEntity(person); 95 | var resolved = repository.resolve(identity); 96 | 97 | expect(resolved).to.be.null; 98 | }); 99 | }); 100 | 101 | describe('#deleteByIdentity', () => { 102 | it('should delete stored entity if given thats identify', () => { 103 | repository.store(person); 104 | 105 | repository.deleteByIdentity(identity); 106 | var resolved = repository.resolve(identity); 107 | 108 | expect(resolved).to.be.null; 109 | }) 110 | }); 111 | }); 112 | 113 | } 114 | -------------------------------------------------------------------------------- /testem.json: -------------------------------------------------------------------------------- 1 | { 2 | "framework" : "mocha+chai", 3 | "before_tests" : "grunt compile", 4 | "src_files" : [ 5 | "src/**/*.ts", 6 | "test/**/*.ts" 7 | ], 8 | "serve_files" : [ 9 | "bower_components/monapt/monapt.js", 10 | "dddbase.js", 11 | "compiled/test/**/*.js" 12 | ] 13 | } --------------------------------------------------------------------------------