MEF helper class for VB.NET

This is a class I created to help me with MEF. It might be useful to somebody.

It is a singleton with IDisposable to create a shared object for your application’s lifetime.

It also features lambda queries for finding metadata as well.

#Region "Compiler Options"
Option Strict Off
Option Explicit On
Option Compare Binary
#End Region
#Region "Library Imports"
Imports System.ComponentModel.Composition
Imports System.ComponentModel.Composition.Hosting
#End Region
' Template version 1.2.0.2. Code developed for framework v2.0.50727.3074
' This code is copyright (c) 2009 ActiveAsp Software. All rights reserved.
''' <summary>
''' Managed Extensibility Framework Helper Class
''' </summary>
''' <remarks>
''' Singleton object
''' -- Change Log --------------------------------------------------------
''' 4/5/2009 11:55:06 PM by Rick - Initial Creation
''' 
''' </remarks>
Public NotInheritable Class MefHelper
    Implements IDisposable
#Region "Constructor / Deconstructor"
    ''' <summary>
    ''' The starting point for this class
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub New()
        ' Enter Shared Class Creation Code Here
        Dim sPathInitial As String = System.IO.Path.GetDirectoryName(GetType(MefHelper).Assembly.Location)
        _ExtensionsPath = System.IO.Path.Combine(sPathInitial, "Extensions")
    End Sub
#Region " IDisposable Support "
    Private disposedValue As Boolean = False        ' To detect redundant calls
    ' IDisposable
    Protected Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                _Container.Dispose()
                _Container = Nothing
            End If
        End If
        Me.disposedValue = True
    End Sub
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region
#End Region
#Region "Public"
#Region "Public Property Declarations"
    Private _Container As CompositionContainer = Nothing
    Public ReadOnly Property Container() As CompositionContainer
        Get
            Return _Container
        End Get
    End Property
    Private _ExtensionsPath As String = ""
    Public Property ExtensionsPath() As String
        Get
            Return _ExtensionsPath
        End Get
        Set(ByVal value As String)
            If Not _ExtensionsPath = value Then
                _ExtensionsPath = value
            End If
        End Set
    End Property
#End Region
#Region "Public Shared Property Declarations"
    ' Properties
    Public Shared ReadOnly Property Instance() As MefHelper
        Get
            If (MefHelper._instance Is Nothing) Then
                SyncLock MefHelper.syncRoot
                    If (MefHelper._instance Is Nothing) Then
                        MefHelper._instance = New MefHelper
                    End If
                End SyncLock
            End If
            Return MefHelper._instance
        End Get
    End Property
    ' Fields
    Private Shared _instance As MefHelper 'ModReq(IsVolatile)
    Private Shared syncRoot As Object = New Object
#End Region
#Region "Public Methods"
    Public Sub Compose()
        Dim Catalog As New AggregateCatalog()
        ' Add This assembly's catalog parts
        Catalog.Catalogs.Add(New AssemblyCatalog(GetType(MefHelper).Assembly))
        ' Directory of catalog parts
        If System.IO.Directory.Exists(_ExtensionsPath) Then
            Catalog.Catalogs.Add(New DirectoryCatalog(_ExtensionsPath))
        End If
        _Container = New CompositionContainer(Catalog)
    End Sub
    Public Function GetExportByContractType(Of T)() As Export(Of T)
        Dim RetVal As Export(Of T) = Nothing
        Try
            RetVal = _Container.GetExport(Of T)()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByContractType(Of T, U)() As Export(Of T, U)
        Dim RetVal As Export(Of T, U) = Nothing
        Try
            RetVal = _Container.GetExport(Of T, U)()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByContractType(Of T)() As Generic.List(Of Export(Of T))
        Dim RetVal As Generic.List(Of Export(Of T)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByContractType(Of T, U)() As Generic.List(Of Export(Of T, U))
        Dim RetVal As Generic.List(Of Export(Of T, U)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByContractName(Of T)(ByVal name As String) As Export(Of T)
        Dim RetVal As Export(Of T) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) String.Compare(p.Definition.ContractName, name, True) = 0).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByContractName(Of T, U)(ByVal name As String) As Export(Of T, U)
        Dim RetVal As Export(Of T, U) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) String.Compare(p.Definition.ContractName, name, True) = 0).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByContractName(Of T)(ByVal name As String, ByVal value As String) As Generic.List(Of Export(Of T))
        Dim RetVal As Generic.List(Of Export(Of T)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) String.Compare(p.Definition.ContractName, name, True) = 0).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByContractName(Of T, U)(ByVal name As String, ByVal value As String) As Generic.List(Of Export(Of T, U))
        Dim RetVal As Generic.List(Of Export(Of T, U)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) String.Compare(p.Definition.ContractName, name, True) = 0).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByMetadata(Of T)(ByVal name As String) As Export(Of T)
        Dim RetVal As Export(Of T) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) p.Metadata.ContainsKey(name) = True).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByMetadata(Of T)(ByVal name As String, ByVal value As String) As Export(Of T)
        Dim RetVal As Export(Of T) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) String.Compare(p.Metadata(name).ToString(), value, True) = 0).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByMetadata(Of T, U)(ByVal name As String, ByVal value As String) As Export(Of T, U)
        Dim RetVal As Export(Of T, U) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) String.Compare(p.Metadata(name).ToString(), value, True) = 0).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportByMetadata(Of T, U)(ByVal name As String) As Export(Of T, U)
        Dim RetVal As Export(Of T, U) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) p.Metadata.ContainsKey(name) = True).Single()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByMetadata(Of T)(ByVal name As String, ByVal value As String) As Generic.List(Of Export(Of T))
        Dim RetVal As Generic.List(Of Export(Of T)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) String.Compare(p.Metadata(name).ToString(), value, True) = 0).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByMetadata(Of T)(ByVal name As String) As Generic.List(Of Export(Of T))
        Dim RetVal As Generic.List(Of Export(Of T)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T).Where(Function(p) p.Metadata.ContainsKey(name) = True).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByMetadata(Of T, U)(ByVal name As String, ByVal value As String) As Generic.List(Of Export(Of T, U))
        Dim RetVal As Generic.List(Of Export(Of T, U)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) String.Compare(p.Metadata(name).ToString(), value, True) = 0).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
    Public Function GetExportsByMetadata(Of T, U)(ByVal name As String) As Generic.List(Of Export(Of T, U))
        Dim RetVal As Generic.List(Of Export(Of T, U)) = Nothing
        Try
            RetVal = _Container.GetExports(Of T, U).Where(Function(p) p.Metadata.ContainsKey(name) = True).ToList()
        Catch ex As Exception
        End Try
        Return RetVal
    End Function
#End Region
#End Region
End Class

Download the code

5. April 2009 23:02 by Rick | Comments (2) | Permalink

Using MEF and querying the catalog container with VB.NET

I am using the Managed Extensibility Framework preview 4 in VB.NET, and I have to say, that it rocks!

The project I am writing is a UI container that has “add-ins” that process files and such. I am working with another programmer to develop other add-ins for the app as well.

To query for specific metadata, we first have to create an interface and a custom attribute.

Public Interface IMefMetadataView
    ReadOnly Property Name() As String
End Interface
<MetadataAttribute()> _
Public Class MefMetadata
    Inherits Attribute
    Public Sub New()
    End Sub
    Public Sub New(ByVal name As String)
        _Name = name
    End Sub
    Private _Name As String = ""
    Public ReadOnly Property Name() As String
        Get
            Return _Name
        End Get
    End Property
End Class

This attribute allows you to specify a name of the add-in. Then we create an interface for our add-in.

    Public Interface IAddin
    End Interface

Then we can create an add-in or two.

	<Export(GetType(IAddin))> _
	<AddinMetadata("Add-in one")> _
	Public Class Addin1
	    Implements IAddin
	
	End Class
	
	<Export(GetType(IAddin))> _
	<AddinMetadata("Add-in two")> _
	Public Class Addin2
	    Implements IAddin
	
	End Class

Now, in our host user interface, we create a catalog and a container. Specifically we are creating an AggregateCatalog (a catalog of catalogs if you will) and then creating a catalog for just our assembly, then we are adding it into the container.

    Dim Container as CompositionContainer
    Dim Catalog As New AggregateCatalog()
    Dim AssemCatalog As New AssemblyCatalog(GetType(Module1).Assembly)
    Catalog.Catalogs.Add(AssemCatalog)
    Container = New CompositionContainer(Catalog)

Querying:

Get a list of addins:

    Dim Addins As Generic.List(Of Export(Of IAddin, IMefMetadataView)) = _
        Container.GetExports(Of IAddin, IMefMetadataView).ToList()

Query for a specific add-in with a name in the metadata using a lambda function:

Dim x As Export(Of IAddin, IMefMetadataView) = Addins.Where(Function(p) String.Compare(p.Metadata("Name").ToString(), "Add-in one", True) = 0).Single()

Or

Dim x As Export(Of IAddin, IMefMetadataView) = Addins.Where(Function(p) String.Compare(p.MetadataView.Name, "Add-in one", True) = 0).Single()

The box directly above allows us to query strongly typed metadata, thanks to the IMefMetadataView Interface and the MefMetadata class.

kick it on DotNetKicks.com
5. April 2009 21:15 by Rick | Comments (549) | Permalink

About Rick

Rick lives in North Las Vegas. He loves his wife, kids, dog, motorcycle, music and programming. There ain't nothing else. Oh yeah, mountain dew!



Programming interests are geared towards multimedia. Platforms are asp.net, windows forms, and WPF.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar

RecentComments

Comment RSS