1

I have read to documentation and can't find anything about resolving a type and at the same time override some of its dependencies. Easiest to illustrate with an example

public class A
{
    public A(IServiceA a, IServiceB b) {}    
}
// Resolve scenarion
type => 
{
    // type is A
    var a = Container.Resolve<IServiceA>();
    a.SomeProperty = "magic";
    return Container.Resolve(type) // TODO: How to resolve A using a
}

Does it make sense? Was looking for something like

return Container.Resolve(type, Rule.Override.TypeOf<IServiceA>(a));

Great job with DryIoc

Edit (2016-05-26) My question was missleading. Below is complete code example (for prism)

ViewModelLocationProvider.SetDefaultViewModelFactory((view, type) =>
{
     var page = view as Page;
     if (page != null)
     {                    
          var navigationService = Container.Resolve<INavigationService>();
          ((IPageAware)navigationService).Page = page;
          var @override = Container.Resolve<Func<INavigationService, type>>(); // How to do this
          return @override(navigationService);
     }
     return Container.Resolve(type);
});
joacar
  • 891
  • 11
  • 28

1 Answers1

2

Resolve as Func with parameter you want to pass:

var factory = Container.Resolve<Func<IServiceA, A>>();
var result = factory(a);

Update:

Given the runtime Type to resolve:

type => 
{
    // type is A
    var a = Container.Resolve<IServiceA>();
    a.SomeProperty = "magic";

    // Asking for required service type, but wrapped in Func returning object
    var factory = Container.Resolve<Func<IServiceA, object>>(requiredServiceType: type);
    return factory(a);
}
dadhi
  • 4,807
  • 19
  • 25
  • Looks promosing. I see that I stated my question a bit missleading since parameter `type` is `Type` and not known at compile time... Any thoughts? – joacar May 26 '16 at 06:49
  • Sorry, did not so your edit, but the Update should work for it too. – dadhi May 26 '16 at 07:01
  • @dadhi How can I override an existing registration? I.e. I have `container.Register()`, but later on, I want to call `container.Register()` the reason I want to do this is registering the default services first, the from app modules (if they're available), upgrade those services when the modules are initialized. – Shimmy Weitzhandler Mar 26 '18 at 05:42
  • Use `container.Register(ifAlreadyRegistered: IfAlreadyRegistered.Replace)`, but if you're already resolved the first registration from the same container, be aware of https://bitbucket.org/dadhi/dryioc/wiki/UnregisterAndResolutionCache – dadhi Mar 26 '18 at 06:36