hexMachina icon indicating copy to clipboard operation
hexMachina copied to clipboard

Injector and hasMapping

Open CrazyFlasher opened this issue 6 years ago • 2 comments

How can I get hasClassToValueMapping, hasClassNameToValueMapping, hasClassToTypeMapping, hasClassNameToTypeMapping?

I see only 1 method hasMapping. What actually it returns? type to class?

CrazyFlasher avatar Oct 23 '19 13:10 CrazyFlasher

It returns true if a type is mapped to something... meaning that injector.getInstance(<Type>) will return a value and not throw an exception. It doesn't really matter if it is value or type mapping because injector can only have one mapping per type anyway (unless it's named but that's not the point here)

Think of it this way - optional injection is pretty much if(injector.hasMapping(field.type)) field.value = injector.getInstance(field.type); where field is the one that is being injected into.

Does it make sense?

st3veV avatar Oct 23 '19 13:10 st3veV

Not exactly. Here is simple example

        var injector:IDependencyInjector = new Injector();

        injector.mapToType(IMockPool_1, MockPool_1);
        Assert.isTrue(injector.hasMapping(IMockPool_1));

        injector.mapToValue(IMockPool_1, injector.getInstance(IMockPool_1));
//        not implemented?
//        Assert.isTrue(injector.hasMappingToValue(IMockPool_1));

        injector.mapClassNameToType("mock.obj.IMockObj_2", MockObj_2);
//        not implemented?
//        Assert.isTrue(injector.hasClassNameMapping("mock.obj.IMockObj_2"));

        var arr:Array<Int> = [1, 2, 3];
        injector.mapClassNameToValue("Array<Int>", arr);
//        not implemented?
//        Assert.isTrue(injector.hasClassNameMappingToValue("Array<Int>"));

CrazyFlasher avatar Oct 23 '19 16:10 CrazyFlasher