Search code examples
c#dllmefkinect

How to share a variable in MEF


I have a master project that load many plugins (dll files) in a directory. I would like to share a variable, who is the Kinect declaration, with all the plugins. I want to do that because, the master class use the Kinect but I need it also in my plugins and can't declare it many times.

What's the best way to do it?

Thanks.

EDIT: So I did this:

[ImportingConstructor]
        public MainWindow([Import("myKinect")] KinectSensor myKinect)
        {
            _kinectSensor = myKinect;
        }

        public MainWindow()
        {
            InitializeComponent();
            StartShowVideo();
        }

And in my container:

CompositionContainer container = new CompositionContainer(containerMaster);
            container.ComposeExportedValue("myKinect",_myKinect);
            container.ComposeParts(this);

But it doesn't work, the UI is not loaded. What am I doing wrong?


Solution

  • Giving each plug-in a reference to the Kinect may not be a great idea. By doing so you will force every plug-in to have knowledge of the Kinect API (or your wrapper there of). Worse yet, every plug-in would potentially be able to change the state of the Kinect sensor to something that works for that plug-in, but since no two plug-ins should know anything about each other, changing the state of a shared resource like a kinect sensor could comlete screw every other plug-in. Not to mention you would need to add a mechanism for notifying plug-ins that the Kinect sensor state has changed. I hope you are starting to see the headaches involved with this approach.

    Instead of giving each plug-in a reference to the Kinect, I'd recomend doing the oppposite. Namely, give a centralized Kinect plug-in access to each plug-in that wants to use the Kienct sensor. More concretely, you could create a kinect plug-in class that [ImportMany]s a collection of IKinectClient classes. Then whenever a video frame is ready, or a speech term is recognized, or whatever else happens, the Kinect plug-in can fire a delegate on each IKinectClient plug-in that is interested (has registered) to be notified of that event. This way, you maintaine a centralized place for your actual kinect code and any changes you need to make to how your application interacts with the kinect sensor and/or Kinect SDK are centralized in a single class. This is the power of Inversion of Control!

    NOTE: I gave IKinectClient as an example name for the interface your kinect related interfaces might support. In reality, you'll probably not want a single large itnerface for all kinect interations. Instead, you'll want to segragate your itnerfaces. e.g. IKinectSpeechClient, IKinectVideoClient, IKinectGestureClient, ect.

    With this appraoch, the client kinect plug-ins don't have to know anything about the Kinect itself, nor do they need to know anything about the Kinect API (give the pace at which Kinect for Windows SDKs are coming out, that is a major plus).

    I have worked on this problem before and blogged about it here:

    http://beachfrontcoding.tumblr.com/post/7339567531/kinectaudiopluginforsoapboxcore

    Good Luck