How does the WebForms and MEF Sample handle UserControl markup?

Jul 13, 2010 at 9:42 AM
Edited Jul 13, 2010 at 9:45 AM

Given the implementation of MefAwareControlBuilder the MefUserControl will be constructed by MEF and not by the System.Web.Compilation.BuildManager. That would imply that no markup can exist within a MefUserControl becuase the FrameworkInitialize code generate by the BuildManager wont be there.

I'm a bit uncomfortable with traversal of the existing CodeDom but it does have some appealing properties. Any comments on this?

Jul 16, 2010 at 3:46 PM

Actually I dont think that's accurate. The user control will be compiled dynamically. The behavior change happen in the control tree production, which instead of invoking the new operator, will delegate to MEF.

Jul 19, 2010 at 7:21 AM
Edited Jul 19, 2010 at 8:32 AM

The control tree production happens through sub-classing, does it not? GetType() of any user control will return a type name derived from the virtual file name in a temporary assembly, if you instantiate the subclass that's not provided by the BuildManager then you effectively instantiate the same user control without markup. You can check this with the .NET reflector, I haven't put the MefAwareControlBuilder to the test but I'm fairly certain that it won't work or at the very least I cannot phantom how it could as relies on sub-classing for control tree production.

Also, to be clear, I'm talking about the user control itself. The newing of objects will delegate to MEF but these objects not all objects can be created by MEF. e.g. A user control containing another user control. Assuming that the second is a MefAwareUserControl it will be newed by MEF and not the BuildManager which sub-classes the type that MEF would create. These are two different things and I do not see how they could work together in this case.

Here's an example of what happens with a user control in terms of code generation. If MEF doesn't return a type that is compatible with webusercontrol1_ascx it will cause a compiler error. Since MEF can't possibly know of this type as it is a anonymous compiler generated type how could it find any valid exports?

private webusercontrol1_ascx __BuildControl__control5()
    webusercontrol1_ascx __ctrl = new webusercontrol1_ascx();
    return __ctrl;

Also, constructing the object this way, how is that in practice different from say, calling SatisfyImportsOnce on "this" from within the constructor?