Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interacting between projects

I have two projects. ProjectA has a class called Task and projectB has class called Transmission. I would like that whenever Transmission is changed - something will happen in Task and whenever Task is changed - something will happen in Transmission.

For that I created another project called Common where both ProjectA and projectB will have reference to. In common I put all the interfaces to interact between both entities.

In Common project I created interface called IManager which ProjectA will implement. The role of the Manager is to handle the updates. The interface look like this:

public interface IManager
{
    ITaskChangedHandler TaskChangedHandler { set; }

    void OnTransmissionChanged(ITransmissionWithTasks transmission);
}

This interface has 2 methods ProjectA has to implement:

  1. void OnTransmissionChanged(ITransmissionWithTasks transmission); When there is a change in transmission (inside ProjectB) this method is called in order to make an update in tasks.

  2. ITaskChangedHandler TaskChangedHandler { set; } The IManager implementation has to have setter to ITaskChangedHandler. ITaskChangedHandler is an interface that defines a method for update transmission in case of task is changed:

    public interface ITaskChangedHandler   
    {   
        void OnTaskChanged(string moduleName, ITask task, long transmissionId);   
    }   
    

Now, it goes like this:

  1. ProjectA has implementation to IManager.

  2. Whenever there is a cahnge in Transmission I use StructureMap to receive instance of IManager implementation and then call OnTransmissionChanged();

  3. This is my problem - Whenever there is a change in Task (in ProjectA) I would like to call IManager implementation's TaskChangedHandler.OnTaskChanged(). But the problem is that the instance of IManager implementation doesn't set the TaskChangedHandler.
    I also don't know where should I set this TaskChangedHandler:

    • This can't be in ProjectA because it don't know the implementation of ITaskChangedHandler.

    • This can't be in ProjectB because the creation of IManager implementation happens in ProjectA, so the set has to be in projectA.

    • This can't be in Common project because it don't know the implementations of IManager and ITaskChangedHandler.

Can anybody help me to solve this issue?

like image 201
Naor Avatar asked Nov 04 '22 16:11

Naor


1 Answers

Here's a quick-and-dirty method that might get you thinking: a static, shared class, you can define in your shared assembly.

public static class SharedEvents
{
    public delegate void FirstEventHandler();
    public delegate void SecondEventHandler();

    public static event FirstEventHandler FirstEvent;
    public static event FirstEventHandler SecondEvent;

    public static void OnFirstEvent()
    {
        if (FirstEvent != null) FirstEvent();
    }

    public static void OnSecondEvent()
    {
        if (!SecondEvent != null) SecondEvent();
    }
}

One registers to handle the FirstEvent, another to handle the SecondEvent: to call each other, call OnFirstEvent() or OnSecondEvent() accordingly.

This is a simple, and pretty poor, implementation of the Mediator pattern:

http://en.wikipedia.org/wiki/Mediator_pattern

With dependency injection, you can get an instance to an object based on a shared interface, too. For example with Ninject, you could register a handler for IFirst to be a constant (specific instance of something that implements IFirst) and vice-versa. Then call Get<IFirst> to grab it.

like image 99
Kieren Johnstone Avatar answered Nov 15 '22 04:11

Kieren Johnstone