For any of those that are interested, I ended up creating an interface IRequireSession which has the following declaration
public interface IRequireSession
{
ISession Session { get; set; }
}
And is implemented in a Form base class : public class DataForm : Form, IRequireSession
{
private ISession _session;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Import()]
public ISession Session
{
get { return _session; }
set {
_session = value;
SetSession(this.Controls);
}
}
private void SetSession(Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control is IRequireSession)
((IRequireSession)control).Session = _session;
SetSession(control.Controls);
}
}
}
This means that I can derive my normal forms from DataForm and get the session passed through to any user controls that implement IRequireSession. Although, I've got a funny feeling that MEF doesn't support attributes on base classes. . I guess I'll find out soon enough.