Ramin Bateni
Ramin Bateni

Reputation: 17425

WPF Prism access to defined info like some class objects inside Module from main project

I have a WPF Prism solution in VS and want keep some settings of my prism module inside an object in the module project (i load this settings from a file), then i created my prism module class (AModule.cs) like this:

Inside my module project (AModul project):

[Export]
[Module(ModuleName = "AModule")]
public class AModule : IModule
{
    ModuleSettingsModel ModuleSettings { get ; set; }

    public AModule()
    {
        // throw new NotImplementedException();
    }

    public AModule(IUnityContainer container)
    {
        // Some codes ....
    }

    ~AModule()
    {
        // some codes
    }

    public void Initialize()
    {
        var regionManager = _container.Resolve<IRegionManager>();
        regionManager.RegisterViewWithRegion("WorkspaceRegion", typeof(ModuleAView));


        ModuleSettings = DataFileManager.LoadModuleData();
    }

It works well and i can use my settings inside AModule


Inside Main WPF Project:

But i need access this settings (ModuleSettings property) in my Main WPF project too. For example i need access to ModuleA»ModuleSettings in my Bootstrapper class of my WPF application. I need do some workd base on each module settings in my main project...

My question is what solutions are there to do? Should i register any type? Where? How?


Note1: ModuleSettings* is inherited from IModuleSettings and IModuleSettings is inside Infrastructure project.

Note2: I load my modules dynamically into prism (my main WPf project has not any reference to AModule);

Upvotes: 2

Views: 521

Answers (2)

Ramin Bateni
Ramin Bateni

Reputation: 17425

After read GlenThomas answer & @BenediktSchroeder's first comment in question and reading some additional references like this & this, i could found & implement a good way for my question:

I solved it by adding a new custom Prism Service

  1. Create an Interface for our service class (eq. ISettingManager) in Infrastructure Project. Modules and Main App need reference to Infrastructure Project
  2. Create an Class for our service (eq. SettingManager)
  3. Define one property in ISettingManager to keep ModuleSetting like:

    Dictionary<string,ModuleSettingsModel> ModuleSettingsPairs{get;set;}

  4. Add some methods in ISettingManager to Get, Add and Remove setting to/from the ModuleSettingsPairs dictionary.

  5. Register the new service type in ConfigureContainer inside the bootstraper as Singlton like:

    RegisterTypeIfMissing(typeof(IAddonSettingsManager), typeof(AddonSettingsManager), true);

  6. Obtain reference to the IAddonSettingsManager service instance in the module project or main app project, it can do by one of following ways:

    • Declaratively obtain references to services using constructor injection
    • Programmatically obtain references to services (by use the Resolve method of the Unity container)
  7. Finally, play with custom Get, Add and Remove methods of our service instance (SettingManager)

Upvotes: 0

Glen Thomas
Glen Thomas

Reputation: 10764

You should put the ModuleSettings into a class library project that is referenced by the various modules/projects. You could use the singleton pattern to only load and maintain one instance of the settings.

Upvotes: 2

Related Questions