简体   繁体   中英

Is possible Unity IoC of Prism get all the common interfaces from the modules?

Maybe the title is not so clear or am I using the wrong mechanism

Imagine this scenario

SOLUTION
|
|_ MODULE 1
   |_Factory1 (class) : IFactory (interface)
|
|_ MODULE 2
   |_Factory2 : IFactory
|
|_ SHELL
   |_List<IFactory>

I need a way that the system can get together all the classes which derives from IFactory in Shell (List). Is there a way to do this? I don't plan want to have a super large conditional list of each class of factories.

I mean, a way something like each module registers its factory and since the Shell gets all of these registered.

If your implementations of IModule take a container as ctor parameter you can register each module's implementation of IFactory in the module's Initialize() method.

public class MyModule : IModule
{
  private readonly IUnityContainer container;
  public MyModule(IUnityContainer container)
  {
    this.container = container;
  }
  public void Initialize()
  {
    this.container.Register<IFactory, MyFactory>("MyFactory");
  }
}

public class Shell
{
  public Shell(IFactory[] factories)
  {
    // work with factories
  }
}

I consider the modules to be part of the infrastructure and not the application itself so I find it acceptable to reference the container inside the modules. Nevertheless: If you ever want to move away from Unity you would have to change that code.

If you don't want to use Modules in that way you can have a look at the TecX project . It contains an enhanced configuration engine for Unity that allows you to modularize your container configuration per assembly and collect all that information from a bootstrapper.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM