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

Multitouch table - Step 1 – Find the right table and monitor

Microsoft Surface at the Rio

Left: Microsoft Surface at the Rio Hotel and Casino

As i said in my last post, I want to make a multitouch table like the Microsoft Surface. I have purchased a table, vellum, tracing paper and 5 LED IR lamps. What happens is, you shine the LED IR lamps up through the tracing paper or vellum, and then the webcam you have below can detect your fingerprints on the vellum, and send those as points to the appropriate software.

Table

I already got the Acer 22’’ X223W monitor, and 2 LED IR Illuminators, 3 more are on the way. Today I bought a table. I went to a furniture store that had clearance items. This table was WAY cheap due to the glass being missing. It was the right height for my table, so I got it! As you can see on the page on the right, I made holes with my drill so I can cut the square out where the top of the drawer is.

Table with the hole cut

This way the webcam and IR LED lights can shine upwards. I plan on taking out the monitor and putting it on the top with a piece of Acrylic or Plexiglas on it. The hole in the top is EXACTLY the right size (well, more or less).

I also purchased a roll of vinyl flooring, for $20 so I can cut it and wrap it around as the walls, to keep the IR Light from escaping, and a small roll of black shelf paper for the top.

The next step is to get the rest of the lights, make sure that the blobs (finger points) work correctly, then add the LED and the top glass.

4. April 2009 17:38 by Rick | Comments (0) | Permalink

Multitouch table

I am going to make a multitouch table, much like the Microsoft Surface. There are two schools of thought on multitouch tables, projector or LCD. With LCD, you actually take the LCD out of the casing, and using a backlight, can mount it into the table, like so:

 lcd
Image shamelessly ripped off from http://wiki.nuigroup.com/Diffused_Illumination_Plans

I got the monitor today, an Acer X223W. I plan on posting as I go with this.

1. April 2009 02:28 by Rick | Comments (0) | 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

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

RecentComments

Comment RSS