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

Specific key with WithCtorParam #268

Open
silkfire opened this issue Aug 9, 2020 · 6 comments
Open

Specific key with WithCtorParam #268

silkfire opened this issue Aug 9, 2020 · 6 comments

Comments

@silkfire
Copy link
Contributor

silkfire commented Aug 9, 2020

When using the WithCtorParam method with a single argument and in need to resolve a dependency with specific key, I know that you can use LocateWithKey like this:

_.Export<Service>().WithCtorParam<IDependency>().LocateWithKey("key")
 .As<IService>();

But how do you solve this with multiple dependency arguments?

_.Export<Service>().WithCtorParam<ISubDependecy1, ISubDependency2, IDependency>((sd1, sd2) => new Dependency(sd1, sd2))
 .As<IService>();

ISubDependecy1 and ISubDependency2 are previously registered and with different keys.

@ipjohnson
Copy link
Owner

I think at the moment there isn't really a way to handle. This maybe a case where you have to hand wire this instance. I'll look at what it would take to do something like WithCtorParam<T>(Func<IExportLocatorScope,T> func) where you can wire up the specific logic you need for wiring up the dependency,

@silkfire
Copy link
Contributor Author

silkfire commented Aug 12, 2020

Thanks, that sounds like a good solution. Perhaps an exception should be thrown if you use the multi-type methods and then LocateWithKey as the behavior would be undefined?

_.Export<Service>().WithCtorParam<ISubDependecy1, ISubDependency2, IDependency>((sd1, sd2) => new Dependency(sd1, sd2)).LocateWithKey("key")    // Throws no error, but which dependency are we referring to?
 .As<IService>();

I've got a separate key-related question so I'll just cheekily ask it here. I'm registering a dependency by key. In my controller I have that dependency in the constructor, without any [Import] attribute. But I'm getting an exception that Grace can't resolve the dependency. I thought it should just try to pick the last registered dependency that matches the type, ignoring the key?

@ipjohnson
Copy link
Owner

Hmm that's tough the container isn't going to resolve that because you specifically registered the dependency with a key and the controller constructor has no way to decided which dependency to use.

You should be able to put an [Import(key = "SomeKey")] on the parameter and that should allow you to specify the key.

@silkfire
Copy link
Contributor Author

I'm not a big fan of the [Import] attribute tbh. Would it be possible to enable this kind of functionality through a flag? Eg. AutoResolveKeyedRegistrations. This will pick the last registration based on type, ignoring any keys.

@ipjohnson
Copy link
Owner

Honestly I'm not a huge fan either but I also don't do keyed registration either. I think it could be an option to have a flag like that but I probably can't implement it any time soon. What if you exported a non keyed version that just looks up the keyed version you want?

@silkfire
Copy link
Contributor Author

I suppose I could do that, yeah. Thanks for the tip!

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

No branches or pull requests

2 participants