The Managed Extensibility Framework (MEF) is a .NET Framework 4 library that simplifies the design of extensible applications and components. Office workflow activity designers utilize MEF assembly exports and imports to enable expression builder plug-ins. The current code base includes three expression builders; NameValueFormBuilder, SimpleFormBuilder and MenuItemsBuilder.

How to resolve Expression Builders

Step 1 - Expression Editor Service

    public IExpressionEditorInstance CreateExpressionEditor(AssemblyContextControlItem assemblies, 
ImportedNamespaceContextItem importedNamespaces, List<ModelItem> variables, string text,
Type expressionType, System.Windows.Size initialSize) { //Resolve MEF Builders var catalog = new AggregateCatalog(new DirectoryCatalog(Properties.Settings.Default.ExtensionsPath),
new AssemblyCatalog(Assembly.GetExecutingAssembly())); var container = new CompositionContainer(catalog); instance = container.GetExportedValue<MyExpressionEditor>(); instance.Text = text; instance.Variables = variables; instance.ExpressionType = expressionType; return instance; }

Step 2 - Expression Editor Control 

    [ImportMany(AllowRecomposition = true)]
    public ObservableCollection<Lazy<IExpressionExtension, IExpressionBuilderMetadata>> extensions { get; set; }

Step 3 - Resolve Builders with Extension<IExpressionItemsProvider>

The Extension<TExtensionType> method resolves builder extensions from a configuration store.

    private TExtensionType Extension<TExtensionType>(string path, string extensionAttribute)
    {
      TExtensionType extension = default(TExtensionType);

      List<Lazy<IExpressionExtension, IExpressionBuilderMetadata>> targetExtensions = null;

      if (expressionMappings.ContainsKey(path))
      {
        if (expressionMappings[path].ExtensionElement.Attribute(extensionAttribute) != null)
        {
          string typeName = expressionMappings[path].ExtensionElement.Attribute(extensionAttribute).Value;

          //extension.GetType().ToString() style not used so a target extension is not coupled to the type.
          //The only coupling is the MetaData attribute
          //MetaData style
          targetExtensions =
            (extensions.Where(v => v.Metadata.Name.Equals(typeName)).Select(v => v))
.ToList<Lazy<IExpressionExtension, IExpressionBuilderMetadata>>(); if (targetExtensions.Count > 0) { extension = (TExtensionType)targetExtensions[0].Value; } } } return extension; }

Step 4 - Resolve MEF Builders defined in configuration store

        expressionMappings = ExpressionMappings.Fill(Properties.Settings.Default.ExpressionEditor);

        //Resolve MEF builder
        pathBuilder = this.ResolveBuilder(this.ExpressionPath);

        if (pathBuilder != null)
        {
          pathBuilder.ItemsProvider = Extension<IExpressionItemsProvider>
            (this.ExpressionPath, ExpressionMappings.ExtensionAttributes.itemsProviderType.ToString());

          pathBuilder.Converter = Extension<IExpressionTypeConverter>
            (this.ExpressionPath, ExpressionMappings.ExtensionAttributes.converterType.ToString());

          pathBuilder.OnUpdated = Builder_OnUpdated;

          expressionMenu.Visibility = Visibility.Visible;
        }

        if (this.ExpressionPath != null)
        {
          expressionMenu.Click += new RoutedEventHandler(ExpressionMenu_Click);
        }  

Sample Expression Builders

NameValueFormBuilder

image

 

SimpleFormBuilder

image

MenuItemsBuilder

image

Last edited Oct 14, 2011 at 3:07 AM by dvana, version 15

Comments

No comments yet.