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

Comments

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