Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do I understand func with arguments correct #87

Unanswered
Nevca asked this question in Q&A
Discussion options

I'm looking at this example.

As far as I understand this is the way of binding a factory, so that all dependencies of the returned instance get resolved automatically, except for the func arguments, which are being "overridden".

Is this correct, is this line:
ctx.Inject<Dependency>(out var dependency);
the equivalent of writing this using Unity:
container.Resolve<IDependency>(new ParameterOverride("dependencyId", dependencyId));

You must be logged in to vote

Replies: 1 comment

Comment options

@Nevca I'm not sure if this is the same. In example, the Bind<int>().To<int>("dependencyId") call specifies to use this code as it is: dependencyId when resolving a dependency of type int.
In the example, near the end, there is also code that will be generated:

public IService Root
 {
 [MethodImpl(MethodImplOptions.AggressiveInlining)]
 get
 {
 Func<int, int, IDependency> transientFunc1 =
 [MethodImpl(MethodImplOptions.AggressiveInlining)]
 (dependencyId, subId) =>
 {
 int transientInt324 = subId;
 int transientInt323 = dependencyId;
 if (_root._singletonClock43 is null)
 {
 using (_lock.EnterScope())
 {
 if (_root._singletonClock43 is null)
 {
 _root._singletonClock43 = new Clock();
 }
 }
 }
 Dependency localDependency65 = new Dependency(_root._singletonClock43, transientInt323, transientInt324);
 return localDependency65;
 };
 return new Service(transientFunc1);
 }
 }

Pay attention to: int transientInt323 = dependencyId;.

And if the binding is changed to Bind<int>().To<int>("999"), the generated code will change to: int transientInt323 = 999;

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /