1

I have multiple modules(each module in a different project) in my prism project. And I would like to share a same object between each module. For example, let say I have Order class and I would like to access this Order object in my modules.

Currently, I implemented an interface and registered it with container in my Prism project.

public interface ICommonService{ 
    Order GetData();
}

public class CommonService : ICommonService{
    public Order MyOrder{ get; set; }

    public Order GetData(){
        return MyOrder;
    }
    public void SetData(Order order){
        MyOrder = order;
    }
}

I am using it in every module where it need MyOrder. Is this a correct way of sharing a same object between modules?

Also, my View Models classes contains several Manager classes. Should only View Model classes use ICommonService or can my Manager classes also use it?

I am trying to write clean and manageable code.

Thank you.

jkl
  • 675
  • 2
  • 8
  • 23
  • The question is not very clear as what is the actual issue you're trying to solve? As PRISM is by definition reference design, it provides example solutions to most of the common scenarios... see here: https://stackoverflow.com/questions/34741727/example-of-using-shared-services-prism and here: https://social.msdn.microsoft.com/Forums/en-US/22907a0f-d805-4195-8272-7c284b72d2ee/example-of-using-shared-services-prism?forum=wpf – Amittai Shapira Dec 20 '17 at 20:14

3 Answers3

2

Is this a correct way of sharing a same object between modules?

Yes.

Should only View Model classes use ICommonService or can my Manager classes also use it?

The manager classes are fine to use the service.

Notes:

  1. You should include a means of notifying other consumers of ICommonService when MyOrder changes. Examples: implement INotifyPropertyChanged or publish a MyOrderChanged event through the event aggregator

  2. Normally, anyone who can access a service (read: knows the interface), should be allowed to do so. It's better to restrict the accessibility of the interface (by putting it in a separate assembly) than restricting accessibility of the service (by documentation), because the former is enforced by the compiler.

Haukinger
  • 10,420
  • 2
  • 15
  • 28
1

You can use Event Aggregation

The Prism Library provides an event mechanism that enables communications between loosely coupled components in the application. This mechanism, based on the event aggregator service, allows publishers and subscribers to communicate through events and still do not have a direct reference to each other.

Link Communicating Between Loosely Coupled Components

Prism

Sample

Jophy job
  • 1,924
  • 2
  • 20
  • 38
  • 1
    isn't Event Aggregation for communicating between modules?Like do certain job as soon as Event is published?But in my case, each of my module needs to use the data only when they are needed. – jkl Dec 20 '17 at 19:19
1

When you define your module, you should specify an dependencies in the constructor for that module. For example:

public class SomeModule : IModule
{
   public SomeModule(ICommonService commonService)
   {
      // commonService will be shared object
   }
}

In your Bootstrapper, when you add the module to the catalog, it will look through the DI container to look up the type. If you have it set to global reference, it will use the same object for all references to ICommonService.

class Bootstrapper : UnityBootstrapper
{
   protected override void ConfigureContainer()
   {
      base.ConfigureContainer();
      RegisterTypeIfMissing(typeof(ICommonService), 
         typeof(CommonService), true); // true for register as singleton
   }

   protected override void ConfigureModuleCatalog()
   {
      base.ConfigureModuleCatalog();
      ModuleCatalog module_catalog = (ModuleCatalog)this.ModuleCatalog;
      module_catalog.AddModule(typeof(SomeModule));
   }
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184