container icon indicating copy to clipboard operation
container copied to clipboard

Pass resolver overrides to the factory method of RegisterFactory

Open hypercodeplace opened this issue 6 years ago • 3 comments

Description

.RegisterFactory should take factory with set of ResolverOverride passed by Resolve.

Problem

Current implementation of .RegisterFactory doesn't allow to take ResolverOverride[] passed by .Resolve like name or type. The following is an easy example:

var container = new UnityContainer()
    .RegisterFactory<IFoo>((c, t, n) =>
        {
            var result = c.Resolve(t, otherName);
            // let's assume here is some additional logic to update "result"
            return result;
        }
);

var parameterOverride = new ParameterOverride("fieldName", 42);
var foo = container.Resolve<IFoo>(parameterOverride);

// Current implementation doesn't allow to pass `parameterOverride` to the inner `.Resolve`.

There is also real-life example.

Our company uses LazyProxy to be able to resolve dependencies by UnityContainer in "lazy"style.

For now we use 5.8.13 version of UnityContainer and custom InjectionFactory to register dependencies.

In 5.9.x versions of UnityContainer InjectionFactory is marked as obsolete and it will be removed in next release. Therefore we will try to migrate LazyProxy to new .RegisterFactory method. With suggested changes the mentioned registration can be written as followed:

return container
    .RegisterType(typeFrom, typeTo, registrationName, typeLifetimeManager, injectionMembers)
    .RegisterFactory(typeFrom, name,
        (c, t, n, o) => LazyProxyBuilder.CreateInstance(t,
            () => c.Resolve(t, registrationName, o)),
        factoryLifetimeManager);

Solution

Add resolver overrides to the signature of .RegisterFactory.

A possible solution is presented at the following pull requests: Abstractions: https://github.com/unitycontainer/abstractions/pull/98 Container: https://github.com/unitycontainer/container/pull/148

hypercodeplace avatar Mar 19 '19 05:03 hypercodeplace

Hi @ENikS, In the description of this issue, I suggested pull requests with implementation, but they were closed. Now I see that you are going to add this functionality to version 6.0.0. I would still be happy to help you with this. Tell me, please, in which branch do I need to direct pull requests so that you can review them again?

hypercodeplace avatar Apr 14 '20 20:04 hypercodeplace

All new development is done on ‘develop’. I would prefer discussing it first, before you spend any time on implementation. I have some plans, perhaps we could synchronize...

There is new factory signature to allow these overrides. I am open to suggestions, if you have any.

Could you list scenarios where you trying to use these?

ENikS avatar Apr 14 '20 20:04 ENikS

Could you list scenarios where you trying to use these?

Yes, of course, this is a very simple scenario - to create a class through a factory registration, taking into account resolve overrides.

hypercodeplace avatar Apr 15 '20 22:04 hypercodeplace