0
votes

I have a Module for a hardware, which shall be possible to interact from View by the User and from another Module.

Question:

Shall I create a singleton instance in the MyModule_A, register this instance two my container and use this container to resolve the instance in MyModule_B?

Or

Shall I have to use a Eventaggregator two communicate between the two Modules?

The prism documentation is for me not clear in that case.

  • Solution commanding. Use when there is an expectation of immediate action from the user interaction.

  • Region context. Use this to provide contextual information between the host and views in the host's region. This approach is somewhat similar to the DataContext, but it does not rely on it.

  • Shared services. Callers can call a method on the service which raises an event to the receiver of the message. Use this if none of the preceding is applicable.

  • Event aggregation. For communication across view models, presenters, or controllers when there is not a direct action-reaction expectation.

    public class MyModule_A : IModule
    

    { IRegionManager _regionManager; private IUnityContainer _container; private ILoggerFacade _logger;

    public MyModule(IRegionManager regionManager, IUnityContainer container)
    {
        regionManager = regionManager;
        _container = container;
    }
    
    public void Initialize()
    {
        _unityContainer.RegisterType<IMyHardware, MyHardware>(new Containercontrolledlifetime);
    }
    

    }

    public class MyModule_AViewModel : BindableBase { private string _title = "MyModule_A"; private readonly IRegionManager _regionManager; private readonly IUnityContainer _unityContainer; private readonly IUnityContainer _unityContainer; private readonly ILoggerFacade _logger; private IMyHardware _myhardware;

    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }
    
    public MyModuleViewModel(IRegionManager regionManager, IUnityContainer unityContainer)
    {
        _unityContainer = unityContainer;
        _logger = unityContainer.Resolve<ILoggerFacade>();
        _myhardware = unityContainer.Resolve<IMyHardware>();
        _logger.Log("ViewModel created", Category.Debug, Priority.Medium);
    }
    
    
    public class MyModule_B : IModule
    {
        IRegionManager _regionManager;
        private IUnityContainer _container;
        private ILoggerFacade _logger;
        private IHardware _myhardware;
    
        public MyModule(IRegionManager regionManager, IUnityContainer container)
        {
            regionManager = regionManager;
            _container = container;
        }
    
        public void Initialize()
        {
        }
    }
    
    public class MyModule_BViewModel : BindableBase
    {
        private string _title = "MyModule_B";
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _unityContainer;
        private readonly ILoggerFacade _logger;
        private IMyHardware _myhardware;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
    
        public MyModule_BViewModel(IRegionManager regionManager, unityContainer unityContainer)
        {
            _unityContainer = unityContainer;
            _logger = unityContainer.Resolve<ILoggerFacade>();
            _myhardware = unityContainer.Resolve<IMyHardware>();
            _logger.Log("ViewModel created", Category.Debug,Priority.Medium);
       }
    }
    
1
Looks like you could just inject the MyModule into the MyModuleViewModel, wouldn't that be what you need?Henk Holterman
In case, if then I have all properties, commands and instances which are in my MyViewModel, but so fare I understood, if I inject the MyModule, then I dont have the properties,commands etc. which are created inside of the ViewModel. Please adivice me, if I'm wrong.Shazter
Still not very clear, not even what you mean by 'remote'.Henk Holterman
Remote means remoting by other ModuleShazter
Still not clear.Henk Holterman

1 Answers

1
votes

Use a shared service, i.e. this:

Shall I create a singleton instance in the MyModule_A, register this instance two my container and use this container to resolve the instance in MyModule_B?

Create an interface for your driver in a third assembly (not a module) referenced by module a and module b. Then register the driver in module a, and have it injected in the view model or whatever in module b or c or even a.

Sketch:

// in the interface-assembly:

public interface IDriver
{
    bool ReadSensor();
}

// in module a:

internal class MyDriver : IDriver ...

public class MyModuleA : IModule
{
    public void Initialize()
    {
        _container.RegisterType<IDriver, MyDriver>( new ContainerControlledLifetimeManager() );
    }
}

// in module b:

internal class MyViewModel
{
    public MyViewModel( IDriver theHardware ) ...
}