Question

I have two .NET parties who needs be bound by a contract. Now, party1 and party2 need to be able call some methods on each other (most of it is calls and reporting result back). I have duplex contract in mind, but the parties are not using WCF.

Is there a design pattern for this?

Edit

The parties are part of the same application. I create the application (party1) and someone else creates a dll (party2) that I load dynamically. Now, both of us should be able to call methods on each other. So, I am out to create an interface contract between us. The intent is to know whether there is a know pattern to do that?

Was it helpful?

Solution

A common solution is to use some kind of pub/sub pattern. By doing so you can avoid circular dependencies.

Basically you create some kind of class which are used to subscribe on events (and publish them).

So both your classes does something like this (but with different events):

public class ClassA : IEventHandler<UserCreated>
{
    IEventManager _eventManager

    public ClassA(IEventManager manager)
    {
       // I subscribe on this event (which is published by the other class)
       manager.Subscribe<UserCreated>(this);
       _eventManager = manager;
    } 

    public void Handle(UserCreated theEvent)
    {
        //gets invoked when the event is published by the other class
    }

    private void SomeInternalMethod()
    {
        //some business logic

        //and I publish this event
        _eventManager.Publish(new EmailSent(someFields));
    }
}

The event manager (simplified and not thread safe):

public class EventManager
{
    List<Subscriber> _subscribers = new List<Subscriber>();

    public void Subscribe<T>(IEventHandler<T> subscriber)
    {
        _subscribers.Add(new Subscriber{ EventType = typeof(T), Subscriber = subscriber});
    }

    public void Publish<T>(T theEvent)
    {
        foreach (var wrapper in subscribers.Where(x => x == typeof(theEvent)))
        {
            ((IEventHandler<T>)wrapper.Subscriber).Handle(theEvent);
        }
    }
}

The small wrapper:

public class Subscriber
{
    public Type EventType;
    public object Subscriber;
}

Voila. the two classes are now loosely coupled from each other (while still being able to communicate with each other)


If you use an inversion of control container it get's easier since you can simplify the event manager and just use the container (service location) to resolve all subscribers:

public class EventManager
{
    IYourContainer _container;

    public EventManager(IYourContainer container)
    {
        _container = container;
    }

    public void Publish<T>(T theEvent)
    {
        foreach (var subscriber in _container.ResolveAll<IEventHandler<T>>())
        {
            subscriber.Handle(theEvent);
        }
    }
}

OTHER TIPS

I think you can use next logic:

 Class1: Interface1 , Class2:Interface2, 

class Manager{
   public Manager(Interface1 managedPart1,Interface2 managedPart2){
        ... some logic for connect to interfaces
   }

}

This way reminds me pattern Bridge, but this is very subjective

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top