views:

256

answers:

2

I'm registering few modules in my Prism application using UnityBootstrapper

protected override IModuleCatalog GetModuleCatalog()
{
    var catalog = new ModuleCatalog();
    catalog
       .AddModule(typeof(LoginModule))
       .AddModule(typeof(AppModule))
       .AddModule(typeof(DataTransformationModule), InitializationMode.OnDemand)
       .AddModule(typeof(SyncModule), InitializationMode.OnDemand);

    return catalog;
}

Later I'm loading those modules which are set to load OnDemand dynamically in response to user actions. Though I were able to load modules OnDemand from other modules, types I registered in OnDemand loading modules were not getting Resolved.

public class SyncModule : IModule
{
    private readonly IUnityContainer container;

    public SyncModule(IUnityContainer container)
    {
        this.container = container;
    }

    public void Initialize()
    {
        this.RegisterViewsAndServices();

        ISyncController controller = this.container.Resolve<ISyncController>();
        controller.Run();
    }

    protected void RegisterViewsAndServices()
    {
        this.container.RegisterType<ISyncController, SyncController>();
        this.container.RegisterType<ISyncAnchorsRepository, SyncAnchorsRepository>();
        this.container.RegisterType<ISyncService, SyncService>();
        this.container.RegisterType<IView, SynchronizeView>("SynchronizeView");
        this.container.RegisterType<IView, SyncTrayView>("SyncTrayView");
    }
}

When I try to load any of the types registered in SyncModule (shown above) from another Module compiler throws an ResolutionFailedException since each module is . Is there anyway to inject same instance of IUnityContainer to all Modules? (Is it going to be a abuse of Prism?)

A: 

Look at the full message of the ResolutionFailedException. Altough somewhat criptic, it usually contains detailed information about what has failed. Usually this exception is thrown for a problem not on the type being resolved itself, but on one of the dependencies injected via constructor parameters.

Konamiman
I can resolve these types without any issue from the Module it was registered to the container. I'm getting this exception in other modules.
Raj
He's saying you need to look at the inner exceptions to ensure that you are looking at the REAL error. Alternatively you can use ExceptionExtensions.GetRootException(exception) to get the root exception, rather than the containing resolution error.
Anderson Imes
A: 

My mistake. I was using Type.GetType without fully qualified name of the assembly to dynamically load types from other Modules. Types registered to Unity container from modules are available for resolution elsewhere.

Raj
You should 1) always show us your real code 2) not be using Type.GetType for any of this. If you have types that you need to use across modules, have those modules share a central library. I usually call this a "Contracts" assembly. If you are sharing something like an ISyncService, simply do unityContainer.Resolve<Contracts.ISyncService>(), rather than (what I think you are doing) unityContainer.Resolve(Type.GetType("SyncModule.ISyncService, SyncModule"));
Anderson Imes
Hi Anderson, could you please have a look into this question: http://stackoverflow.com/questions/1678423/what-is-the-best-method-to-load-views-dynamically-from-a-navigation-control-in-pr I've elaborated the scenario where i'm using GetType() with code sample.
Raj