Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolving with custom parameter values #91

Closed
bonesoul opened this issue Jun 26, 2020 · 9 comments
Closed

resolving with custom parameter values #91

bonesoul opened this issue Jun 26, 2020 · 9 comments
Assignees
Labels
Milestone

Comments

@bonesoul
Copy link

I've a named registration of;

_container.Register<IVariantSubproduct, StainlessSteelPlate>("PAS.LEV");

and definition of the class is

        public StainlessSteelPlate(ISubproduct subproduct):base(subproduct)
        { }

So i can named-resolve it like;

return _resolver.Resolve<IVariantSubproduct>(code);

but how can I supply the value of parameter?

return _resolver.Resolve<IVariantSubproduct>(code, params?);

@bonesoul
Copy link
Author

I tried changing my factory resolver to;

        public IVariantSubproduct GetVariantSubproduct(string code, ISubproduct subproduct)
        {
            return _resolver.Resolve<IVariantSubproduct>(code, false, new object[] {subproduct});
        }

and calling it with;

_subproductFactory.GetVariantSubproduct(x.Code, x);

but getting

Cannot resolve type WareEngine.Modules.Stocks.Models.Subproducts.Variant.IVariantSubproduct.
Service is not registered or unresolvable type requested

@z4kn4fein
Copy link
Owner

Hey, the second one is the proper way to pass a parameter, however, it's not working now because of a bug. I'm going to fix it now and publish a new version.
Thanks for reporting!

z4kn4fein added a commit that referenced this issue Jun 29, 2020
@z4kn4fein
Copy link
Owner

Could you please check the latest pre-release version? Thanks!

@bonesoul
Copy link
Author

Okay i'll let you know.

meanwhile i used this workaround

return _resolver.ResolveFactory<ISubproduct, IVariantSubproduct>(subproduct.Code)(subproduct);

@z4kn4fein z4kn4fein self-assigned this Jun 29, 2020
@z4kn4fein z4kn4fein added the bug label Jun 29, 2020
@z4kn4fein z4kn4fein added this to the v3.2.0 milestone Jun 29, 2020
@bonesoul
Copy link
Author

bonesoul commented Jul 2, 2020

okay
return _resolver.Resolve<IVariantSubproduct>(code, false, new object[] {subproduct});

works all good now but I guess I've found a better way to accomplish what i need;

return _resolver.ResolveFactory<ISubproduct, IVariantSubproduct>(subproduct.Code)(subproduct);

@z4kn4fein
Copy link
Owner

Thanks for the feedback!
Sure, that works too, but I would recommend saving the generated factory somewhere because although it's cached by the container, it could be recreated when the dependency tree changes.

@bonesoul
Copy link
Author

bonesoul commented Jul 9, 2020

so when it get's recreated will i have problems accessing the items or your recommendation is just sake of optimization?

@z4kn4fein
Copy link
Owner

Just for optimization purposes, the factory remains stable. The container wipes out the whole cache only when you register something after you created the factory and if you do that frequently it could be regenerated over and over again, but if it's not the case, it'll remain cached, so it's fine with your version as well.

@bonesoul
Copy link
Author

bonesoul commented Jul 9, 2020

Ah I do registrations only while initial initializing, still thanks for the heads up and great library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants