Sharing a dynamic object between loosley coupled Modules Prism?

Topics: Prism v4 - WPF 4
Apr 29, 2012 at 6:54 AM

Hi!

First of all thank you for open source Prism library ;)

Is it possible to share an dynamic type object from a Module between other modules without any static referencing or interface or any other DI/IoC solution?

Lets say we have a GlobalEvent like this in Core.dll assembly:

public class GlobalEvent : CompositePresentationEvent<dynamic>
{
    public GlobalEvent()
    {
    }

    // public void Publish(dynamic payload) exist in base class
}

 

 

and in ModuleA.dll (has static reference to Core.dll), publish it like this:

public void RaiseVoucherAdded(Models.Voucher voucher)
{
    _eventAggregator.GetEvent<GlobalEvent>().Publish(voucher);
}

// also Voucher class is in ModuleA.dll in Models namespace
public class Voucher
{
     // members...
}

 

 

and in ModuleB.dll (has static reference to Core.dll), subscribe like this:

    this._eventAggregator.GetEvent<GlobalEvent>().Subscribe(EventOccured);
    ...
    private void EventOccured(dynamic data)
    {
        if (data.GetType().Name == "ModuleA.Models.Voucher")
        {
            DoSomethingByVoucher(data);
        }
    }

Well,

What are disadvantages of this solution? Is it even possible? If it is, does it support modules that created by any other CLR language in .NET?

Thanks in advance :)

 

Developer
May 2, 2012 at 6:06 PM

Hi,

Based on my understanding of your proposed scenario, this could be achieved without problems, specially if you don't desire any static referencing or interface as you mentioned.

In my opinion this approach could be helpful if you need an element to support any operation at compile time. On the other hand, you may have to be very careful when using it as it can provoke run-time failures that will not appear in a statically typed language where you can catch them at compile time.

On the hand a very similar implementation can be achieved by passing the object type as the payload of the event instead. But this will imply that you will have to cast this object to support any specific operation (e.g. using a common interface). Which way you choose, may depend on your personal preferences and the requirements of your scenario.

Also, as far as I know variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time. Based on this, I believe you shouldn't encountered problems with other CLR languages in .NET.

Additionally, you could find the following MSDN articles interesting:

I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami

May 23, 2012 at 2:53 PM

Thank you for answer. I found it very useful. :)