Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.47 KB

big-project-tips.md

File metadata and controls

54 lines (39 loc) · 1.47 KB

Big project tips

As your project grows there are several patterns that might be useful to help with container maintenance.

Namespace services names

With createNamespace helper you can easily create a simple object with service names you can reference later.

import {createNamespace, namespaceEntry as r} from 'alpha-dic';

const names = createNamespace({
    worker: r,
    repository: {
        user: r,
        userTemplates: 'user-templates'
    }
});

names.worker; // 'worker'
names.repository.user; // 'repository.user'
names.repository.userTemplates; // 'repository.userTemplates'
names.unknownServiceName; // throws na error


@Service()
class Foo {
    constructor(@Inject(names.repository.user) userRepository) {
        
    }
}

Sanity check test

That's a one of the best life savior. Just try to get instance of all services! Stub ones you'd like to omit and check whether container is properly defined (no circular dependencies)

describe('sanity check', () => {
    const container = getContainerReference(); // just get an instance of container somehow
    
    // stub some definitions
    
    container.findByName('mongo').useFactory(() => {});
    
    for (const definition of container.findByPredicate(() => true)) {
        it('Service ${definition.name}', () => {
            // if definition is wrong then rejected promise gets returned thus failing your test
            return container.get(definition);
        })
    }
    
})