jkl
jkl

Reputation: 675

C# WPF Prism - Share a same object between different modules(project)

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.

Upvotes: 1

Views: 1461

Answers (3)

Haukinger
Haukinger

Reputation: 10863

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.

Upvotes: 2

SwDevMan81
SwDevMan81

Reputation: 49988

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));
   }
}

Upvotes: 1

Jophy job
Jophy job

Reputation: 1964

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

Upvotes: 1

Related Questions